Test instructions: Give the number of n, define an interval mysterious number for the interval all the numbers can be obtained by combining the number of mex,m queries, for the interval [l,r] asked the interval of the mysterious tree.
If we sort this number and know that the first n number of mysterious number is X, that is, now the number of the interval is [1,x], the number of new addition to a, then it is not difficult to find, we gather the number and get a section of the interval [a+1,a+x], then if a+1<=x, we can spell these two paragraphs, And the mystery number becomes a+x+1.
That is to say, we have the current solution ans, we will all the small number of ans add up (in fact, according to the previous push should be less than, but write small and so will not be wrong, and for the code is better, as for why not more than repeat), if the Sigma<ans explained that there is a break, that is, ans answer. Otherwise we turn ans into sigma+1 and continue to update the answer.
Time complexity 0 (nlogn*p), where P is a constant (when the number listed as Fibonacci will be stuck to the limit 40)
There was a little episode in writing the code. At first, the Chairman's tree was written for each node to accumulate separately, so the complexity of the time is obviously not right, actually directly to the sum of each node to get out to lose it. Sure enough, it's a qaq.
1#include <cstdio>2 using namespacestd;3 #defineN 1000054 intn,m,root[n],ls[ -*n],rs[ -*n],sum[ -*n],cnt,ans,Get;5InlineintRead () {6 intx=0, f=1;CharA=GetChar ();7 while(a>'9'|| a<'0') {if(a=='-') f=-1; A=GetChar ();}8 while(a>='0'&& a<='9') x=x*Ten+a-'0', a=GetChar ();9 returnx*F;Ten } One voidInser (intXint& Y,intLintRintv) { Ay=++CNT; -sum[y]=sum[x]+v; - if(L==R)return; theLS[Y]=LS[X]; rs[y]=Rs[x]; - intMid= (l+r) >>1; - if(V>mid) Inser (rs[x],rs[y],mid+1, r,v); - ElseInser (ls[x],ls[y],l,mid,v); + } - intQueryintXintYintLintRintLim) { + intMid= (l+r) >>1; A if(R<=lim)returnsum[y]-Sum[x]; at Else if(Lim<=mid)returnquery (Ls[x],ls[y],l,mid,lim); - Else returnSum[ls[y]]-sum[ls[x]]+query (rs[x],rs[y],mid+1, R,lim); - } - intMain () { -n=read (); - for(intI=1; i<=n;i++) Inser (root[i-1],root[i],1, 1e9,read ()); inm=read (); - for(intL,r,i=1; i<=m;i++){ toL=read (); R=read (); ans=1; + while(1){ - Get=query (root[l-1],root[r],1, 1e9,ans); the if(Get<ans) Break; *ans=Get+1; $ }Panax Notoginsengprintf"%d\n", ans); - } the return 0; +}
bzoj4408: [Fjoi 2016] Mystery number