題目鏈接:
http://www.lydsy.com/JudgeOnline/problem.php?id=2038
專題練習:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29469#overview
2038: [2009國家集訓隊]小Z的襪子(hose)
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 966 Solved: 472
[ 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
莫隊算法可以解決一類不修改、離線查詢問題。
構造曼哈頓最小生成樹的做法還沒有寫。
寫了個直接分段解決的辦法。
把1~n分成sqrt(n)段。
unit = sqrt(n)
m個查詢先按照第幾個塊排序,再按照 R排序。
然后直接求解。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013/8/16 19:07:51 4 File Name :F:\2013ACM練習\專題學習\莫隊算法\小Z的襪子.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 21 const int MAXN = 50010; 22 const int MAXM = 50010; 23 struct Query 24 { 25 int L,R,id; 26 }node[MAXM]; 27 long long gcd(long long a,long long b) 28 { 29 if(b == 0)return a; 30 return gcd(b,a%b); 31 } 32 struct Ans 33 { 34 long long a,b;//分數a/b 35 void reduce()//分數化簡 36 { 37 long long d = gcd(a,b); 38 a /= d; b /= d; 39 } 40 }ans[MAXM]; 41 int a[MAXN]; 42 int num[MAXN]; 43 int n,m,unit; 44 bool cmp(Query a,Query b) 45 { 46 if(a.L/unit != b.L/unit)return a.L/unit < b.L/unit; 47 else return a.R < b.R; 48 } 49 void work() 50 { 51 long long temp = 0; 52 memset(num,0,sizeof(num)); 53 int L = 1; 54 int R = 0; 55 for(int i = 0;i < m;i++) 56 { 57 while(R < node[i].R) 58 { 59 R++; 60 temp -= (long long)num[a[R]]*num[a[R]]; 61 num[a[R]]++; 62 temp += (long long)num[a[R]]*num[a[R]]; 63 } 64 while(R > node[i].R) 65 { 66 temp -= (long long)num[a[R]]*num[a[R]]; 67 num[a[R]]--; 68 temp += (long long)num[a[R]]*num[a[R]]; 69 R--; 70 } 71 while(L < node[i].L) 72 { 73 temp -= (long long)num[a[L]]*num[a[L]]; 74 num[a[L]]--; 75 temp += (long long)num[a[L]]*num[a[L]]; 76 L++; 77 } 78 while(L > node[i].L) 79 { 80 L--; 81 temp -= (long long)num[a[L]]*num[a[L]]; 82 num[a[L]]++; 83 temp += (long long)num[a[L]]*num[a[L]]; 84 } 85 ans[node[i].id].a = temp - (R-L+1); 86 ans[node[i].id].b = (long long)(R-L+1)*(R-L); 87 ans[node[i].id].reduce(); 88 } 89 } 90 91 92 int main() 93 { 94 //freopen("in.txt","r",stdin); 95 //freopen("out.txt","w",stdout); 96 while(scanf("%d%d",&n,&m) == 2) 97 { 98 for(int i = 1;i <= n;i++) 99 scanf("%d",&a[i]); 100 for(int i = 0;i < m;i++) 101 { 102 node[i].id = i; 103 scanf("%d%d",&node[i].L,&node[i].R); 104 } 105 unit = (int)sqrt(n); 106 sort(node,node+m,cmp); 107 work(); 108 for(int i = 0;i < m;i++) 109 printf("%lld/%lld\n",ans[i].a,ans[i].b); 110 } 111 return 0; 112 }