MO Team algorithm + discretization
1.map will be tle, must be discretized to do
2.long Long will wa,__int64 define%I64D output output can AC
3. Note that the input sequence will explode int
#include <cstdio>#include<cstring>#include<cmath>#include<queue>#include<map>#include<algorithm>using namespacestd;Const intMAXN =100000+Ten;intN, M;__int64 TMP[MAXN], A[MAXN], LSH[MAXN];intCNT;intPos[maxn];__int64 C[maxn];__int64 Ans[maxn], ans;intL, R;structx{intL, R, id;} S[MAXN];BOOLcmpConstX&a,Constx&b) { if(POS[A.L] = = POS[B.L])returnA.R <B.R; returnA.L <B.L;}intf (__int64 x) {intL =1, r =CNT; while(L <=r) {intMid = (L + r)/2; if(Lsh[mid] < x) L = mid +1; Else if(lsh[mid]>x) R = mid-1; Else returnmid; }}voidLSH () {sort (tmp+1, TMP +1+N); CNT=0, lsh[++cnt] = tmp[1]; for(inti =2; I <= N; i++) { if(Tmp[i] = = Tmp[i-1])Continue; lsh[++CNT] =Tmp[i]; } for(inti =1; I <= N; i++) A[i] =(__int64) f (A[i]);}intMain () { while(~SCANF ("%d", &N) {memset (c,0,sizeofc); intSZ = sqrt (1.0*N); for(inti =1; I <= N; i++) {Pos[i]= I/sz; scanf ("%i64d", &Tmp[i]); A[i]=Tmp[i]; } LSH (); scanf ("%d", &m); for(inti =1; I <= m; i++) {scanf ("%d%d", &S[I].L, &S[I].R); S[i].id=i; } sort (S+1, S +1+m, CMP); Ans=0; for(inti = s[1].L; I <= s[1].R; i++) {Ans= Ans-c[a[i]] [c[a[i]] *C[a[i]]; C[a[i]]++; Ans= Ans + c[a[i]] * C[a[i]] *C[a[i]]; } ans[s[1].id] = Ans; L = s[1].L; R = s[1].R; for(inti =2; I <= m; i++) { while(L <S[I].L) {Ans= Ans-c[a[l]] [c[a[l]] *C[a[l]]; C[A[L]]--; Ans= Ans + c[a[l]] * C[a[l]] *C[a[l]]; L++; } while(L >S[I].L) {L--; Ans= Ans-c[a[l]] [c[a[l]] *C[a[l]]; C[A[L]]++; Ans= Ans + c[a[l]] * C[a[l]] *C[a[l]]; } while(R <S[I].R) {R++; Ans= Ans-c[a[r]] [c[a[r]] *C[a[r]]; C[A[R]]++; Ans= Ans + c[a[r]] * C[a[r]] *C[a[r]]; } while(R >S[I].R) {Ans= Ans-c[a[r]] [c[a[r]] *C[a[r]]; C[A[R]]--; Ans= Ans + c[a[r]] * C[a[r]] *C[a[r]]; R--; } Ans[s[i].id]=Ans; } for(inti =1; I <= m; i++) printf ("%i64d\n", Ans[i]); } return 0;}
Nbut 1457 Sona