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)

Th style= "Font-family:arial,verdana,helvetica,sans-serif" >status
f.a.qs home discuss ProblemSet 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 man of life, littleZEvery morning it takes a long time to find a pair to wear from a pile of colorful socks. Finally one day, littleZCan no longer endure this annoying to find socks process, so he decided to resign......
Specifically, the smallZPut thisNOnly socks from1ToNNumber, and then from the numberLToR (LAlthough smallZDo 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 job is to tell the smallZ, he has a great chance of pumping two socks of the same color. Of course, littleZHopefully this probability is as high as possible, so he might ask multiple(l,r)To facilitate their own 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

[Submit] [Status] [Discuss] 
HOME Back ???  Chinese????? 中文版???
All rights reserved? 2008-2012 View Online Assessment | 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

A detailed explanation of the subject

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[1].l, R = A[1].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[1] = MP (FZ, FM, A[1].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

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.