首页 > 代码库 > Sona
Sona
Sona
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.
InputThis 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.OutputFor 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
Hint
无
题目的大意就是,给定一个序列,然后给出一些区间的询问,求出区间内每一类数字的数量的立方和.
显然,这是离线题,非常适合用莫队去做.主要的地方还是在remove和add两个函数内.
remove:
某种数字的数目从x变成了x-1,所以ans-=x^3-(x-1)^3=3x^2-3x+1
add:
某种数字的数目从x变成了x+1,所以ans+=(x+1)^3-x^3=3x^2+3x+1
至此,题目也就解完了.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define LL long long 5 using namespace std; 6 int n,Q,K,blocks,cc[100005]; 7 LL cnt[100005],ans; 8 struct query{ 9 int L,R,index; LL ans; 10 }a[100005]; 11 struct data{ 12 int x,index; 13 }c[100005]; 14 inline int read(){ 15 int x=0; char ch=getchar(); 16 while (ch<‘0‘||ch>‘9‘) ch=getchar(); 17 while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar(); 18 return x; 19 } 20 bool cmp_x(data u,data v){return u.x<v.x;} 21 bool cmp_index(data u,data v){return u.index<v.index;} 22 bool cmp_blocks(query u,query v){return u.L/blocks==v.L/blocks?u.R<v.R:u.L<v.L;} 23 bool cmp_id(query u,query v){return u.index<v.index;} 24 void remove(int p){ans-=(LL)3*cnt[c[p].x]*cnt[c[p].x]-(LL)3*cnt[c[p].x]+1,cnt[c[p].x]--;} 25 void add(int p){ans+=(LL)3*cnt[c[p].x]*cnt[c[p].x]+(LL)3*cnt[c[p].x]+1,cnt[c[p].x]++;} 26 int main(){ 27 while (scanf("%d",&n)==1){ 28 for (int i=1; i<=n; i++) if (i*i>n){blocks=i-1; break;} 29 for (int i=1; i<=n; i++) c[i].x=read(),c[i].index=i; 30 sort(c+1,c+1+n,cmp_x); 31 int cntnew=1; 32 memset(cc,0,sizeof cc),cc[1]=1; 33 for (int i=2; i<=n; i++) if (c[i].x==c[i-1].x) cc[i]=cntnew; else cc[i]=++cntnew; 34 for (int i=1; i<=n; i++) c[i].x=cc[i]; 35 sort(c+1,c+1+n,cmp_index); 36 Q=read(); 37 for (int i=1; i<=Q; i++) a[i].L=read(),a[i].R=read(),a[i].index=i; 38 sort(a+1,a+1+Q,cmp_blocks); 39 int curL=1,curR=0; 40 memset(cnt,0,sizeof cnt); ans=0; 41 for (int i=1; i<=Q; i++){ 42 while (curL<a[i].L) remove(curL++); 43 while (curR>a[i].R) remove(curR--); 44 while (curL>a[i].L) add(--curL); 45 while (curR<a[i].R) add(++curR); 46 a[i].ans=ans; 47 } 48 sort(a+1,a+1+Q,cmp_id); 49 for (int i=1; i<=Q; i++) printf("%I64d\n",a[i].ans); 50 } 51 return 0; 52 }
Sona