[bzoj4552] [Tjoi2016] [Heoi2016] Sort

Source: Internet
Author: User
Tags stdin

Description

Give one to the full arrangement, now the order of the whole sequence of sub-local sorting, sorted into species:

Indicates that the number of intervals is sorted in ascending order;

Indicates that the number of intervals is sorted in descending order.

Finally, ask for the number at the first position.

Input

Act two integers and. Represents the length of a sequence, indicating the number of local sorts.

The first integer that represents the full permutation to.

Next enter the line, each line has an integer

Sort in ascending order, 1 for descending order, or interval for sorting.

Finally, enter an integer that indicates where to ask after the sort is finished.

Output

The output data has only one row of integers, representing the numbers at the end of the order in which all parts are sorted.

Sample Input

6 3
1 6 2 5 3 4
0 1 4
1 3 6
0 2 4
3

Sample Output

5

HINT

Solution

Two-point answer.

The position is marked and the rest is marked.

The segment tree maintains the sort operation and finally determines whether the position is.

#include <cmath>#include<ctime>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineN 100005#defineM 300005using namespacestd;structlinetree{intL,r,t,op;} LT[M];structquest{intOp,l,r;} B[n];intA[n],n,m,q,t,l,r,mid;inlinevoidBuildintUintLintRintk) {LT[U].L=l;lt[u].r=r;lt[u].op=0; if(lt[u].l<LT[U].R) {        intlef=u<<1,rig=u<<1|1; intMid= (LT[U].L+LT[U].R) >>1; Build (lef,l,mid,k); Build (Rig,mid+1, r,k); LT[U].T=lt[lef].t+lt[rig].t; }    Else if(a[lt[u].l]>=k) lt[u].t=0; Elselt[u].t=1;} InlinevoidCoverintUinti) {    if(lt[u].l>=b[i].l&&lt[u].r<=B[I].R) {        if(!B[i].op) {Lt[u].op=-1; if(lt[u].l-b[i].l+1<=t) {lt[u].t=min (t (LT[U].L-B[I].L), lt[u].r-lt[u].l+1); }            Elselt[u].t=0; }        Else{Lt[u].op=1; if(b[i].r-lt[u].r+1<=t) {lt[u].t=min (t (B[I].R-LT[U].R), lt[u].r-lt[u].l+1); }            Elselt[u].t=0; }    }    Else if(lt[u].l<LT[U].R) {        intlef=u<<1,rig=u<<1|1; intMid= (LT[U].L+LT[U].R) >>1; if(lt[u].op>0)/*Descending*/{Lt[u].op=0; lt[lef].op=lt[rig].op=1; LT[RIG].T=min (lt[u].t,lt[rig].r-lt[rig].l+1); LT[LEF].T=lt[u].t-lt[rig].t; }        Else if(lt[u].op<0)/*Ascending*/{Lt[u].op=0; lt[lef].op=lt[rig].op=-1; LT[LEF].T=min (lt[u].t,lt[lef].r-lt[lef].l+1); LT[RIG].T=lt[u].t-lt[lef].t; }        if(b[i].l<=mid) cover (lef,i); if(b[i].r>mid) cover (rig,i); LT[U].T=lt[lef].t+lt[rig].t; }}inlineintAskintUintLintR) {    if(lt[u].l>=l&&lt[u].r<=R)returnlt[u].t; if(lt[u].l<LT[U].R) {        intlef=u<<1,rig=u<<1|1, ret=0; intMid= (LT[U].L+LT[U].R) >>1; if(lt[u].op>0)/*Descending*/{Lt[u].op=0; lt[lef].op=lt[rig].op=1; LT[RIG].T=min (lt[u].t,lt[rig].r-lt[rig].l+1); LT[LEF].T=lt[u].t-lt[rig].t; }        Else if(lt[u].op<0)/*Ascending*/{Lt[u].op=0; lt[lef].op=lt[rig].op=-1; LT[LEF].T=min (lt[u].t,lt[lef].r-lt[lef].l+1); LT[RIG].T=lt[u].t-lt[lef].t; }        if(L<=mid) ret+=Ask (LEF,L,R); if(R>mid) ret+=Ask (RIG,L,R); returnret; }}inlineBOOLChkintk) {Build (1,1, n,k);  for(intI=1; i<=m;++i) {T=ask (1, B[I].L,B[I].R); cover (1, i); }    return!ask (1, q,q);} Inlinevoidinit () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;++i) scanf ("%d",&A[i]);  for(intI=1; i<=m;++i) scanf ("%d%d%d",&b[i].op,&b[i].l,&B[I].R); scanf ("%d",&q); L=1; r=N;  while(l<R) {Mid= (l+r+1) >>1; if(Chk (mid)) l=mid; Elser=mid-1; } printf ("%d\n", L);}intMain () {Freopen ("sort.in","R", stdin); Freopen ("Sort.out","W", stdout);    Init ();    Fclose (stdin);    Fclose (stdout); return 0;}

[bzoj4552] [Tjoi2016] [Heoi2016] Sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.