# KYEREMAL-BZOJ2038-[2009 Country Training Team-small Z socks (hose)-MO Team algorithm

Source: Internet
Author: User
Tags gcd

bzoj2038-[2009 Country Training team-small Z socks (hose)

f.a.qs home discuss problemset status ranklist contest modifyuser   manacher logout donate this site
Notice: Save the season Happy & another year of World final data, thank you &OJ Exam Test Breakthrough 3000 Mark! 2038: [2009 countries Training team] small Z socks (hose) Time limit: Sec Memory Limit: 259 MB
Submit: 3673 Solved: 1702
[Submit] [Status] [Discuss] Description

As a rambling person, little Z spends a lot of time every morning finding a pair to wear from a bunch of colorful socks.

Eventually, one day, littleZCan no longer endure this annoying to find socks process, so he decided to resign...
In detail, the smallZPut thisNJust socks from1ToNNumber. Then from the numberLToR (LAlthough smallZDo not care about two socks is not a complete pair, and even do not care whether two socks is a left and right, he is very concerned about the color of socks, after all, wearing two different colors of socks will be very embarrassing.
Your job is to tell the smallZ。 How much he had the chance to draw two socks that were just 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 consists of 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, in which CI denotes the color of the sock, and the same color is represented by the same number. Next m line, two positive integer l per line. R indicates a query.

Output

Include M-line. For each query in a row, the output fraction of A/b represents the same probability of randomly extracting two of the same socks color from the range [l,r] of the query.

If the probability is 0, the output is 0/1. Otherwise the output A/b must be a minimal fraction. (see example)

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
"Example Explanation"
Question 1: Total C (5,2) = 10 possible, two out of 2 of which there are 1 possible, the extraction of two 3 is 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 of 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 are n,m≤50000. 1≤l < R≤n,ci≤n.

HINT

Source

[Submit] [Status] [Discuss] ?
HOME Back ???  Chinese????? 中文版???
All rights reserved? 2008-2012 views Online | Xiang ICP 备 13,009,380th # | Webmaster statistics based on OpenSource project Hustoj.

The main idea: give a bunch of numbers, each time asked an interval, the interval to take two number of the same probability.

Analysis: Orz Mo Tao, MO team to the various range of inquiries

Give a few blogs about the MO team algorithm:

Very popular and intuitive to understand the algorithm

The specific

Rigorous plane dot Manhattan minimum distance spanning tree thesis

A more rigorous English paper

Code

`#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring > #include <algorithm>using namespace std; #define REP (I, L, R) for (LL i = (l); (i) <= (R); ((i) + +)) #define REP (I, L, R) for (LL i = (l); (i) >= (R); ((i)-))  #define LL Long Long#define maxn 1000010LL N, M, COL[MAXN], LEN[MAXN], K, C[MAXN], POS[MAXN], FZ, FM, Minc, maxc;struct QR {LL L, r, Num;} A[maxn];struct Answer {ll x, y, num;} ans[maxn];inline ll min (ll a, ll b) {return a<b? A:b;} inline ll Max (ll A, ll b) {return a>b? A:B;} inline BOOL CMP (QR A, QR b) {return POS[A.L]&LT;POS[B.L] | | (POS[A.L]==POS[B.L] && A.R&LT;B.R);} Inline ll com (ll N) {return (n-1) *N/2;} Inline answer MP (ll X, ll y, ll num) {answer t; t.x = x, T.y = y, t.num = num; return t;} inline bool Cmp2 (answer A, answer b) {return a.num < B.num;} inline ll gcd (ll A, ll b) {return!b? A:GCD (b, a%b);} inline void Update (LL &l, LL &r, LL L, LL R, LL &fz, ll &AMP;FM) {//[L, R]-[L, R] if (L < L) REP (    I, L-1, L) FZ + = C[col[i]], fm + = r-l+1, c[col[i]]++, l--;    else if (L > L) Rep (I, L, l-1) FZ + =-c[col[i]] + 1, FM + L-R, C[col[i]]--, l++;    if (R > R) Rep (I, r+1, r) FZ + = C[col[i]], fm + = r-l+1, c[col[i]]++, r++; else if (R < R) REP (I, R, r+1) FZ + =-c[col[i]] + 1, fm + = L-r, C[col[i]]--, r--;}    inline void block () {K = sqrt (n);    Rep (i, 1, K-1) len[i] = K;    Len[k] = n-(K-1) *k;    Rep (i, 1, K) Rep (J, (I-1) *k + 1, (i-1) *k + len[i]) pos[j] = i; Sort (a+1, a+1+m, CMP);} inline void xx () {   memset (c, 0, sizeof (c));    LL L = a.l, R = A.R;    Rep (i, L, R) c[col[i]]++;    Rep (i, Minc, maxc) FZ + = (c[i] * (c[i]-1)/2);    fm = COM (r-l+1);    ANS = MP (FZ, FM, A.num); Rep (I, 2, m) update (L, R, A[I].L, A[I].R, FZ, FM), Ans[i] = MP (FZ, FM, a[i].num);}    int main () {minc = maxn, maxc = 0;    CIN >> n >> m;    Rep (i, 1, N) scanf ("%lld", col + i), MAXC = Max (MAXC, Col[i]), minc = min (Minc, col[i]);    Rep (i, 1, m) scanf ("%lld%lld", &AMP;A[I].L, &AMP;A[I].R), a[i].num = i;    Block ();    XX ();    Sort (ans+1, ans+1+m, CMP2);        Rep (i, 1, m) n = gcd (ans[i].x, Ans[i].y), printf ("%lld/%lld\n", ans[i].x/n, ans[i].y/n); return 0;}`

KYEREMAL-BZOJ2038-[2009 Country Training Team-small Z socks (hose)-MO Team algorithm

Related Keywords:

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.