Topic: Given a sequence, multiple times to ask the sum of the number of occurrences of all numbers in an interval
MO Team algorithm does not explain
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define M 50500using namespace std;struct query{int l,r,pos; BOOL operator < (const query &y) const;} Q[m];int n,m,k,block,a[m];int l=1,r=0,cnt[m];unsigned int now,ans[m];bool query:: operator < (const query &y) cons t{if ((l>>8)! = (y.l>>8)) return L < Y.L; return r < Y.R;} inline void Update (int x,int y) {now-=cnt[x]*cnt[x]; Cnt[x]+=y; NOW+=CNT[X]*CNT[X];} int main () {int i; cin>>n>>m>>k; for (i=1;i<=n;i++) scanf ("%d", &a[i]); Block=static_cast<int> (sqrt (n) +1e-7); for (i=1;i<=m;i++) scanf ("%d%d", &Q[I].L,&Q[I].R), q[i].pos=i; Sort (q+1,q+m+1); for (i=1;i<=m;i++) {while (R<Q[I].R) Update (a[++r],1); while (L>Q[I].L) Update (a[--l],1); while (R>Q[I].R) Update (a[r--],-1); while (L<Q[I].L) Update (a[l++],-1); Ans[q[i].pos]=now; } for (i=1;i<=m;i++) printf ("%u\n", Ans[i]);}
Bzoj 3781 Small b query Mo team algorithm