题目:
http://www.lydsy.com/JudgeOnline/problem.php?id=2038
题解:
开LongLong!!!!
按照莫队的方法把询问拍个序,然后搞cnt数组统计每个颜色出现次数,用cur统计当前方案
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #define N 50010 typedef unsigned long long ll; using namespace std; ll n,m,c[N],S,L,R; ll ans1[N],ans2[N],cnt[N],cur; struct node { ll l,r,bl,id; bool operator < (const node &x)const { return bl<x.bl || bl==x.bl && r<x.r; } }q[N]; ll gcd(ll x,ll y) { return y?gcd(y,x%y):x; } int main() { scanf("%lld%lld",&n,&m);S=sqrt(n),cnt[0]=1; for (ll i=1;i<=n;i++) scanf("%lld",c+i); for (ll i=1,l,r;i<=m;i++) { scanf("%lld%lld",&q[i].l,&q[i].r); q[i].id=i;q[i].bl=(q[i].l-1)/S+1; } sort(q+1,q+1+m); for (ll i=1;i<=m;i++) { ll tl=q[i].l,tr=q[i].r; while (L<tl) cnt[c[L]]--,cur-=cnt[c[L]],L++; while (L>tl) L--,cur+=cnt[c[L]],cnt[c[L]]++; while (tr>R) R++,cur+=cnt[c[R]],cnt[c[R]]++; while (R>tr) cnt[c[R]]--,cur-=cnt[c[R]],R--; ans1[q[i].id]=cur; ans2[q[i].id]=(tr-tl)*(tr-tl+1LL)/2; } for (int i=1;i<=m;i++) { ll G=gcd(ans1[i],ans2[i]); printf("%lld/%lld\n",ans1[i]/G,ans2[i]/G); } return 0; }