1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 #defineMAXN 500058typedefLong Longll;9ll ans=0, ANS[MAXN],BNS[MAXN];Ten intn,m,sum[maxn],a[maxn],block[maxn],temp; One structtsegment{ A intId,l,r; - }QS[MAXN]; - voidprepare () { thetemp=sqrt (n); - for(intI=1; i<=n;i++) block[i]= (i-1)/temp+1; - } - BOOLComp (tsegment x,tsegment y) { + if(block[x.l]==BLOCK[Y.L]) { - if(block[x.l]%2==1)returnx.r<Y.R; + Else returnX.r>Y.R; A } at returnblock[x.l]<BLOCK[Y.L]; - } - voidChangeintZinty) { - intx=A[z]; -ans-=1ll*sum[x]* (sum[x]-1); -sum[x]+=y; inans+=1ll*sum[x]* (sum[x]-1); - } to ll gcd (ll X,ll y) { + if(y==0)returnx; - returnGCD (y,x%y); the } * voidWork () { $ans=0;Panax Notoginseng for(intL=1, r=0, i=1; i<=m;i++){ - for(; qs[i].l<l;l--) Change (L-1,1); the for(; qs[i].l>l;l++) Change (l,-1); + for(; qs[i].r>r;r++) Change (r+1,1); A for(; qs[i].r<r;r--) Change (r,-1); theans[qs[i].id]=ans; + } - for(intI=1; i<=m;i++){ $ if(ans[i]!=0){ $ll x=gcd (Ans[i],bns[i]); -printf"%lld/%lld\n", ans[i]/x,bns[i]/x); -}Else{ theprintf"0/1\n"); - }Wuyi } the } - intMain () { Wuscanf"%d%d",&n,&m); - for(intI=1; i<=n;i++) scanf ("%d",&a[i]); About for(intI=1; i<=m;i++) scanf ("%d%d", &QS[I].L,&QS[I].R), qs[i].id=i; $ for(intI=1; i<=m;i++) bns[i]=1ll* (qs[i].r-qs[i].l+1) * (qs[i].r-qs[i].l); - prepare (); -Sort (qs+1, qs+m+1, comp); - Work (); A return 0; +}
View Code
Title Link: http://www.lydsy.com/JudgeOnline/problemset.php
The main topic: see the problem surface.
Procedure: Mo team algorithm.
bzoj2038: [2009 countries Training team] small Z socks (hose)