Same as BZOJ2038. Review it.
1#include <cstdio>2#include <cmath>3#include <algorithm>4 using namespacestd;5 intBlock;6 structquery{7 intI,l,r;8 BOOL operator< (ConstQuery &q)Const{9 if(L/block==q.l/block)returnr<Q.R;Ten returnl/block<q.l/Block; One } A}query[220000]; - intcnt[1111111]; - Long Longres,ans[220000]; the voidInsertLong Longx) { -res-=x*cnt[x]*Cnt[x]; -++Cnt[x]; -res+=x*cnt[x]*Cnt[x]; + } - voidRemoveLong Longx) { +res-=x*cnt[x]*Cnt[x]; A--Cnt[x]; atres+=x*cnt[x]*Cnt[x]; - } - inta[220000]; - intMain () { - intn,t; -scanf"%d%d",&n,&t); in for(intI=1; i<=n; ++i) scanf ("%d", A +i); -Block= (int) sqrt (n); to for(intI=0; i<t; ++i) { +Query[i].i=i; -scanf"%d%d",&query[i].l,&QUERY[I].R); the } *Sort (query,query+t); $ intL=1, r=1; cnt[a[1]]=1; res=a[1];Panax Notoginseng for(intI=0; i<t; ++i) { - while(L<QUERY[I].L) Remove (a[l++]); the while(L>QUERY[I].L) Insert (a[--l]); + while(R>QUERY[I].R) Remove (a[r--]); A while(R<QUERY[I].R) Insert (a[++R]); theans[query[i].i]=Res; + } - for(intI=0; i<t; ++i) { $printf"%i64d\n", Ans[i]); $ } - return 0; -}
Codeforces 86D Powerful Array (Mo team algorithm)