BZOJ2038: [2009 countries Training team] small Z socks (hose)

Source: Internet
Author: User
Tags gcd

Time limit:20 Sec Memory limit:259 MB 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, small z to this n socks from 1 to n number, and then from the number L to R (L Although small z does not care about two socks is not a complete pair, even do not care about two socks whether a left and right, he is very concerned about the color of 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 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;
100% of the data in N,m≤50000,1≤l < R≤n,ci≤n.

Mo Team Bare title:

Just talk about how to complete the movement of L and R in O (1) complexity.

Use a bucket to record the number of times each color's socks appear.

L Shift Right or R left: The total possible number reduces the interval length after the change, the number of the same socks can be reduced by wearing the same sock number of the same color.

L Move left or R right: The total likelihood increases the interval length before the change, and the number of socks that wear the same socks increases the number of the same color before the change.

#include <cstdio>#include<algorithm>using namespaceStd;typedefLong Longll;Const intn=50005;structx{intBh,l,r;} X[n];intCo[n],tong[n];ll Fm[n],fz[n];BOOLcmpConstX &a,ConstX &b) {    returna.l/ -==b.l/ -? a.r<b.r:a.l/ -<b.l/ -;} ll GCD (ll A,ll b) { while(b) {ll C=a%b; A=b; b=C; }    returnA;}intMain () {intn,m; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) scanf ("%d",&Co[i]);  for(intI=1; i<=m;i++) scanf ("%d%d", &AMP;X[I].L,&AMP;X[I].R), x[i].bh=i; Sort (x+1, x+m+1, CMP); ll a=0, b=0;  for(intI=1, l=1, r=0; i<=m;i++)    {         while(x[i].r<R) B-=r-l,a-=--tong[co[r--]];  while(x[i].l>l) B-=r-l,a-=--tong[co[l++]];  while(x[i].r>R) A+=tong[co[++r]]++,b+=r-l;  while(x[i].l<l) A+=tong[co[--l]]++,b+=r-l; FM[X[I].BH]=b; FZ[X[I].BH]=A; }     for(intI=1; i<=m;i++)        if(Fz[i]) {ll T=gcd (Fz[i],fm[i]); printf ("%lld/%lld\n", fz[i]/t,fm[i]/t); }        Elseprintf"0/1\n"); return 0;}

BZOJ2038: [2009 countries Training team] small Z socks (hose)

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.