LibreOJ2241-"cqoi2014" sequencing manipulator

Source: Internet
Author: User

Portal

Description

Given a \ ( n (n\leq10^5) \) number of sequences \ (\{a_n\}\), the sequence of \ (n\) operations. If the number in the \ (p_i\) position before the first \ ( i\) operation is i\ , the interval \ ([i,p_i]\)is flipped. After the n\ operation, the sequence becomes ascending. To find out each time \ (p_i\).

Solution

Splay.
The \ (a_i\) in the question will be repeated ... So first discretization of a wave, the same number by position ranking. Then, according to the initial position to build a splay, each time \ (i\) rotation to the root of the left subtree size can be, the interval reversal with the splay is also very good implementation. Note that flipping requires adding two sentinel nodes at the tail of the sequence.

Time complexity \ (O (NLOGN) \).

Code
//"cqoi2014" sorting manipulator#include <algorithm>#include <cstdio>usingStd::sort;inline CharGC () {Static Charnow[1<< -],*s,*t;if(s==t) {t= (S=now) +fread (now,1,1<< -, stdin);if(s==t)returnEOF;}return*s++;}inline intRead () {intx=0;CharCH=GC (); while(ch<' 0 '||' 9 '&LT;CH) CH=GC (); while(' 0 '<=ch&&ch<=' 9 ') x=x*Ten+ch-' 0 ', CH=GC ();returnx;}inline voidSwapint&x,int&y) {intT t=x,x=y,y=t;}Const intn=2e5+10;intN,a[n];intrt,fa[n],ch[n][2],siz[n];BOOLRev[n];intWhintP) {returnp==ch[fa[p]][1];}voidUpdateintP) {siz[p]=siz[ch[p][0]]+1+siz[ch[p][1]];}voidRever (intP) {rev[p]^=1; Swap (ch[p][0],ch[p][1]);}voidPUSHDW (intP) {if(Rev[p]) rever (ch[p][0]), Rever (ch[p][1]), rev[p]=false;}voidRotateintP) {intQ=FA[P],R=FA[Q],W=WH (P); Fa[p]=r;if(r) CH[R][WH (q)]=p; fa[ch[q][w]=ch[p][w^1]]=q; fa[ch[p][w^1]=q]=p; Update (q), update (p);}voidPushdwrt (intP) {if(Fa[p]) pushdwrt (fa[p]); PUSHDW (P);}voidSplay (intPint&k) {pushdwrt (P);intT=FA[K]; for(intQ=fa[p];q!=t;rotate (P), q=fa[p])if(fa[q]!=t) Rotate (WH (p) ^wh (q) p:q); Update (K=P);}voidBLDTR (int&p,intL0,intR0) {if(L0&GT;R0)return;intMid=l0+r0>>1;    P=a[mid]; BLDTR (ch[p][0],l0,mid-1), BLDTR (ch[p][1],mid+1, R0); fa[ch[p][0]]=fa[ch[p][1]]=p; Update (P);}intRnkintx) {intP=rt; while(true) {PUSHDW (P);if(x<=siz[ch[p][0]]) p=ch[p][0];Else if(x==siz[ch[p][0]]+1)returnPElsex-=siz[ch[p][0]]+1, p=ch[p][1]; }}voidSplitintXintY) {intP=RNK (x-1), Q=rnk (y+1); Splay (P,RT), splay (q,ch[rt][1]);}structrec{intX,id;} Tmp[n];BOOLCMPX (rec A,rec b) {returna.x==b.x?a.id<b.id:a.x<b.x;}voidDiscrete () { for(intI=1; i<=n;i++) tmp[i].x=a[i],tmp[i].id=i; Sort (tmp+1, Tmp+n+1, cmpx); for(intI=1; i<=n;i++) A[tmp[i].id+1]=i+1; a[1]=1, A[n+2]=n+2;}intMain () {n=read (); for(intI=1; i<=n;i++) A[i]=read (); Discrete (); BLDTR (RT,1N+2); for(intI=2; i<=n+1; i++) {splay (I,RT);intx=siz[ch[rt][0]]+1; printf"%d "X-1); Split (I,x), Rever (ch[ch[rt][1]][0]); }return 0;}

LibreOJ2241-"cqoi2014" sequencing manipulator

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.