题意:
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。
解析:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36887
代码:
#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson lo,mi,rt<<1
#define rson mi+1,hi,rt<<1|1
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define dec(i,a,b) for(int i=(a); i>=(b); i--)
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 50000 + 10;
const double pi = acos(-1.0);
int readT()
{
char c;
int ret = 0,flg = 0;
while(c = getchar(), (c < '0' || c > '9') && c != '-');
if(c == '-') flg = 1; else ret = c ^ 48;
while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
return flg ? - ret : ret;
}
LL readTL()
{
char c;
int flg = 0;
LL ret = 0;
while(c = getchar(), (c < '0' || c > '9') && c != '-');
if(c == '-') flg = 1; else ret = c ^ 48;
while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
return flg ? - ret : ret;
}
LL num[maxn]; //当前询问的区间中颜色i的数量
LL up[maxn], dw[maxn]; //答案的分子,分母
LL ans;
int color[maxn]; //i位置的颜色
int pos[maxn]; //莫队算法中分块排序的键值
struct Query
{
int l, r, id;
} query[maxn]; //离线的询问
bool cmp(Query a, Query b)
{
if (pos[a.l] == pos[b.l])
return a.r < b.r;
return pos[a.l] < pos[b.l];
}
LL gcd(LL a, LL b)
{
return b == 0 ? a : gcd(b, a % b);
}
void update(int x, int d)
{
ans -= num[color[x]] * num[color[x]];
num[color[x]] += d;
ans += num[color[x]] * num[color[x]];
}
int main()
{
#ifdef LOCAL
FIN;
#endif // LOCAL
int n, m;
while (~scanf("%d%d", &n, &m))
{
mem(num, 0);
int block = ceil(sqrt(1.0 * n)); //分块
for (int i = 1; i <= n; i++)
{
scanf("%d", &color[i]);
pos[i] = (i - 1) / block;
}
for (int i = 0; i < m; i++)
{
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
sort(query, query + m, cmp);
int nowl = 1, nowr = 0; //记录当前所包含的块区间
ans = 0;
for (int i = 0; i < m; i++)
{
int id = query[i].id;
if (query[i].l == query[i].r)
{
up[id] = 0, dw[id] = 1;
continue;
}
if (nowr < query[i].r)
{
for (int j = nowr + 1; j <= query[i].r; j++)
{
update(j, 1);
}
}
else
{
for (int j = nowr; j > query[i].r; j--)
{
update(j, -1);
}
}
nowr = query[i].r;
if (nowl < query[i].l)
{
for (int j = nowl; j < query[i].l; j++)
{
update(j, -1);
}
}
else
{
for (int j = nowl - 1; j >= query[i].l; j--)
{
update(j, 1);
}
}
nowl = query[i].l;
LL a = ans - query[i].r + query[i].l - 1;
LL b = (LL)(query[i].r - query[i].l + 1) * (query[i].r - query[i].l);
LL c = gcd(a, b);
a /= c, b /= c;
up[id] = a, dw[id] = b;
}
for (int i = 0; i < m; i++)
{
printf("%lld/%lld\n", up[i], dw[i]);
}
}
return 0;
}