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 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.

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.

For each part, you should output the energy of that part.

8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5

128
72
2
1

#include<cstdio>#include<iostream>#include<fstream>#include<algorithm>#include<functional>#include<cstring>#include<string>#include<cstdlib>#include<iomanip>#include<numeric>#include<cctype>#include<cmath>#include<ctime>#include<queue>#include<stack>#include<list>#include<set>#include<map>using namespace std;const int maxn=1000010;typedef long long ll;int n,m,k;struct node{    int l,r,id;}Q[maxn]; ///保存询问值int pos[maxn];///保存所在块bool cmp(const node &a,const node &b){   return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&(pos[a.l]&1?a.r<b.r:a.r>b.r));///奇偶排序   //if(pos[a.l]==pos[b.l]) return a.r<b.r;//先按l所在的块排，如果相等就按r排    //else return pos[a.l]<pos[b.l];}ll num[maxn*2];///保存区间的个数///num要定义成ll，wa了好久ll a[maxn],b[maxn];ll ans[maxn],ans2[maxn];int L=0,R=0;ll Ans=0;void add(int x){   Ans-=num[a[x]]*num[a[x]]*num[a[x]];   num[a[x]]++;   Ans+=num[a[x]]*num[a[x]]*num[a[x]];}void del(int x){   Ans-=num[a[x]]*num[a[x]]*num[a[x]];   num[a[x]]--;   Ans+=num[a[x]]*num[a[x]]*num[a[x]];}int main(){        while(~scanf("%d",&n))    {   memset(num,0,sizeof(num));              //scanf("%d",&n);   //int sz=n/sqrt(m*2/3);   int sz=sqrt(n*1.0); ///不加1.0尽然编译错误       for(int i=1;i<=n;i++)    {        scanf("%lld",&a[i]);        b[i]=a[i];        pos[i]=i/sz;    }    sort(b+1,b+n+1);    int num1=unique(b+1,b+n+1)-b-1;    for(int i=1;i<=n;i++)       {         a[i]=lower_bound(b+1,b+num1+1,a[i])-b;          }        scanf("%d",&m);    for(int i=1;i<=m;i++)    {        scanf("%d%d",&Q[i].l,&Q[i].r);        Q[i].id=i;    }    sort(Q+1,Q+1+m,cmp);   num=0;    L=1;R=0;Ans=0;    for(int i=1;i<=m;i++)     {        while(R<Q[i].r)        {            R++;            add(R);        }        while(L>Q[i].l) ///前缀和L+1>Q[i].l        {            L--;            add(L);        }        while(L<Q[i].l)///前缀和L+1<Q[i].l        {            del(L);            L++;        }        while(R>Q[i].r)        {            del(R);            R--;        }        ans[Q[i].id]=Ans;    }   // printf("Case %d:\n",cas);     for(int i=1;i<=m;i++)        printf("%I64d\n",ans[i]);    }    return 0;}