Test instructions: Take an interval, the probability of taking two numbers in interval;
Idea: The probability p= (A * (A-1)/2+b* (B-1)/2+ ...) /(r-l+1) * (r-l)/2 simplified p= (a*a+b*b+......+z*z-(r-l+1))/(r-l+1) * (R-L);
The left endpoint of the inquiry interval is disposed in the same block, and every query in one block is processed, and for the same piece, the right endpoint is pressed for strict increment processing, and the left endpoint is constantly moving;
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;intT,n,m,unit;inta[500010],num[500010];structnode{intId,l,r;} m[500010];intCMP (node A,node b) {if(A.l/unit!=b.l/unit)returna.l/unit<b.l/Unit; returna.r<B.R;}Long LonggcdLong LongALong Longb) { if(b==0)returnA; returnGCD (b,a%b);}structque{Long Longb; voidReduce ()//score Numerator { Long LongD=gcd (A, b); A/=d;b/=D; }}ans[500010];voidWork () {intL=1, r=0; Long Longtemp=0; memset (num,0,sizeof(num));//the number of the same number in the interval for(intI=0; i<m;i++) { while(r<M[i]. R) {R++; Temp-=(Long Long) num[a[r]]*Num[a[r]]; NUM[A[R]]++;//A[r] The number plus 1temp+= (Long Long) num[a[r]]*Num[a[r]]; } while(l>M[i]. L) {L--; Temp-=(Long Long) num[a[l]]*Num[a[l]]; NUM[A[L]]++;//A[l] The number plus 1temp+= (Long Long) num[a[l]]*Num[a[l]]; } while(r>M[i]. R) {Temp-=(Long Long) num[a[r]]*Num[a[r]]; NUM[A[R]]--;//A[l] The number minus 1temp+= (Long Long) num[a[r]]*Num[a[r]]; R--; } while(l<M[i]. L) {Temp-=(Long Long) num[a[l]]*Num[a[l]]; NUM[A[L]]--;//A[r] The number minus 1temp+= (Long Long) num[a[l]]*Num[a[l]]; L++; } ans[m[i].id].a=temp-(r-l+1); ANS[M[I].ID].B=(Long Long) (r-l) * (r-l+1); Ans[m[i].id].reduce (); }}intMain () {inti,j,k; while(SCANF ("%d%d", &n,&m)! =EOF) { for(i=1; i<=n;i++) scanf ("%d",&A[i]); Unit=(int) sqrt (n);//The total interval is divided into unit blocks for(i=0; i<m;i++) {m[i].id=i; scanf ("%d%d", &m[i]. l,&M[i]. R); } sort (M,m+M,CMP);//sort the inquiryWork (); for(i=0; i<m;i++) printf ("%lld/%lld\n", ans[i].a,ans[i].b); } return 0;}
HYSBZ 2038 small Z Socks (hose) (Mo Team Algorithm primer)