DescriptionIn 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: give a 1 to n full arrangement, now the full permutation of the 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) for the interval [l,r] number descending sort finally ask Q The number on the position.
InputThe 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
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 Input 6 3
1 6 2 5 3 4
0 1 4
1 3 6
0 2 4
3 Sample Output 5 Solution: The first two-point answer. Then just judge whether the number is legal. Set this number to x so you can count the number in this sequence to be 1, the number smaller than X is 1, and the number equal to X is 0. Finally, only the number in the Q position can be judged to be 0. To maintain the number of -1,0,1 in the sequence, it is clear that the operation can be processed with a line tree. Code:
#include <iostream> #include <cstdio> #include <cstring> #define N 100010 using namespace std;
int a[n],ans,n,l,r,q,m,v[n],p[n<<2],sum; struct Seg{int s0,s1,s2;}
t[n<<2]; struct Use{int k,l,r;}
C[n];
SEG Update (SEG a,seg b) {seg C;
C.s0=a.s0+b.s0;
C.S1=A.S1+B.S1;
C.S2=A.S2+B.S2;
return C;
} void Build (int k,int l,int r) {int mid= (L+R) >>1;
if (l==r) {if (v[l]==0) t[k].s0++;
if (v[l]==1) t[k].s1++;
if (v[l]==2) t[k].s2++;
Return
} build (K<<1,l,mid);
Build (K<<1|1,mid+1,r);
T[k]=update (t[k<<1],t[k<<1|1]);
} void Paint (int k,int l,int R,int v) {if (v==0) {t[k].s0=r-l+1;t[k].s1=t[k].s2=0;}
if (v==1) {t[k].s1=r-l+1;t[k].s0=t[k].s2=0;}
if (v==2) {t[k].s2=r-l+1;t[k].s0=t[k].s1=0;}
P[k]=v;
} void pushdown (int k,int l,int r) {int mid= (L+R) >>1;
Paint (K<<1,l,mid,p[k]);
Paint (K<<1|1,mid+1,r,p[k]);
P[k]=-1; } SEG Query (int k,int l,int r,int ll,int rr) {int mid= (L+R) ≫>1;
if (LL==L&&R==RR) {return t[k];}
if (p[k]!=-1) pushdown (k,l,r);
if (rr<=mid) return query (K<<1,L,MID,LL,RR);
else if (MID<LL) return query (K<<1|1,MID+1,R,LL,RR);
else return update (query (K<<1,L,MID,LL,MID), query (K<<1|1,MID+1,R,MID+1,RR));
} void Change (int k,int l,int r,int ll,int rr,int v) {int mid= (L+R) >>1;
if (LL<=L&&R<=RR) {paint (k,l,r,v); return;}
if (p[k]!=-1) pushdown (k,l,r);
if (ll<=mid) change (K<<1,L,MID,LL,RR,V);
if (MID<RR) change (K<<1|1,MID+1,R,LL,RR,V);
T[k]=update (t[k<<1],t[k<<1|1]);
} int judge (int x) {for (int i=1;i<=n;i++) if (a[i]<x) v[i]=0;
else if (a[i]==x) v[i]=1;
else v[i]=2;
for (int i=1;i<= (N<<2); i++) T[i].s0=t[i].s1=t[i].s2=0,p[i]=-1;
Build (1,1,n);
for (int i=1;i<=m;i++) {seg tt=query (1,1,N,C[I].L,C[I].R);
cout<<tt.s0<< ' <<tt.s1<< ' <<tt.s2<<endl; If(c[i].k==0)
{int a=c[i].l+tt.s0-1,b=a+tt.s1;
if (TT.S0) change (1,1,n,c[i].l,a,0);
cout<<a+1<< ' <<b<<endl;
if (TT.S1) change (1,1,n,a+1,b,1);
if (TT.S2) change (1,1,n,b+1,c[i].r,2);
} else{int a=c[i].l+tt.s2-1,b=a+tt.s1;
if (TT.S2) change (1,1,n,c[i].l,a,2);
if (TT.S1) change (1,1,n,a+1,b,1);
if (TT.S0) change (1,1,n,b+1,c[i].r,0);
}} seg Tt=query (1,1,N,Q,Q);
if (TT.S0) return 0;
if (TT.S1) return 1;
if (TT.S2) return 2;
} int main () {scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
for (int i=1;i<=m;i++) scanf ("%d%d%d", &C[I].K,&C[I].L,&C[I].R);
scanf ("%d", &q); l=1;r=n;
Cout<<judge (5) <<endl;
while (l<=r) {//cout<<l<< ' <<r<<endl;
int mid= (L+R) >>1;
int T=judge (mid);
if (t==2) l=mid+1;
else if (t==1) {ans=mid;break;}
else r=mid-1; } cout<<ans<<endl; }