1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cmath>5#include <algorithm>6 #definell Long Long7 #defineM 500098 using namespacestd;9 structDataTen { One ll L,r,a,b,id; A }da[m]; - ll S,n,m,c[m],block,ku,pos[m],su[m]; - BOOLCMP (data a1,data A2) the { - if(pos[a1.l]==POS[A2.L]) - returna1.r<A2.R; - returnpos[a1.l]<POS[A2.L]; + } - BOOLCMP1 (Data a1,data A2) + { A returna1.id<a2.id; at } - ll Sqr (ll A1) - { - returnA1*A1; - } - voidUpintA1,intA2) in { - if(A1) to { +s-=Sqr (su[c[a1]); -su[c[a1]]+=A2; thes+=Sqr (su[c[a1]); * } $ return;Panax Notoginseng } - ll GCD (ll a1,ll A2) the { +ll a3=a1%A2; A for(; A3;) the { +a1=A2; -A2=A3; $a3=a1%A2; $ } - returnA2; - } the intMain () - {Wuyiscanf"%lld%lld",&n,&m); the for(intI=1; i<=n;i++) -scanf"%lld",&c[i]); Wu for(intI=1; i<=m;i++) - { Aboutscanf"%lld%lld",&da[i].l,&DA[I].R); $Da[i].id=i; - } -Block= (int) sqrt (n); -ku=n/Block; A if(n%block) +ku++; the for(intI=1; i<=n;i++) -pos[i]= (I-1)/block+1; $Sort (da+1, da+m+1, CMP); the intH=0, t=0; the for(intI=1; i<=m;i++) the { the for(; t<da[i].r;t++) -Up (t+1,1); in for(; t>da[i].r;t--) theUp (t,-1); the for(; h<da[i].l;h++) AboutUp (h,-1); the for(; h>da[i].l;h--) theUp (H-1,1); the if(da[i].l==DA[I].R) + { -Da[i].a=0; theda[i].b=1;Bayi Continue; the } thell len=da[i].r-da[i].l+1; -da[i].a=s-Len; -da[i].b=len* (len-1); thell k=gcd (da[i].a,da[i].b); theDa[i].a/=K; theDa[i].b/=K; the } -Sort (da+1, da+m+1, CMP1); the for(intI=1; i<=m;i++) theprintf"%lld/%lld\n", da[i].a,da[i].b); the return 0;94}
Mo to the algorithm classic example, by the sub-block sort after violence.
Bzoj 2038: [2009 countries Training team] small Z socks (hose)