Mo Team Example.
MO Team Study: https://www.cnblogs.com/Paul-Guderian/p/6933799.html
The subject molecule is Sigma (C (sum[a[i]],2)) and the denominator is Sigma (l-r+1,2); Maintain molecules and can.
MO Team Scope of application: offline, interval, transfer to the next block O (1).
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <cmath>7 using namespacestd;8 9 Const intn=50010;TentypedefLong LongLL; One intN,m,sq,a[n]; A LL Ans,gcd,sum[n]; - structnode{ - intL,r,k,id; the LL FZ,FM; - }; - node B[n]; - + intMaxxintXintY) {returnX>y?x:y;} - BOOLcmp1 (node X,node y) + { A if(X.K==Y.K)returnx.r<Y.R; at returnx.l<Y.L; - } - BOOLcmp2 (node X,node y) - { - returnx.id<y.id; - } in voidRevise (intXintd) - { toans-=sum[a[x]]* (sum[a[x]]-1)/2; +sum[a[x]]+=D; -ans+=sum[a[x]]* (sum[a[x]]-1)/2; the } * ll GETGCD (ll A,ll b) $ {Panax Notoginseng returnB?GETGCD (b,a%b): A; - } the + intMain () A { theFreopen ("a.in","R", stdin); +Freopen ("a.out","W", stdout); -scanf"%d%d", &n,&m); sq=sqrt (n); $ for(intI=1; i<=n;i++) scanf ("%d",&a[i]); $ for(intI=1; i<=m;i++) - { -scanf"%d%d",&b[i].l,&B[I].R); theb[i].k=b[i].l/sq; -B[i].id=i;Wuyi } theSort (b +1, b+m+1, CMP1); -memset (SUM,0,sizeof(sum)); Wu intL=1, r=1; sum[a[1]]=1; ans=0; - for(intI=1; i<=m;i++) About { $ while(L<B[I].L) {Revise (l,-1); l++;} - while(L>B[I].L) {Revise (L-1,1); l--;} - while(R<B[I].R) {Revise (r+1,1); r++;} - while(R>B[I].R) {Revise (r,-1); r--;} AB[i].fz=ans;b[i].fm= (LL) (b[i].r-b[i].l+1)) * ((LL) (B[I].R-B[I].L))/2; +Gcd=GETGCD (b[i].fz,b[i].fm); theB[i].fz/=gcd;b[i].fm/=gcd; - if(b[i].fz==0) b[i].fm=1; $ } theSort (b +1, b+m+1, CMP2); the for(intI=1; i<=m;i++) the { theprintf"%lld/%lld\n", b[i].fz,b[i].fm); - } in return 0; the}
"bzoj2038-Little Z socks" mo team algorithm