2038: [2009 countries Training team] small Z socks (hose) time limit:20 Sec Memory limit:259 MB
submit:7676 solved:3509
[Submit] [Status] [Discuss] 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 though little Z doesn't care if two socks are a complete pair, even if two socks are left and right, he cares about the color of the socks, After all, wearing two different color socks will be very embarrassing.
Your task is to tell little Z how much he has the chance to draw two socks of the same color. Of course, little Z wants this probability to be as high as possible, so he may ask multiple (L,R) to facilitate his choice.
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] [Discuss]
Mo team algorithm, is to think about how to calculate the probability.
For a query interval [l,r], the first denominator should be (r-l+1) * (R-L), which is the total number of options (consider the selected order). For a color I, if its occurrence is cnt[i], select two times the scheme number of this color is cnt[i]* (cnt[i]-1). The total number of legal options is sum (cnt[i]* (cnt[i]-1)), because sum (Cnt[i]) is the interval length, you can extract a sum (cnt[i]) = (r-l+1) in the scheme number, then the final molecule can be written as SUM (SQR (cnt[i) )-(r-l+1), you only need to maintain the sum of squares of the occurrences of each color.
1#include <bits/stdc++.h>2 3typedefLong LongLongint;4 5Template <classInt>6 int gcd (int a, int b) {7 returnB? GCD (b, a%b): A;8 }9 Ten Const intMAXN =50000+5; One A intN, M; - intL, R, S; - intCOL[MAXN]; the intCNT[MAXN]; - Longint answer; - - structQuery { + intL, R, id; - Longint A, b; + }QRY[MAXN]; A atInlineBOOLCMP_LR (ConstQuery &a,ConstQuery &b) { - if(a.l/s! = B.L/s) - returnA.L <B.L; - Else - returnA.R <B.R; - } in -InlineBOOLCMP_ID (ConstQuery &a,ConstQuery &b) { to returna.ID <b.id; + } - theInline Longint Sqr (intt) { * return(Longint) (t) *(Longint) (t); $ }Panax Notoginseng -InlinevoidRemoveintt) { theAnswer-=Sqr (cnt[t]); +--Cnt[t]; AAnswer + =Sqr (cnt[t]); the } + -InlinevoidInsertintt) { $Answer-=Sqr (cnt[t]); $++Cnt[t]; -Answer + =Sqr (cnt[t]); - } the -Inline Longint Calc (intt) {Wuyi return(Longint) (T +1) *(Longint) (t); the } - WuInlinevoidSolvevoid) { - for(inti =1; I <= m; ++i) { About while(L < QRY[I].L) Remove (col[l++]); $ while(L > QRY[I].L) Insert (col[--l]); - while(R < QRY[I].R) Insert (col[++R]); - while(R > Qry[i].r) Remove (col[r--]); -Longint len = qry[i].r-qry[i].l +1; AQRY[I].A = Answer-Len; +qry[i].b = len* (Len-1); the } - } $ theInlinevoidPrintvoid) { the for(inti =1; I <= m; ++i) { theLongint g =gcd (QRY[I].A, qry[i].b); theprintf"%lld/%lld\n", qry[i].a/g, qry[i].b/g); - } in } the theSigned Main (void) { Aboutscanf"%d%d", &n, &m); the the for(inti =1; I <= N; ++i) thescanf"%d", col +i); + - for(inti =1; I <= m; ++i) thescanf"%d%d", &QRY[I].L, &QRY[I].R), qry[i].id =i;Bayi thes = sqrt (n); L =1; R =0; the -Std::sort (Qry +1, Qry +1+m, CMP_LR); Solve (); -Std::sort (Qry +1, Qry +1+m, cmp_id); Print (); the}
@Author: Yousiki
Bzoj 2038: [2009 countries Training team] small Z socks (hose)