2038: [2009 countries Training team] small Z socks (hose) time limit:20 Sec Memory limit:259 MB
submit:6504 solved:3024
[Submit] [Status] [Discuss] 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
Mo team naked problem. We do it offline. First divide the 1~n into sqrt (n) blocks, each of which is sqrt (n). The number of the block with the left endpoint is the first keyword, and the right endpoint is the second keyword, sorting all queries. Then update the answer every time you move, moving the complexity O (1). This is the total complexity of O (N*SQRT (n)). In blocks numbered odd, the right end point is small to large, and even, from large to small. This will be faster, as to why you want to.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6 #defineINF 1<<307 #defineMAXN 500058 using namespacestd;9 intN,M,C[MAXN],S[MAXN],POS[MAXN],Q[MAXN];Ten Long Longans; One structfuck{ A intL,r,id; - Long LongFZ,FM; - }A[MAXN]; the intRead () { - intx=0, f=1;Charch; - for(Ch=getchar ();ch<'0'|| Ch>'9'; Ch=getchar ())if(ch=='-') f=-1; - for(; ch>='0'&&ch<='9'; Ch=getchar ()) x=x*Ten+ch-'0'; + returnx*F; - } + BOOLcmp (Fuck X,fuck y) { A if(pos[x.l]==POS[Y.L]) at if(pos[x.l]&1)returnx.r<Y.R; - Else returnX.r>Y.R; - returnpos[x.l]<POS[Y.L]; - } - Long LonggcdLong LongALong Longb) { - returnb==0? A:GCD (b,a%b); in } - voidUpdata (intXinty) { toans-= (Long Long) s[c[x]]*S[c[x]]; +s[c[x]]+=y; -ans+= (Long Long) s[c[x]]*S[c[x]]; the } * intMain () { $N=read (); m=read ();Panax Notoginseng for(intI=1; i<=n;i++) c[i]=read (); - for(intI=1; i<=m;i++) A[i].l=read (), a[i].r=read (); the for(intI=1; i<=m;i++) a[i].id=i; + intpps=sqrt (n); A for(intI=1; i<=n;i++) pos[i]= (i-1)/pps+1; theSort (A +1, a+m+1, CMP); + for(intI=1; i<=m;i++) q[a[i].id]=i; - for(intI=1, l=1, r=0; i<=m;i++){ $ for(; r<a[i].r;r++) Updata (r+1,1); $ for(; r>a[i].r;r--) Updata (r,-1); - for(; l<a[i].l;l++) Updata (l,-1); - for(; l>a[i].l;l--) Updata (L-1,1); the if(a[i].l==A[I].R) { -a[i].fz=0; a[i].fm=1;Wuyi Continue; the } -a[i].fz=ans-(a[i].r-a[i].l+1); WuA[i].fm= (Long Long) (a[i].r-a[i].l+1) * (a[i].r-a[i].l); - Long Longk=gcd (a[i].fz,a[i].fm); AboutA[i].fz/=k; A[i].fm/=K; $ } - for(intI=1; i<=m;i++) printf ("%lld/%lld\n", a[q[i]].fz,a[q[i]].fm); - return 0; -}
View Code
BZOJ2038: [2009 countries Training team] small Z socks (hose)