[Bzoj2039] Small Z socks [MO team Algorithm template problem]

Source: Internet
Author: User
Tags gcd

2038: [2009 Country Training team] small Z socks (hose)

Time limit:20 Sec Memory limit:259 MB
submit:11866 solved:5318
[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 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
"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;
MO Team algorithm:

When we encounter many questions of interval query, we all think of line tree or tree-like array. But this problem does not work. At this point we can use the MO team algorithm.

The so-called Mo team algorithm, is optimized after the violence . Why do you say that?

When we know the information of an interval (l,r), we can have O (1) Time maintenance (L-1,R) or (L + 1,r) or (l,r-1) or (l,r + 1) information.

As the problem says: In the 1-n of the range of different colors of socks scattered, we know (L,R) the number of socks in the interval, when to (L-1,R) we only need to add the original basis in the subscript (L-1) location of socks on the line.

Then say the MO Team algorithm specific operation:

First off-line all the interval to be queried in order: the 1-n sub-block, divided into sqrt (n) or sqrt (n) + 1 intervals. We use the interval of the left end of the query as the first key value, and the right end of the query as the second key to sort.

When we query the interval in turn, L is monotonically increasing in different intervals, and R is monotonically increasing in the same interval, when the two pointers are moved to the operation of the Myopia O (n).

In the same interval, L is unordered, and R is disordered in different intervals, so violence moves on the line.

because the chunking is sqrt (n) Move pointer approximate o (n) total complexity O (n sqrt ( N))

Analysis:

say the question again, when querying an interval ans = (c (a1,2) + C (a2,2) + ... C (ak,2))/C (len,2), A1,a2......ak for the number of individual color socks in the current interval, Len is the current interval length, A1 + a2 + ... ak = = Len;

Because C (x,2) = = x * (x-1), the answer is ((A1 * (a1-1)) + ... (AK * (AK-1)))/Len * (len-1), denominator plus A1 + A2 + ... ak, plus Len becomes a1²+a2²+......ak².

So we just maintain the sum of squares of each sock in the l,r, and the numerator-Len, the denominator is Len * (len-1) for the answer, GCD-pass a moment. Specific maintenance method using MO team algorithm

Attach the AC code:

# include <iostream># include<cstdio># include<cstring># include<cmath># include<algorithm>using namespacestd;Const intN = 5e4 + A;intN,m,a[n],tot,block[n];Long LongAns,sum[n],f[n];Long LongSLong LongK) {returnKK;}Long LongGCD (Long LongALong Longb) {if(A < b) Swap (A, a); while(b) {A%= B;swap (A, b);}returnA;}structdata{intl,r,xh; Long Longc,d;BOOL operator< (ConstData & Other)Const { if(Block[l] = = Block[other.l])returnR <OTHER.R;returnL <other.l;}} Q[n];BOOLCMP (data a,data b) {returnA.xh <b.xh;}voidUpdata (intNumintL) {ans-= S (Sum[a[num]]); Sum[a[num]] + = L;ans + =S (Sum[a[num]]);}intMain () {scanf ("%d%d", &n,&m); tot =sqrt (n);  for(inti =1; I <= n;i++) scanf ("%d", &a[i]), block[i] = I/tot +1;  for(inti =1; I <= m;i++) scanf ("%d%d", &AMP;Q[I].L,&AMP;Q[I].R), q[i].xh =i; Sort (q+1, q + M +1); intL =1, r =0;  for(inti =1; I <= m;i++){     while(L < Q[I].L) Updata (l,-1), l++;  while(L > Q[i].l) updata (L-1,1), l--;  while(R < Q[I].R) Updata (R +1,1), r++;  while(R > Q[i].r) updata (r,-1), r--; if(L = = r) {q[i].c =0; q[i].d =1;Continue;} Q[I].C= ans-(Q[I].R-Q[I].L +1); Q[I].D= (Long Long) (Q[I].R-Q[I].L +1) * (Long Long) (Q[I].R-Q[I].L); Long LongK =GCD (Q[I].D,Q[I].C); Q[I].C/= K;Q[I].D/=K; } sort (Q+1, q + M +1, CMP);  for(inti =1; I <= m;i++) {printf ("%lld/%lld\n", Q[I].C,Q[I].D); }  return 0;}

[Bzoj2039] small Z socks [MO team Algorithm template problem]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.