bzoj 2038: [2009国家集训队]小Z的袜子(经典莫队)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I_believe_CWJ/article/details/81544946

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

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 15871  Solved: 7197
[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

版权所有者:莫涛

[Submit][Status][Discuss]

解题思路:用莫队算法先求出区间选出相同颜色袜子的选法种数,然后再与分母求一次gcd,遇到分子为0的情况特判一下即可。某种颜色的袜子选出相同颜色的选法种类为Cx,2分母亦是如此。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<double, int> dpar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const LL INF = 3e9+7;
const uLL base = 131;
const int N = 1010;
const double pi = 3.1415926;

int n, m, sz;
int a[50010];
int L, R;
LL ans, b[50010], sum[50010], x[50010], y[50010];

struct node
{
    int l ,r, id;
    bool operator<(const node a)const {
        if(a.l/sz == l/sz) return r < a.r;
        return l < a.l;
    }
}e[50010];

LL cal(LL x) //种数
{
    return (x*(x-1))/2 > 0 ? (x*(x-1))/2 : 0;
}

void del(int x)
{
    ans -= cal(sum[x]) - cal(sum[x]-1);
    sum[x] --;
}

void add(int x)
{
    sum[x] ++;
    ans += cal(sum[x]) - cal(sum[x]-1);
}

LL gcd(LL a, LL b)
{
    if(a%b == 0) return b;
    return gcd(b, a%b);
}

int main()
{
    while(~scanf("%d%d", &n, &m)) {
        sz = ceil(sqrt(n*1.0));
        mem0(sum);
        for(int i = 1; i <= n; i ++) {
            scanf("%d", &a[i]);
        }
        for(int i = 0; i < m; i ++) {
            scanf("%d%d", &e[i].l, &e[i].r);
            e[i].id = i;
            x[i] = e[i].l, y[i] = e[i].r;
        }
        L = 1;
        R = 0;
        ans = 0;
        sort(e, e+m);
        for(int i = 0; i < m; i ++) {
            while(L < e[i].l) del(a[L ++]);
            while(L > e[i].l) add(a[-- L]);
            while(R < e[i].r) add(a[++ R]);
            while(R > e[i].r) del(a[R --]);
            b[e[i].id] = ans;
        }
        for(int i = 0; i < m; i ++) {
            if(b[i] == 0) printf("0/1\n");
            else {
                LL len = y[i]-x[i]+1;
                len = cal(len);
                LL k = gcd(b[i], len);
                printf("%lld/%lld\n", b[i]/k, len/k);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/I_believe_CWJ/article/details/81544946