1. 程式人生 > >bzoj 2038 [2009國家集訓隊]小Z的襪子(hose) 莫隊算法

bzoj 2038 [2009國家集訓隊]小Z的襪子(hose) 莫隊算法

output efi inpu += -1 r++ http 題目中的 png

2038: [2009國家集訓隊]小Z的襪子(hose)

Time Limit: 20 Sec Memory Limit: 259 MB
Submit: 10239 Solved: 4659
[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。

HINT

Source

版權所有者:莫濤

題解: 技術分享

莫隊算法,其實就是當我們知道了[L, R] 的答案的時候,而且我們可以通過O(1) 查詢到 [L-1, R] [L+1, R] [L, R-1] [L, R+1] 的時候,並且可以支持離線的時候。我們就可以改變題目中的詢問次序,從而是O(n2) 優化到O(n1.5) 。 先將查詢分成√n 塊, 每一塊就有 √n 個。將詢問的排序,按照左端點所在的大塊為第一關鍵字,有端點為第二關鍵字。然後按照這個順序暴力解出答案。 技術分享
 1 #include <iostream>
 2 #include <cstdio>
 3
#include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 #include <set> 11 using namespace std; 12 typedef long long LL; 13 #define ms(a, b) memset(a, b, sizeof(a)) 14 #define pb push_back 15 #define mp make_pair 16 const int INF = 0x7fffffff; 17 const int inf = 0x3f3f3f3f; 18 const int mod = 1e9+7; 19 const int maxn = 50000 + 10; 20 LL a[maxn], ans_fenzi[maxn], ans_fenmu[maxn], num[maxn], k; 21 struct node { 22 LL l, r, id; 23 } q[maxn]; 24 bool cmp(node x1, node x2) { 25 if(x1.l/k != x2.l/k) { 26 return x1.l/k < x2.l/k; 27 } else 28 return x1.r < x2.r; 29 } 30 LL gcd(LL a, LL b) { 31 return b==0?a:gcd(b, a%b); 32 } 33 void init() { 34 ms(num, 0); 35 ms(a, 0); 36 } 37 void solve() { 38 LL n, m; 39 scanf("%lld%lld", &n, &m); 40 for(LL i = 1; i<=n; i++) scanf("%lld", &a[i]); 41 for(LL i = 0; i<m; i++) { 42 scanf("%lld%lld", &q[i].l,&q[i].r); 43 q[i].id = i; 44 } 45 k = (LL)sqrt(n); 46 sort(q, q+m, cmp); 47 LL L = 1, R = 0; 48 LL temp = 0; 49 for(LL i = 0; i<m; i++) { 50 while (R < q[i].r) { 51 R++; 52 temp -= (long long)num[a[R]] * num[a[R]]; 53 num[a[R]]++; 54 temp += (long long)num[a[R]] * num[a[R]]; 55 } 56 while (R > q[i].r) { 57 temp -= (long long)num[a[R]] * num[a[R]]; 58 num[a[R]]--; 59 temp += (long long)num[a[R]] * num[a[R]]; 60 R--; 61 } 62 while (L < q[i].l) { 63 temp -= (long long)num[a[L]] * num[a[L]]; 64 num[a[L]]--; 65 temp += (long long)num[a[L]] * num[a[L]]; 66 L++; 67 } 68 while (L > q[i].l) { 69 L--; 70 temp -= (long long)num[a[L]] * num[a[L]]; 71 num[a[L]]++; 72 temp += (long long)num[a[L]] * num[a[L]]; 73 } 74 LL fenzi = temp -(R-L+1); 75 LL fenmu = (R-L+1)*(R-L); 76 LL GCD = gcd(fenzi, fenmu); 77 ans_fenzi[q[i].id] = fenzi / GCD; 78 ans_fenmu[q[i].id] = fenmu / GCD; 79 } 80 for(LL i = 0; i<m; i++) { 81 printf("%lld/%lld\n", ans_fenzi[i], ans_fenmu[i]); 82 } 83 } 84 int main() { 85 #ifdef LOCAL 86 freopen("input.txt", "r", stdin); 87 // freopen("output.txt", "w", stdout); 88 #endif 89 init(); 90 solve(); 91 return 0; 92 }
View Code

bzoj 2038 [2009國家集訓隊]小Z的襪子(hose) 莫隊算法