Links: http://www.lydsy.com/JudgeOnline/problem.php?id=2038
2038: [2009 countries Training team] small Z socks (hose) time limit:20 Sec Memory limit:259 MB
submit:6475 solved:3004
[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
Idea: Mo team algorithm, from the interval L, R transfer to L1, R1 need to pay the price |l-l1| +| r-r1|, block-after-offline query algorithm complexity o (n * sqrt (n))
Attention to large field of view OJ high precision with%LLD
#include <cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<cstdlib>#include<iostream>using namespaceStd;typedefLong LongLL;Const intN =60000;intA[n], cnt[n];intBlk; LL TP;structans{LL A, B; ll gcd (ll A1, ll B1) { while(B1) {LL T= A1%B1; A1=B1; B1=T; } returnA1; } voidreduce () {LL D=gcd (A, b); A/=D; b/=D; }}ans[n];structnode{LL L, R, I;} Q[n];BOOLcmpConstNode &a,ConstNode &b) { if(a.l/blk! = B.L/Blk) { returnA.l/blk < B.L/Blk; }Else{ returnA.R <B.R; }}inlinevoidAddintx) {TP+=2*Cnt[a[x]]; CNT[A[X]]++;} InlinevoidRemoveintx) {Cnt[a[x]]--; TP-=2*(Cnt[a[x]]);}voidSolveintNintm) {memset (CNT,0,sizeof(CNT)); Blk= (int) sqrt (n); Sort (q, q+m, CMP); TP=0; intL =1, r =0; for(inti =0; I < m; i++){ while(L <Q[I].L) {Remove (L); L++; } while(L >Q[I].L) {L--; Add (l); } while(R <Q[I].R) {R++; Add (R); } while(R >Q[I].R) {Remove (R); R--; } ans[q[i].i].a=TP; ANS[Q[I].I].B= (LL) (R-l +1) * (R-l); Ans[q[i].i].reduce (); }}intMain () {intN, M; while(~SCANF ("%d%d", &n, &m)) { for(inti =1; I <= N; i++) {scanf ("%d", &A[i]); } for(inti =0; I < m; i++) {scanf ("%lld%lld", &Q[I].L, &Q[I].R); Q[I].I=i; } solve (n, m); for(inti =0; I < m; i++) {printf ("%lld/%lld\n", ANS[I].A, ans[i].b); } } return 0;}
MO Team Algorithm 2038: [2009 countries Training team] small Z socks (hose)