BZOJ2038小Z的袜子(莫队算法)

2038: [2009国家集训队]小Z的袜子(hose)

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 15717  Solved: 7133
[Submit][Status][Discuss]

Description

作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。

Input

输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。

Output

包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)

Sample Input

6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6

Sample Output

2/5
0/1
1/1
4/15
【样例解释】
询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
【数据规模和约定】
30%的数据中 N,M ≤ 5000;
60%的数据中 N,M ≤ 25000;
100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。

HINT

Source

版权所有者:莫涛

莫队内部的四个while要先拓展再收缩,否则有些题目会WA掉(貌似这题不是)

其中求分子分母经过了公式化简,所以分母不用除以2了,分子变为sigma(a*a)-sigma(a)

其中a正是R-L+1

写了一上午TvT,代码十分钟,debug两小时

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
long long col[50005],cot[50005];
long long last[50005][2];
struct forma
{
    long long l,r,num;
}stu[50005];
long long k;
bool cmp(forma a,forma b){
    if(a.l/k==b.l/k)return a.r<b.r;
    return a.l<b.l;
}
void init(){
    memset(col,0,sizeof(col));
    memset(cot,0,sizeof(cot));
}
long long gcd(long long a,long long b){
    return b?gcd(b,a%b):a;
}
int main()
{
    int num,tnum;
    scanf("%d%d",&num,&tnum);
        init();
        for(int i=1;i<=num;i++)
            scanf("%lld",&col[i]);
        for(int i=1;i<=tnum;i++){
            scanf("%lld%lld",&stu[i].l,&stu[i].r);
            stu[i].num=i;
        }
        k=sqrt(num);
        sort(stu+1,stu+1+tnum,cmp);
        int L=1,R=0;
        long long ans=0;
        for(int i=1;i<=tnum;i++){
            while(R<stu[i].r){
                ans+=((cot[col[R+1]]+1)*(cot[col[R+1]]+1)-cot[col[R+1]]*cot[col[R+1]]);
                cot[col[R+1]]++;
                R++;
            }
            while(L>stu[i].l){
                ans+=((cot[col[L-1]]+1)*(cot[col[L-1]]+1)-cot[col[L-1]]*cot[col[L-1]]);
                cot[col[L-1]]++;
                L--;
            }
            while(R>stu[i].r){
                ans-=(cot[col[R]]*cot[col[R]]-(cot[col[R]]-1)*(cot[col[R]]-1));
                cot[col[R]]--;
                R--;
            }
            while(L<stu[i].l){
                ans-=(cot[col[L]]*cot[col[L]]-(cot[col[L]]-1)*(cot[col[L]]-1));
                cot[col[L]]--;
                L++;
            }
            last[stu[i].num][1]=ans-R+L-1;  ///作为分子
            last[stu[i].num][0]=(long long)(R-L+1)*(R-L); ///不是longlong会返回WA
        }
        for(int i=1;i<=tnum;i++){
            long long GCD=gcd(last[i][1],last[i][0]);
            last[i][1]/=GCD;
            last[i][0]/=GCD;
            if(last[i][1]==0)
                last[i][0]=1;
        }
        for(int i=1;i<=tnum;i++){
            printf("%lld/%lld\n",last[i][1],last[i][0]);
        }
}

猜你喜欢

转载自blog.csdn.net/qq_41548233/article/details/81408180