Title 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/output format
Input Format:
The first behavior of the input data is 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 Format:
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.
Input/Output sample
Input Sample # #:Copy
6 31 6 2 5 3 40 1 41 3 60 2 43
Sample # # of output:Copy
5
Description
Hebei Province selected 2016 the first day of the second question. The time limit of the original question is 6s, but the valley is 1s, so rokua data, for 30% of the data, there is n,m<=1000, for 100% of the data, there is n,m<=30000
The size of the two-part answer is mid.
Greater than or equal to mid is set to 1, and the rest is set to 0.
This allows the $\large O (LOGN) $ sort to be implemented with the line segment tree.
So if the position P is 1 after the sort ends, increase the L, otherwise decrease R.
#include <iostream>#include<cstdio>using namespacestd;#defineReg RegisterinlineintRead () {intres =0;CharCh=GetChar (); while(!isdigit (CH)) ch=GetChar (); while(IsDigit (CH)) res= (res<<3) + (res<<1) + (ch^ -), ch=GetChar (); returnRes;}#defineN 100010intN, M, p, Erf;intans;intA[n];structQue {intL, R, opt;} Q[n];intcnt[n*4], lazy[n*4];#defineLS (o) o << 1#defineRS (o) O << 1 | 1inlinevoidPushup (into) {Cnt[o]= Cnt[ls (o)] +Cnt[rs (o)];}voidBuild (intLintRinto) {Lazy[o]= -1; if(L = =R) {Cnt[o]= (A[l] >=Erf); Lazy[o]= -1; return ; } intMID = L + R >>1; Build (L, Mid, LS (o)); Build (Mid+1, R, RS (o)); Pushup (o);}//lazy:1)-1 means none//2) 1 means change to 1//3) 0 means change to 0InlinevoidPushdown (intLintRinto) {if(Lazy[o] = =-1)return ; intMID = L + R >>1; if(Lazy[o] = =1) {Cnt[ls (o)]= Mid-l +1; Lazy[ls (o)]=1; Cnt[rs (o)]= R-mid; Lazy[rs (o)]=1; Lazy[o]= -1; } Else{cnt[ls (o)]=0, Lazy[ls (o)] =0; Cnt[rs (o)]=0, Lazy[rs (o)] =0; Lazy[o]= -1; }}voidChangeintLintRintOintQlintQrintc) { if(l >= QL and R <=qr) { if(c) Cnt[o] = r-l +1, Lazy[o] =1; ElseCnt[o] =0, Lazy[o] =0; return; } pushdown (L, R, O); intMID = L + R >>1; if(QL <=mid) Change (l, Mid, LS (o), QL, QR, c); if(QR > Mid) Change (mid +1, R, RS (o), QL, QR, c); Pushup (o);}intQueryintLintRintOintQlintqr) { if(l >= QL and R <= QR)returnCnt[o]; Pushdown (L, R, O); intMID = L + R >>1; intres =0; if(Mid >= QL) Res + =query (l, Mid, LS (o), QL, QR); if(Mid < QR) Res + = Query (mid +1, R, RS (o), QL, QR); returnRes;} InlineBOOLCheckintmid) {Erf=mid; Build (1N1);//printf ("Mid =%d\n", mid); for(reginti =1; I <= m; i + +) { intL = q[i].l, R =Q[I].R; intc = Query (1N1, L, R); if(Q[i].opt = =0) {//AscendingChange1N1, R-c +1R1); Change (1N1, L, R-c,0); } Else{Change (1N1, L, L + C-1,1); Change (1N1, L + C, R,0); } }//printf ("%d\n", query (1, N, 1, p, p)); returnQuery1N1, p, p);}intMain () {n= Read (), M =read (); for(reginti =1; I <= N; i + +) a[i] =read (); for(reginti =1; I <= m; i + +) q[i].opt = Read (), Q[I].L = Read (), Q[I].R =read (); P=read (); intL =1, r =N; while(L <=r) {intMID = L + R >>1; if(Check (mid)) ans = Mid, L = mid +1; ElseR = Mid-1; } printf ("%d\n", ans); return 0;}
[Luogu2824] [heoi2016/tjoi2016] Sort