P1494 [国家集训队]小Z的袜子
P1494 [国家集训队]小Z的袜子
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<set> #include<map> #include<stack> #include<cstring> #pragma GCC optimize(2) #define inf 2147483647 #define ls rt<<1 #define rs rt<<1|1 #define lson ls,nl,mid,l,r #define rson rs,mid+1,nr,l,r #define N 1000010 #define For(i,a,b) for(register long long i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() using namespace std; long long a[N],cnt[N],b[N],l,r,ql,qr,t,k,w; long long n,m,size,num,now,ans1[N],ans2[N]; struct query{ long long l,r,id; }q[N]; inline bool cmp(query x,query y){ return (b[x.l]^b[y.l])?b[x.l]<b[y.l]:(b[x.l]&1)?x.r<y.r:x.r>y.r; } void in(long long &x){ long long y=1; char c=g();x=0; while(c<'0'||c>'9'){ if(c=='-')y=-1; c=g(); } while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g(); } x*=y; } void o(long long x){ if(x<0){ p('-'); x=-x; } if(x>9)o(x/10); p(x%10+'0'); } void update(long long x,long long add){ now-=cnt[a[x]]*cnt[a[x]]; cnt[a[x]]+=add; now+=cnt[a[x]]*cnt[a[x]]; } long long gcd(long long a,long long b){ return (a%b==0?b:gcd(b,a%b)); } int main(){ in(n);in(m); size=sqrt(n); num=ceil((double)n/size); For(i,1,num) For(j,(i-1)*size+1,i*size) b[j]=i; For(i,1,n) in(a[i]); For(i,1,m){ in(q[i].l);in(q[i].r); q[i].id=i; } sort(q+1,q+m+1,cmp); l=1;r=0; For(i,1,m){ ql=q[i].l;qr=q[i].r; while(l<ql) update(l++,-1); while(l>ql) update(--l,1); while(r<qr) update(++r,1); while(r>qr) update(r--,-1); w=now-(qr-ql+1); k=(qr-ql+1)*(qr-ql); t=gcd(w,k); w/=t; k/=t; ans1[q[i].id]=w; ans2[q[i].id]=k; } For(i,1,m){ o(ans1[i]);p('/');o(ans2[i]);p('\n'); } return 0; }