Transfer from http://www.cnblogs.com/kuangbin/archive/2013/08/16/3263483.html
2038: [2009 countries Training team] small Z socks (hose) time limit:20 Sec Memory limit:259 MB
submit:966 solved:472
[Submit] [Status] Description
As a rambling person, little Z spends a lot of time every morning looking for a pair to wear from a bunch of colorful socks. Finally one day, little z can no longer endure this annoying to find socks process, so he decided to resign to fate ... Specifically, little z numbered the n socks from 1 to N, and then from the number L to R (l
Input
The first line of the input file contains two positive integers n and M. n is the number of socks, M is the number of inquiries raised by small Z. The next line consists of n positive integer ci, where CI denotes the color of the sock, and the same color is represented by the same number. The next m line, two positive integer l for each line, R indicates a query.
Output
Contains m rows, for each query the output fraction of a line is a/b indicating the probability of randomly extracting two socks of the same color from the range [l,r] of the query. If the probability is 0 then output 0/1, otherwise the output of A/b must be the simplest fraction. (See examples)
Sample Input6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6
Sample Output2/5
0/1
1/1
4/15
"Sample Interpretation"
Inquiry 1: Total C (5,2) = 10 possible, of which two 2 are extracted 1 possible, extract two 3 has 3 possible, the probability is (1+3)/10=4/10=2/5.
Question 2: Total C (3,2) = 3 possible, can not draw the same color socks, the probability is 0/3=0/1.
Inquiry 3: Total C (3,2) = 3 possible, are extracted two 3, the probability is 3/3=1/1.
Note: The above C (A, b) represents the number of combinations, the combination of C (A, B) is equivalent to the selection of B in a different item number of selection scheme.
"Data size and conventions"
30% of the data are n,m≤5000;
60% of the data are n,m≤25000;
100% of the data in N,m≤50000,1≤l < R≤n,ci≤n.
HINT Source
Copyright owner: Mo Tao
[Submit] [Status]
HOME back
MO team algorithm can solve a class of non-modified, offline query problems.
The practice of constructing the Manhattan minimum spanning tree is not yet written.
Wrote a direct segmentation solution.
Divide the 1~n into sqrt (n) segments.
unit = sqrt (n)
m queries are sorted by the first few blocks and then by R.
and then solve directly.
#include <iostream>#include<algorithm>#include<string.h>#include<vector>#include<cstdio>#include<cmath>using namespacestd;Const intMAXN =60005; typedefLong LongLL;structnode{intL,r,id; LL A, B;} P[MAXN],T[MAXN];intC[maxn],b[maxn],block;BOOLCMP1 (node A, node B) {returnB[A.L]!=B[B.L]? b[a.l]<b[b.l]:a.r<B.R;} LL ans; LL NUM[MAXN];BOOLCMP2 (node A, node B) {returna.id<b.id;}voidUpdateintXintadd) {ans-= num[x]*Num[x]; NUM[X]+=add; Ans+=num[x]*num[x];} ll GCD (ll A, ll b) {if(b==0)returnA; Else returnGCD (b,a%b);}voidSolveintM) { intL=1, r=0; for(intI=1; i<=m; i++){ while(r<P[i]. R) {R++; Update (C[r],1); } while(r>P[i]. R) {update (c[r],-1); r--; } while(l<P[i]. L) {update (c[l],-1); l++; } while(l>P[i]. L) {L--; Update (C[l],1); } if(P[i]. l==P[i]. R) {p[i].a=0; p[i].b=1;Continue; } LL a= ans-(p[i]. R-p[i]. L +1); LL b= (Long Long) (P[i]. R-p[i]. L +1) * ((P[i]. RP[i]. L)); LL k=gcd (A, b); T[P[I].ID].A=a/k; t[p[i].id].b=b/K; }}intMain () {intn,m; c[0]=0; while(SCANF ("%d%d", &n,&m) = =2) {ans=0; for(intI=1; i<=n; i++) scanf ("%d",&C[i]); for(intI=1; i<=m; i++) {scanf ("%d%d", &p[i]. l,&P[i]. R); //if (P[i]. L>p[i]. R) Swap (P[i]. L,p[i]. R);P[i].id=i; } memset (num,0,sizeof(num)); Block=(int) sqrt (N); for(intI=1; i<=n; i++) B[i]= (I-1)/block+1; Sort (P+1, p+m+1, CMP1); Solve (M); for(intI=1; i<=m; i++) {printf ("%lld/%lld\n", t[i].a,t[i].b); } } return 0;}
bzoj2038: [2009 countries Training team] small Z socks (hose) MO Team Algorithm