2038: [2009国家集训队]小Z的袜子(hose)
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1591 Solved: 764
[ Submit][ Status]
Description
作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L
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
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。
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
这个题的正解是莫队算法,也可以分块搞,学习了一下分块法,莫队算法稍后补上。
分块法,以根号n为单位分块,然后离线读入查询,排序,两个指针扫描,
/* ***********************************************
Author :rabbit
Created Time :2014/3/12 16:40:57
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const ll maxn=50020;
ll S[maxn],col[maxn],pos[maxn];
ll gcd(ll a,ll b){
if(a==0)return b;
return gcd(b%a,a);
}
struct Query{
ll l,r,a,b,id;
}pp[maxn];
bool cmp1(Query a,Query b){
return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&a.r<b.r);
}
bool cmp2(Query a,Query b){
return a.id<b.id;
}
void update(ll pos,ll &ans,ll add){
ans=ans-S[col[pos]]*(S[col[pos]]-1);
S[col[pos]]+=add;
ans=ans+S[col[pos]]*(S[col[pos]]-1);
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ll n,m;
while(~scanf("%lld%lld",&n,&m)){
for(ll i=1;i<=n;i++)scanf("%lld",&col[i]);
memset(S,0,sizeof(S));
for(ll i=1;i<=m;i++)scanf("%lld%lld",&pp[i].l,&pp[i].r),pp[i].id=i;
ll limit=(ll)sqrt(n+0.5);
for(ll j=1;j<=n;j++)pos[j]=(j-1)/limit+1;
sort(pp+1,pp+m+1,cmp1);ll ans=0;
for(ll i=1,l=1,r=0;i<=m;i++){
if(r<pp[i].r){
for(r=r+1;r<=pp[i].r;r++)update(r,ans,1);r--;
}
if(r>pp[i].r)for(;r>pp[i].r;r--)update(r,ans,-1);
if(l<pp[i].l)for(;l<pp[i].l;l++)update(l,ans,-1);
if(l>pp[i].l){
for(l=l-1;l>=pp[i].l;l--)update(l,ans,1);l++;
}
if(pp[i].l==pp[i].r){
pp[i].a=0;pp[i].b=1;continue;
}
pp[i].a=ans;pp[i].b=(pp[i].r-pp[i].l+1)*(pp[i].r-pp[i].l);
ll k=gcd(pp[i].a,pp[i].b);pp[i].a/=k;pp[i].b/=k;
}
sort(pp+1,pp+m+1,cmp2);
for(int i=1;i<=m;i++)printf("%lld/%lld\n",pp[i].a,pp[i].b);
}
return 0;
}