[MO Team algorithm] small Z socks (hose)

Source: Internet
Author: User
Tags gcd

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. Test instructions: Answer multiple inquiries; [L,r], ask

Idea: That is, the number of times in the maintenance interval and the number of times within the interval of the sum of squares, this information can be in O (1) time from the interval [l,r] to the interval [l-1,r][l+1,r][l,r-1][l,r+1], it can be used MO team algorithm to answer questions About MO Team algorithm: Https://www.cnblogs.com/chenhuan001/p/5279988.htmlAC code:
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>typedefLong Longll;using namespacestd;ll c[50010],cnt[50010],block;ll ans=0; ll top[50010],bot[50010];structquery{ll L,r,ind;} query[50010];BOOLCMP (Query a,query b) {if(A.l/block==b.l/block)returna.r<B.R; Else returna.l/block<b.l/Block;}voidAdd (ll i) {ans-=cnt[c[i]]*Cnt[c[i]]; Cnt[c[i]]++; Ans+=cnt[c[i]]*Cnt[c[i]];}voidSub (ll i) {ans-=cnt[c[i]]*Cnt[c[i]]; Cnt[c[i]]--; Ans+=cnt[c[i]]*Cnt[c[i]];} ll GCD (ll A,ll b) {returnB?GCD (b,a%b): A;}intMain () {ll n,m;scanf ("%lld%lld",&n,&m);  for(LL i=1; i<=n;i++) scanf ("%lld",&C[i]);  for(LL i=1; i<=m;i++) scanf ("%lld%lld", &AMP;QUERY[I].L,&AMP;QUERY[I].R), query[i].ind=i; Block=(LL) sqrt (n); Sort (Query+1, query+1+m,cmp); ll L=1, r=0;//l starting from 1 for(LL i=1; i<=m;i++){         while(R&GT;QUERY[I].R) {Sub (R); r--;}  while(R&LT;QUERY[I].R) {r++; Add (R);}  while(L&GT;QUERY[I].L) {l--; Add (L);}  while(L&LT;QUERY[I].L) {Sub (L); l++;} ll TMP1=ans-(r-l+1); ll TMP2= (r-l+1) * (rm); ll G=gcd (TMP1,TMP2); if(tmp1==0) top[query[i].ind]=0, bot[query[i].ind]=1; Elsetop[query[i].ind]=tmp1/g,bot[query[i].ind]=tmp2/G; }     for(LL i=1; i<=m;i++) printf ("%lld/%lld\n", Top[i],bot[i]); return 0;}

[MO Team algorithm] 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.