Title Link: https://www.luogu.org/problemnew/show/P1494
Title 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, small z to this n socks from 1 to n number, and then from the number L to R (L Although small z does 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 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.
However, in the case of l=r in the data, please judge this situation, output 0/1.
Input/output format
Input format:
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 format:
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/Output sample
Input Sample # #:
6 41 2 3 3 3 22 61 33 51 6
Sample # # of output:
2/50/11/14/15
Description
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
Directly on the line segment tree, because it does not satisfy the addition and nature :
Assuming that the number of socks for each color in the interval [L, R] is now calculated, we have separately maintained the number of socks for each color in the left and right intervals [L, mid] and [Mid+1, R], and then start maintenance up:
It will be found that each color of the socks in the left and right of the number of intervals to complete the maintenance of a parent node, we can not be in O (1) time to complete the single node of the segment tree maintenance.
But at this time, if a range [L, R] is known, we can determine in O (1) The case of [L, R+1] and [L, R-1] and [L-1, R] and [L+1, R],
Suppose we now know [L, R] The case, need to calculate is [l ', R '] case, easy to take the time complexity of O (| l-l ' | + | r-r ' |),
So how can you answer query with less code in a short amount of time?
The sequence sqrt (N) is divided, then the number of the block with the left endpoint of the queried interval is the first priority, the size of the right end of the interval is the second priority, and then the complexity is greatly reduced by order.
In the order described above, the following conditions are observed from the interval I to the i+1:
So assuming that there is an X (x≥2) in a range of socks, then the possible number of two socks is $C _x^2$,
Suppose the socks in the interval have 1~k a total of k color, assuming that the number of socks in the K-color is num[k], then the two socks with the same color is the number of possible $C _{num\left[1 \right]}^2 + c_{num\left[2 \right]}^2 \cdots C _{num\left[K \right]}^2$,
$C _n^2 = \frac{{n\left ({n-1} \right)}}{2}$ According to the combinatorial number formula,
So the probability of taking two colored socks is $\frac{{num\left[1 \right]\left ({num\left[1 \right]-1} \right) + \cdots + num\left[K \right]\left ({n um\left[K \right]-1} \right)}}{{x\left ({X-1} \right)}}$,
That is $\frac{{num\left[1 \right]{}^2 + \cdots + num\left[K \right]^2-x}}{{x\left ({X-1} \right)}}$.
Because $X = r-l + 1$, we are actually querying $Q \left ({l,r} \right) = \sum\limits_{k = 1}^k {num\left[k \right]^2}$,
So
If the interval is shifted, add a sock with a color of $c $:
$Q \left ({l,r + 1} \right) = Q\left ({l-1,r} \right) = Q\left ({l,r} \right)-num\left[C \right]^2 + \left ({num\left[ C \right] + 1} \right) ^2 = Q\left ({l,r} \right) + 2 \times num\left[c \right] + 1$
If the interval is shifted, reduce the socks with a color of $c $:
$Q \left ({l,r-1} \right) = Q\left ({l{\rm{+}}1,r} \right) = Q\left ({l,r} \right)-num\left[C \right]^2 + \left ({nu m\left[C \right]-1} \right) ^2 = Q\left ({l,r} \right)-2 \times num\left[C \right] + 1$
AC Code:
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intmaxn=50000+Ten;Const intmaxm=50000+Ten;classinterval{ Public: intBlock//represents the block of the left endpoint intId//indicates the original order of Inquiry intL,r; BOOL operator< (ConstInterval &oth)Const { return( This->block = = Oth.block)? ( This->r < OTH.R): ( This->block <Oth.block); }}ITV[MAXM];intn,m;intCOLOR[MAXN],NUM[MAXN]; ll Up[maxn],down[maxn];inline ll gcd (ll A,ll b) {returnd={0? A:GCD (b,a%b);}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) { intlen=sqrt (n); for(intI=1; i<=n;i++) scanf ("%d",&Color[i]); for(intI=1; i<=m;i++) {scanf ("%d%d",&itv[i].l,&ITV[I].R); Itv[i].block=itv[i].l/Len; Itv[i].id=i; Down[i]= (ITV[I].R-ITV[I].L)/2* (itv[i].r-itv[i].l+1); } sort (ITV+1, itv+m+1); memset (num,0,sizeof(num)); intPl=1, pr=0; LL Q=0; for(intI=1; i<=m;i++) { if(itv[i].l==ITV[I].R) {Down[itv[i].id]=1; Up[itv[i].id]=0; Continue; } Down[itv[i].id]= (LL) (itv[i].r-itv[i].l+1) * (itv[i].r-ITV[I].L); if(pr<ITV[I].R) { for(intj=pr+1; j<=itv[i].r;j++) {Q=q+2*num[color[j]]+1; NUM[COLOR[J]]++; } } if(pr>ITV[I].R) { for(intj=pr;j>itv[i].r;j--) {Q=q-2*num[color[j]]+1; NUM[COLOR[J]]--; } } if(pl>ITV[I].L) { for(intj=pl-1; j>=itv[i].l;j--) {Q=q+2*num[color[j]]+1; NUM[COLOR[J]]++; } } if(pl<ITV[I].L) { for(intj=pl;j<itv[i].l;j++) {Q=q-2*num[color[j]]+1; NUM[COLOR[J]]--; }} Up[itv[i].id]=q-(itv[i].r-itv[i].l+1); PL=ITV[I].L; PR=ITV[I].R; } for(intI=1; i<=m;i++) {LL g=gcd (Up[i],down[i]); printf ("%lld/%lld\n", up[i]/g,down[i]/g); } }}
Luogu 1494-Small Z socks-[MO Team Algorithm template title]