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 has105 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.
8 1 1 3 1 3 1 3 3 4 1 8 3 8 5 6 5 5
128 72 2 1
无
XadillaX
莫队算法,参考我上一篇博客吧。或者搜索我的“莫队算法”的文章。
对于每个询问 [l,r]按l分块,每sqrt(n)一块,同一块内按R递增排序。
LL,RR指针标记处理的区间,转移是O(1)的。先离散化数字,用一个数组统计这个数字出现的次数
如果次数加1 那么 ans = ans + num[i]*num[i] - (num[i])-1*(num[i]-1)
加一个输入外挂快些900+ms,不用也行。1250ms。挺快的 了。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<math.h> using namespace std; #define maxn 100007 #define ll long long int num[maxn],color[maxn],ord[maxn]; ll ans[maxn]; struct Node{ int l,r,id; ll ans; }; Node query[maxn]; int squ; inline int comp(const Node &a,const Node &b){ if(a.l / squ == b.l / squ) return a.r < b.r; return a.l/squ < b.l/squ; } int getn(){ int a = 0; char x; while(1){ x = getchar(); if(x==' ' || x == '\n') return a; a = a*10+x-'0'; } } ll po3[maxn]; inline int compid(const Node&a,const Node &b){ return a.id < b.id; } int main(){ int n,m,q; for(int i = 0;i < maxn; i++) po3[i] = 1ll*i*i*i; while(scanf("%d",&n)!=EOF){ getchar(); for(int i = 1;i <= n; i++){ //scanf("%d",&color[i]); color[i] = getn(); ord[i] = color[i]; } sort(ord+1,ord+n+1); int m = unique(ord+1,ord+1+n)-(ord+1); for(int i = 1;i <= n; i++) color[i] = lower_bound(ord+1,ord+1+m,color[i])-(ord+1); for(int i = 0;i <= m; i++) num[i] = 0; scanf("%d",&q); getchar(); for(int i = 0;i < q; i++){ //scanf("%d%d",&query[i].l,&query[i].r); query[i].l = getn(); query[i].r = getn(); query[i].id = i; } squ = sqrt(n*1.0); sort(query,query+q,comp); int LL=query[0].l,RR=query[0].l-1; ll res = 0; int x; for(int i = 0;i < q; i++){ int id = query[i].id; while(RR < query[i].r){ RR++; x = num[color[RR]]++; res -= po3[x]; x++; res += po3[x]; } while(RR > query[i].r){ x = num[color[RR]]--; res -= po3[x]; x--; res += po3[x]; RR--; } while(LL > query[i].l){ LL--; x = num[color[LL]]++; res -= po3[x]; x++; res += po3[x]; } while(LL < query[i].l){ x = num[color[LL]]--; res -= po3[x]; x--; res += po3[x]; LL++; } query[i].ans = res; } sort(query,query+q,compid); for(int i = 0;i < q; i++){ printf("%I64d\n",query[i].ans); } } return 0; }