BZOJ2038: [2009国家集训队]小Z的袜子(hose) -- 莫队算法 ,,分块

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

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 3577  Solved: 1652
[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。
 
对于L , R区间, 如果 A B,C,...Z颜色袜子的个数如果为a, b, c, ... z那么 ans = (A*(A-1)/2 + B*(B-1)/+....)/ ((R-L+1)*(R-L)/2;
整理之后分子就是(A*A+B*B+...+Z*Z - (R-L+1))/2
所有从[L, R] 到[L+x, L+y]区间的复杂度为O(x+y), 如此可以用莫队算法, 但是 貌似求曼哈段最小生成那种做法比较繁琐,,简单的方法就是 分块,分成sqrt (n)块, 然后根据每一个询问左端点所在的块 排序,如果在同一个块内,则按右端点排序。
  1 #include <set>
  2 #include <map>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 typedef unsigned long long ull;
 16 typedef long long ll;
 17 const int inf = 0x3f3f3f3f;
 18 const double eps = 1e-8;
 19 const int MAXN = 5e4+10;
 20 int block;
 21 int a[MAXN], pos[MAXN];
 22 struct Query
 23 {
 24     int L, R, idx;
 25     bool operator < (const Query &rhs)const
 26     {
 27         if (pos[L] == pos[rhs.L])
 28             return R < rhs.R;
 29         else
 30             return L < rhs.L;
 31         //return (pos[L] == pos[rhs.L] && R < rhs.R) || L < rhs.L;
 32     }
 33 
 34 } q[MAXN];
 35 int n, m;
 36 ll s[MAXN], ans1[MAXN], ans2[MAXN];
 37 ll sqr (ll x)
 38 {
 39     return  x * x;
 40 }
 41 ll ans;
 42 void update (int x, int d)
 43 {
 44     ans -= sqr(s[a[x]]);
 45     s[a[x]] += d;
 46     ans += sqr(s[a[x]]);
 47 }
 48 ll GCD (ll x, ll y)
 49 {
 50     return y == 0 ? x : GCD(y, x % y);
 51 }
 52 int main()
 53 {
 54 #ifndef ONLINE_JUDGE
 55     freopen("in.txt","r",stdin);
 56 #endif
 57     while (~ scanf ("%d%d", &n, &m))
 58     {
 59         memset(s, 0, sizeof (s));
 60         for (int i = 1; i <= n; i++)
 61         {
 62             scanf ("%d", a+i);
 63         }
 64         block = (int) sqrt(n);
 65         for (int i = 1; i <= n; i++)
 66         {
 67             pos[i] = (i-1)/block + 1;
 68         }
 69         for (int i = 0; i < m; i++)
 70         {
 71             scanf ("%d%d", &q[i].L, &q[i].R);
 72             q[i].idx = i;
 73         }
 74         sort (q, q+m);
 75         int l = 1, r = 0;
 76         ans = 0;
 77         for (int i = 0; i < m; i++)
 78         {
 79             while (r < q[i].R)
 80             {
 81                 update(r+1, 1);
 82                 r++;
 83             }
 84             while (r > q[i].R)
 85             {
 86                 update(r, -1);
 87                 r--;
 88             }
 89             while (l < q[i].L)
 90             {
 91                 update(l, -1);
 92                 l++;
 93             }
 94             while (l > q[i].L)
 95             {
 96                 update(l-1, 1);
 97                 l--;
 98             }
 99             ans1[q[i].idx] = ans - (q[i].R - q[i].L + 1);
100             ans2[q[i].idx] = (ll)(q[i].R - q[i].L + 1)*(q[i].R - q[i].L);
101         }
102         for (int i = 0; i < m; i++)
103         {
104             ll tmp = GCD(ans1[i], ans2[i]);
105             ans1[i] /= tmp;
106             ans2[i] /= tmp;
107             printf("%lld/%lld
", ans1[i], ans2[i]);
108         }
109     }
110     return 0;
111 }
原文地址:https://www.cnblogs.com/oneshot/p/4472744.html