http://www.lydsy.com/JudgeOnline/problem.php?id=4552
Two-point answer
Consider the number of >=mid as the number of 1,<mid 0
This sort of ascending, descending order is equivalent to the number of interval query 0,1, the interval covers 0,1
Line tree to complete
Query given position P
if = 1, indicating the number of P position >=mid, increase the lower bound
if = 0, indicating the number of P position <mid, downward bound
#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;#defineN 100001intn,m,p;intA[n],mid;intsum0[n<<2],sum1[n<<2],flag[n<<2];inttot0,tot1;structnode{intTy,l,r;} E[n];voidReadint&x) {x=0;CharC=GetChar (); while(!isdigit (c)) c=GetChar (); while(IsDigit (c)) {x=x*Ten+c-'0'; C=GetChar ();}}voidBuildintKintLintR) {Sum0[k]=sum1[k]=0; FLAG[K]=-1; if(l==r) {if(A[l]>=mid) sum1[k]++; Elsesum0[k]++; return; } intMid=l+r>>1; Build (k<<1, L,mid); Build (k<<1|1, mid+1, R); SUM0[K]=sum0[k<<1]+sum0[k<<1|1]; SUM1[K]=sum1[k<<1]+sum1[k<<1|1];}voidDownintKintLintMidintR) { if(!Flag[k]) {Sum0[k<<1]=mid-l+1; Sum1[k<<1]=0; Sum0[k<<1|1]=r-mid; Sum1[k<<1|1]=0; } Else{sum1[k<<1]=mid-l+1; Sum0[k<<1]=0; Sum1[k<<1|1]=r-mid; Sum0[k<<1|1]=0; } flag[k<<1]=flag[k<<1|1]=Flag[k]; FLAG[K]=-1;}voidQueryintKintLintRintOplintOPR) { if(L>=OPL && r<=OPR) {tot0+=Sum0[k]; TOT1+=Sum1[k]; return; } intMid=l+r>>1; if(flag[k]!=-1) down (k,l,mid,r); if(opl<=mid) query (k<<1, L,MID,OPL,OPR); if(opr>mid) query (k<<1|1, mid+1, R,OPL,OPR);}voidChangeintKintLintRintOplintOprintty) { if(L>=OPL && r<=OPR) { if(!ty) {Sum0[k]=r-l+1; SUM1[K]=0; } Else{Sum0[k]=0; SUM1[K]=r-l+1; } Flag[k]=Ty; return; } intMid=l+r>>1; if(flag[k]!=-1) down (k,l,mid,r); if(opl<=mid) Change (k<<1, L,mid,opl,opr,ty); if(opr>mid) Change (k<<1|1, mid+1, R,opl,opr,ty); SUM0[K]=sum0[k<<1]+sum0[k<<1|1]; SUM1[K]=sum1[k<<1]+sum1[k<<1|1];}intAskintKintLintRintPOS) { if(L==R)returnSum1[k]; intMid=l+r>>1; if(flag[k]!=-1) down (k,l,mid,r); if(Pos<=mid)returnAsk (k<<1, L,mid,pos); returnAsk (k<<1|1, mid+1, R,pos);}BOOLCheckintmid) {MID=mid; Build (1,1, N); for(intI=1; i<=m;++i) {tot0=tot1=0; Query (1,1, N,E[I].L,E[I].R); if(!e[i].ty) { if(tot0) Change (1,1, n,e[i].l,e[i].l+tot0-1,0); if(TOT1) Change (1,1, n,e[i].r-tot1+1, E[I].R,1); } Else { if(TOT1) Change (1,1, n,e[i].l,e[i].l+tot1-1,1); if(tot0) Change (1,1, n,e[i].r-tot0+1, E[I].R,0); } } returnAsk1,1, n,p);}intMain () {read (n); read (m); for(intI=1; i<=n;++i) read (a[i]); for(intI=1; i<=m;++i) read (e[i].ty), read (E[I].L), read (E[I].R); Read (p); intL=1, r=N,mid,ans; while(l<=r) {Mid=mid=l+r>>1; if(Check (mid)) ans=mid,l=mid+1; Elser=mid-1; } cout<<ans;}
4552: [tjoi2016&heoi2016] Sort time limit:60 Sec Memory limit:256 MB
submit:1478 solved:748
[Submit] [Status] [Discuss] Description in 2016, her sister liked the number sequence. So he often studies some weird questions about sequences, and now he's studying a problem that needs you to help him. This puzzle is like this: given a 1 to n full arrangement, now the full permutation sequence of M-order, sorted into two kinds: 1: (0,l,r) for the interval [l,r] of the number ascending sort 2: (1,l,r) means the interval [l,r] The number in descending order finally asks the number at position Q. Input inputs the first behavior of the data two integers n and M. n indicates the length of the sequence, and M indicates the number of local sorts. 1 <= N, M <= 10^5 the second behavior n integers, representing a full array of 1 to N. Next enter M line, each line has three integer op, L, R, op 0 for ascending sort, op 1 for descending sort, L, R for sorting interval. Finally, enter an integer q,q to indicate where to ask after sorting, 1 <= q <= N. 1 <= n <= 10^5,1 <= m <= 10^5 Output
The output data has only one row, and an integer that represents the number at the Q position after the whole part is sorted in order.
Sample Input6 3
1 6 2 5 3 4
0 1 4
1 3 6
0 2 4
3Sample Output5
Bzoj thousand plan 128:bzoj4552: [tjoi2016&heoi2016] Sort