Sona(NBUT-1457)
Problem Description
Sona , Maven of the Strings . Of cause, she can play the zither.
Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.
There're an ancient score(樂譜). But because it's too long, Sona can't play it in a short moment. So Sona decide to just play a part of it and revise it.
A score is composed of notes. There are 109 kinds of notes and a score has 105 notes at most.
To diversify Sona's own score, she have to select several parts of it. The energy of each part is calculated like that:
Count the number of times that each notes appear. Sum each of the number of times' cube together. And the sum is the energy.
You should help Sona to calculate out the energy of each part.
Input
This problem contains several cases. And this problem provides 2 seconds to run.?
The first line of each case is an integer N (1 ≤ N ≤ 10^5), indicates the number of notes.?
Then N numbers followed. Each number is a kind of note. (1 ≤ NOTE ≤ 10^9)?
Next line is an integer Q (1 ≤ Q ≤ 10^5), indicates the number of parts.?
Next Q parts followed. Each part contains 2 integers Li and Ri, indicates the left side of the part and the right side of the part.
Output
For each part, you should output the energy of that part.
Sample Input
8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5
Sample Output
128
72
2
1
題意:給出 n 個數和 m 組詢問,對于每組詢問給出一個區間 [l,r],求區間內每種數字出現次數的立方和
思路:普通莫隊+離散化
很基礎的普通莫隊,直接修改 O(1) 部分即可,由于數據范圍,需要進行離散化
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=10007; const int N=500000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std;struct Node{int l,r;//詢問的左右端點int id;//詢問的編號 }q[N]; int n,m,a[N],temp[N]; int block;//分塊 LL ans,cnt[N]; LL res[N];bool cmp(Node a,Node b){//奇偶性排序return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r); }void add(int x){//統計新的ans-=cnt[a[x]]*cnt[a[x]]*cnt[a[x]];cnt[a[x]]++;ans+=cnt[a[x]]*cnt[a[x]]*cnt[a[x]]; } void del(int x){//減去舊的ans-=cnt[a[x]]*cnt[a[x]]*cnt[a[x]];cnt[a[x]]--;ans+=cnt[a[x]]*cnt[a[x]]*cnt[a[x]]; } int main(){while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;++i){scanf("%d",&a[i]);temp[i]=a[i];}//離散化sort(temp+1,temp+1+n);int len=unique(temp+1,temp+1+n)-temp;for(int i=1;i<=n;i++)a[i]=lower_bound(temp+1,temp+1+len,a[i])-temp;scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}ans=0;memset(cnt,0,sizeof(cnt));block=n/sqrt(m*2/3*1.0);//分塊,防卡常數sort(q+1,q+m+1,cmp);//對詢問進行排序int l=1,r=0;//左右指針for(int i=1;i<=m;i++){int ql=q[i].l,qr=q[i].r;//詢問的左右端點while(l>ql) add(--l);//[l-1,r]while(l<ql) del(l++);//[l+1,r]while(r<qr) add(++r);//[l,r+1]while(r>qr) del(r--);//[l,r-1]res[q[i].id]=ans;//獲取答案}for(int i=1;i<=m;i++)printf("%lld\n",res[i]);}return 0; }?
總結
以上是生活随笔為你收集整理的Sona(NBUT-1457)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Crash的数字表格(HYSBZ-215
- 下一篇: Menagerie(AtCoder-22