Using the plane Manhattan distance minimum spanning tree or MO team algorithm can be acridine qwq~
But apparently the latter better write (escape ~)
Mo team how to write, look at the picture bar qwq~
So I didn't open a long and then took 3000 groups without making a mistake and handed it up to WA Qaq
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#defineint long Longusing namespacestd;Const intmx=50010;structNode {intL,r,num;} QUE[MX];BOOLCMP1 (Node A,node b) {returna.l<B.L;}BOOLCMP2 (Node A,node b) {returna.r<B.R;}intn,m,c[mx],num[mx],ans[mx],ans1[mx][2];inlineintGCD (intAintb) {inttmp while(a>0) tmp=b%a,b=a,a=tmp;returnb;} Signed Main () {scanf ("%lld%lld",&n,&m); for(intI=1; i<=n;i++) scanf ("%lld",&C[i]); for(intI=1; i<=m;i++) {scanf ("%lld%lld", &QUE[I].L,&QUE[I].R);if(que[i].l>que[i].r) Swap (QUE[I].L,QUE[I].R); } for(intI=1; i<=m;i++) que[i].num=i; Sort (que+1, que+1+M,CMP1); for(intI=1; I<=m;i+=sqrt (m)) sort (Que+i,que+min (m,i+ (int) sqrt (m)), CMP2); for(intI=1; i<=m;i++) { if(i% (int) sqrt (m) = =1|| i==1) {memset (num,0,sizeof(num)); for(intj=que[i].l;j<=que[i].r;j++) num[c[j]]++; for(intj=0; j<=n;j++) ans[i]+=num[j]* (num[j]-1)/2; } Else { for(intj=que[i-1].l,to=que[i].l;j!=to ;) { if(j<to) ans[i]-=num[c[j]]-1, Num[c[j]]--, j + +; Elseans[i]+=num[c[j-1]],num[c[j-1]]++,j--; } for(intj=que[i-1].r,to=que[i].r;j!=to ;) { if(j<to) ans[i]+=num[c[j+1]],num[c[j+1]]++,j++; Elseans[i]-=num[c[j]]-1, Num[c[j]]--, j--; } Ans[i]+=ans[i-1]; } intDIV=GCD (Ans[i], (que[i].r-que[i].l+1) * (QUE[I].R-QUE[I].L)/2); if(que[i].r==que[i].l| | ans[i]==0) ans1[que[i].num][0]=0, ans1[que[i].num][1]=1; Elseans1[que[i].num][0]=ans[i]/div,ans1[que[i].num][1]= (que[i].r-que[i].l+1) * (QUE[I].R-QUE[I].L)/(div*2); } for(intI=1; i<=m;i++) printf ("%lld/%lld\n", ans1[i][0],ans1[i][1]); return 0;}
bzoj2038 Little Z's socks