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

Source: Internet
Author: User
Tags gcd

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

Question Description

As a rambling man of life, littleZ every morning it takes a long time to find a pair to wear from a pile 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)

Input example

6 4 1 2 3 3 3 2 2 6 1 3 3 5 1 6

Output example

2/50/11/14/

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.

Exercises

Notice the ci≤n, so you can record the number of each color, so that the answer is very easy to calculate. Further discovery: When we know the answer to the interval [L, R], it is easy to draw the answer to these intervals [L + 1, R], [L-1, R], [L, R-1], [L, R + 1], so that we can do it with Mo team.

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL        ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin);    Tail = (Head = buffer) + L; } return *head++;}    int read () {int x = 0, f = 1; char c = Getchar ();    while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();}    while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 50010#define LL long longint N, M, COL[MAXN], HAS[MAXN]; LL ANS[MAXN], bns[maxn];struct Que {int id, L, R;} QS[MAXN], Tq[maxn];bool Cmpl (Que A, Que b) {return A.L < B.L;} BOOL Cmpr (Que A, Que b) {return A.R < B.R;} ll GCD (ll A, ll b) {return!b? A:GCD (b, a% b);} int main () {n = Read (); m = read (); for (int i = 1; I <= n; i++) Col[i] = read (); for (int i = 1; I <= m; i++) qs[i].id = i, qs[i].l = Read (), q S[I].R = read (); sort (qs + 1, QS + M + 1, Cmpl);//for (int i = 1; I <= m; i++) printf ("%d%d%d\n", Qs[i].id, QS[I].L, QS [I].R); int mxl = QS[M].L, siz = (int) sqrt (MXL +. 5), NL = 1, nr = siz;for (int i = 1; i <= m;) {int J, K, x;for (j = i; J < m && nl <= qs[j].l && qs[j].l <= nr; j + +);//for (x = i; x <= J; x+ +) printf ("Here:%d%d%d\n", QS[X].L, QS[X].R, qs[x].id); sort (qs + I, QS + j + 1, cmpr); i = j + 1; NL = nr + 1; NR = nl + siz-1;} for (int i = 1; I <= m; i++) printf ("%d%d%d\n", Qs[i].id, QS[I].L, QS[I].R); nl = 1, nr = 0;  LL ans = 0;//has[col[1]] = 1;for (int i = 1; I <= m; i++) {int QL = QS[I].L, QR = Qs[i].r;while (qr > Nr) nr++, ans + = HAS[COL[NR], Has[col[nr]]++;while (QL < NL) nl--, ans + has[col[nl]], Has[col[nl]]++;while (QR < nr) has[col[nr]]-- , ans-= Has[col[nr]], nr--;while (QL > NL) has[col[NL]]--, ans-= has[col[nl]], nl++;int len = qr-ql + 1; Ans[qs[i].id] = ans; Bns[qs[i].id] = (LL) len * (LL) (len-1) >> 1ll;//printf ("%d%d%d%lld%d\n", Qs[i].id, QL, QR, ans, len);} for (int i = 1; I <= m; i++) {LL a = Ans[i], B = bns[i], g;if (!a) {puts ("0/1"); continue;} g = gcd (A, b); A/= g; b/= g;printf ("%lld/%lld\n", A, b);} 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.