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

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。

Solution

莫队裸题。用计数排序的辅助数组维护答案。有关莫队的讲解戳这里

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned LL;
const int N = 50005;
int getint() {
    int r = 0; char c = getchar();
    for (; '0' > c || c > '9'; c = getchar());
    for(;'0'<=c&&c<='9';c=getchar())r=r*10-'0'+c;
    return r;
}
int n, m;
const int size = 224;
struct query_type {
    int l, r, id;
    LL ans;
} query[N];
int A[N];
bool cmpl(query_type x, query_type y) {
    if (x.l / size == y.l / size) return x.r < y.r;
    return x.l / size < y.l / size;
}
bool cmpid(query_type x, query_type y) {
    return x.id < y.id;
}
LL ans = 0;
LL v[N];
void add(int x) {
    ans -= v[A[x]]*(v[A[x]]-1);
    ++v[A[x]];
    ans += v[A[x]]*(v[A[x]]-1);
}
void del(int x) {
    ans -= v[A[x]]*(v[A[x]]-1);
    --v[A[x]];
    ans += v[A[x]]*(v[A[x]]-1);
}
LL gcd(LL a, LL b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
int main() {
    n = getint(); m = getint();
    for (int i = 1; i <= n; ++i) A[i] = getint();
    int x, y;
    for (int i = 1; i <= m; ++i) {
        x = getint(); y = getint();
        query[i].l = x;
        query[i].r = y;
        query[i].id = i;
    }
    sort(query+1, query+m+1, cmpl);
    int nowl = 1, nowr = 1;
    v[A[1]] = 1;
    for (int i = 1; i <= m; ++i) {
        while (nowl < query[i].l) {
            del(nowl);
            ++nowl;
        }
        while (nowl > query[i].l) {
            --nowl;
            add(nowl);
        }
         
        while (nowr < query[i].r) {
            ++nowr;
            add(nowr);
        }
        while (nowr > query[i].r) {
            del(nowr);
            --nowr;
        }
        query[i].ans = ans;
    }
    sort(query+1, query+m+1, cmpid);
    for (int i = 1; i <= m; ++i) {
        if (!query[i].ans) {
            puts("0/1");
            continue;
        }
        LL len = query[i].r - query[i].l + 1;
        len = len*(len-1);
        LL g = gcd(query[i].ans , len);
        printf("%u/%u\n", query[i].ans/g, len / g);
    }
}