http://www.lydsy.com/JudgeOnline/problem.php?id=2038
Learned the next team, quite the orz of God
First of all, if you push the formula, it's easy. For query $[l,r]$
$ $ans =\frac{\sum \binom{x_i}{2}}{\binom{r-l+1}{2}}$$
Late repair ... Come back to mend.
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef unsigned int ll; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #defi Ne for2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n);-I.) #define FOR4 (i,a,n) for (int i = (a);i> (n),-i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int n=50005;struct dat {int L, r, id;} Q[n];int A[n], N, M, Blc[n];ll sum, x[n], Y[n], S[n];inline ll gcd (const LL &a, const LL &b) {return b?gcd (b, a%b): A;} inline BOOL cmp (const DAT &a, const dat &b) {return blc[a.l]==blc[b.l]?a.r<b.r:a.l<b.l;} inline void fix (const int &X, const int &f) {sum-=s[x]*s[x];s[x]+=f;sum+=s[x]*s[x];} void init () {int sz=sqrt (0.5+n), now=0, Tot=0;for1 (i, 1, n) {++now;blc[i]=tot;if (Now==sz) now=0, ++tot;} Sort (q+1, q+1+m, CMP);} int main () {read (n); read (m); For1 (i, 1, N) read (a[i]); For1 (i, 1, m) read (Q[I].L), read (Q[I].R), q[i].id=i;init (); int l=q[1 ].L, R=q[1].r;for1 (i, L, r) fix (A[i], 1); x[q[1].id]=sum-(r-l+1); y[q[1].id]= (r-l+1) * (r-l); For1 (I, 2, m) {int XL=Q[I].L, XR=Q[I].R, Id=q[i].id;ll rg= (xr-xl+1), while (L<XL) fix (a[l++],-1), while (L>XL) fix (A[--l], 1), while (R<XR) fix (A[++r], 1), while (R>XR) fix (a[r--],-1); x[id]=sum-rg;y[id]=rg* (rg-1);} For1 (i, 1, m) {if (x[i]<=0 | | y[i]<=0) {puts ("0/1"); continue;} ll G=GCD (X[i], y[i]); printf ("%u/%u\n", x[i]/g, y[ I]/G); }retuRN 0;}
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.
HINT Source
Copyright owner: Mo Tao
"Bzoj" 2038: [2009 Country Training team] small Z socks (hose) (combination count + probability + MO team algorithm + chunking)