國家集訓隊 小Z的襪子
阿新 • • 發佈:2018-08-20
show org 什麽 new long sort ron n) www.
傳送門
自從“HH的項鏈”被樹狀數組幹爆了之後,莫隊終於揚眉吐氣了一把。
很經典的莫隊模板題,好像沒什麽好說的……
代碼有(十)些(分)冗長,將就著看吧……
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define LL long long using namespace std; struct zzz{ LL l,r,pos; }que[50010]; int n,m,sq; bool cmp(zzz x,zzz y){ if(x.l/sq != y.l/sq) return x.l/sq < y.l/sq; else return x.r < y.r; } LL read(){ LL k=0; char c=getchar(); for(;c<'0'||c>'9';) c=getchar(); for(;c>='0'&&c<='9';c=getchar()) k=(k<<3)+(k<<1)+c-48; return k; } LL a[50010],tong[50010]; struct hhh{ LL fm,fz; }anss[50010]; LL gcd(LL x,LL y){ return x%y==0? y:gcd(y,x%y); } int main(){ n=read(),m=read(); sq=sqrt(n); for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=m;i++) que[i].l=read(),que[i].r=read(), que[i].pos=i; sort(que+1,que+m+1,cmp); LL l=1,r=0,ans=0; for(int i=1;i<=m;i++){ while(l<que[i].l){ if(tong[a[l]]>0) ans-=tong[a[l]]*tong[a[l]]; tong[a[l++]]--; if(tong[a[l-1]]>0) ans+=tong[a[l-1]]*tong[a[l-1]]; } while(l>que[i].l){ if(tong[a[l-1]]>0) ans-=tong[a[l-1]]*tong[a[l-1]]; tong[a[--l]]++; if(tong[a[l]]>0) ans+=tong[a[l]]*tong[a[l]]; } while(r<que[i].r){ if(tong[a[r+1]]>0) ans-=tong[a[r+1]]*tong[a[r+1]]; tong[a[++r]]++; if(tong[a[r]]>0) ans+=tong[a[r]]*tong[a[r]]; } while(r>que[i].r){ if(tong[a[r]]>0) ans-=tong[a[r]]*tong[a[r]]; tong[a[r--]]--; if(tong[a[r-1]]>0) ans+=tong[a[r+1]]*tong[a[r+1]]; } if(l==r||ans==r-l+1){ anss[que[i].pos].fz=0; anss[que[i].pos].fm=1; continue; } LL g=gcd(ans-(r-l+1),(r-l+1)*(r-l)); anss[que[i].pos].fz=(ans-(r-l+1))/g; anss[que[i].pos].fm=((r-l+1)*(r-l))/g; } for(int i=1;i<=m;i++) printf("%lld/%lld\n",anss[i].fz,anss[i].fm); return 0; }
國家集訓隊 小Z的襪子