Because the 10^9 is very large, so first discretization, to give you the number of hash time complexity O (NLOGN)
Then is the sub-team known [L,r], due to prior discretization, can be at O (1) time Update [l+1,r],[l,r+1],[l-1,r],[l,r-1] time complexity O (n*sqrt (n));
The code below, the speed is not very fast (I prefer manual to heavy, unique has not been how to use)
/*96655 ' s source code for bmemory:3744 KB time:2968 mslanguage:g++ result:accepted*/#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<iostream>#include<cstdlib>#include<queue>#include<map>#include<Set>#include<cmath>using namespaceStd;typedefLong LongLL;Const intmaxn=100005; Map<int,int>MP;intA[MAXN],POS[MAXN],B[MAXN]; LL sum;structnode{intL,r,id; LL ans;} RES[MAXN];BOOLCMP1 (node A,node b) {if(POS[A.L]==POS[B.L])returna.r<B.R; returna.l<B.L;}BOOLCMP2 (node A,node b) {returna.id<b.id;}voidChangeintPosintop) {LL temp=B[a[pos]]; Sum-=temp*temp*temp; B[a[pos]]+=op; Temp=B[a[pos]]; Sum+=temp*temp*temp;}intMain () {intn,q; while(~SCANF ("%d",&N)) {Doublecn=N; intBlk= (int) (sqrt (CN)); Sum=0, Mp.clear (); for(intI=1; i<=n; ++i) scanf ("%d", &a[i]), pos[i]= (i-1)/blk+1, b[i]=A[i]; Sort (b+1, b+n+1); intCnt=2; for(intI=2; i<=n; ++i)if(b[i]!=b[i-1]) b[cnt++]=B[i]; --CNT; for(intI=1; i<=n; ++i) a[i]=lower_bound (b +1, B +1+cnt,a[i])-(b +1); memset (b,0,sizeof(b)); scanf ("%d",&q); for(intI=1; i<=q; ++i) scanf ("%d%d", &RES[I].L,&RES[I].R), res[i].id=i; Sort (Res+1, res+1+Q,CMP1); for(intI=1, l=1, r=0; i<=q; ++i) { for(; r<res[i].r; + +r) Change (R+1,1); for(; r>res[i].r;--r) Change (R,-1); for(; l<res[i].l; + +l) Change (L,-1); for(; l>res[i].l;--l) Change (l-1,1); Res[i].ans=sum; } Sort (Res+1, res+q+1, CMP2); for(intI=1; i<=q; ++i) printf ("%i64d\n", Res[i].ans); } return 0;}
View Code
NBUT1457 Sona-Mo Team algorithm