Bzoj 3223:tyvj 1729 Literary Balance Tree

Source: Internet
Author: User

You need to write a data structure (which can refer to the title of the topic) to maintain an ordered series, which requires the following actions: Flipping an interval, such as the original ordered sequence is 5 4 3 2 1, the flip interval is [2,4], the result is 5 2 3 4 1

Input

The first behavior n,m n means the initial sequence has n number, this sequence is (1,2......n-1,n) m for the number of rollover operations
Next m line two numbers per line [L,r] Data guarantee 1<=l<=r<=n

Output

Outputs a row of n numbers, indicating the result of the original sequence after M-transform

Sample Input5 3

1 3

1 3

1 4

Sample Output4 3 2) 1 5

HINT



n,m<=100000

Source

Balance Tree

Analysis:

First write splay maintenance interval rollover operation ...

We can use the idea of the lazy tag of the line segment tree, if we want to flip an interval, mark the child node, and then delegate the tag every time we access the tagged node ...

For each node, its weight is the ordinal of the initial sequence, each flip interval [l,r], we first ranked L-1 node to the root node, so that the [l,+∞] are in the root node of the right subtree, and then we find the ranking of the R-1 node, and then go to the root node of the right son position , so that [L,r] is on the left son of the r-1 node, so we can just mark it ...

Code:
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5 //by Neighthorn6 using namespacestd;7 8 Const intmaxn=100000+5;9 Ten intn,m; One intTOT,ROOT,W[MAXN],LS[MAXN],RS[MAXN],FA[MAXN],SIZ[MAXN],LAZY[MAXN]; A  -InlinevoidZigintx) { -     inty=fa[x],tmp=Siz[y]; the     if(Rs[x]) -ls[y]=rs[x],fa[rs[x]]=y,siz[y]=siz[y]-siz[x]+siz[rs[x]],siz[x]=tmp; -     Else -ls[y]=0, siz[y]=siz[y]-siz[x],siz[x]=tmp; +fa[x]=Fa[y]; -     if(Fa[x]) { +         if(ls[fa[x]]==y) Als[fa[x]]=x; at         Else -rs[fa[x]]=x; -     } -fa[y]=x,rs[x]=y; - } -  inInlinevoidZagintx) { -     inty=fa[x],tmp=Siz[y]; to     if(Ls[x]) +rs[y]=ls[x],fa[ls[x]]=y,siz[y]=siz[y]-siz[x]+siz[ls[x]],siz[x]=tmp; -     Else thers[y]=0, siz[y]=siz[y]-siz[x],siz[x]=tmp; *fa[x]=Fa[y]; $     if(Fa[x]) {Panax Notoginseng         if(ls[fa[x]]==y) -ls[fa[x]]=x; the         Else +rs[fa[x]]=x; A     } thefa[y]=x,ls[x]=y; + } -  $InlinevoidSplay (intXintz) { $      while(fa[x]!=z) { -         inty=Fa[x]; -         if(fa[y]==z) { the             if(ls[y]==x) - Zig (x);Wuyi             Else the Zag (x); -         } Wu         Else{ -             if(ls[fa[y]]==y) { About                 if(ls[y]==x) $ Zig (y), zig (x); -                 Else - Zag (x), Zig (x); -             } A             Else{ +                 if(rs[y]==x) the Zag (y), zag (x); -                 Else $ Zig (x), Zag (x); the             } the         } the     } the     if(!z) -root=x; in } the  theInlinevoidInsintRtintx) { About     if(rt==0) thew[++tot]=x,siz[tot]=1, root=tot; the     Else if(x<W[rt]) { the         if(ls[rt]==0) +w[++tot]=x,siz[tot]=1, Fa[tot]=rt,siz[rt]++,ls[rt]=tot,splay (Tot,0); -         Else thesiz[rt]++, Ins (ls[rt],x);Bayi     } the     Else{ the         if(rs[rt]==0) -w[++tot]=x,siz[tot]=1, Siz[rt]++,fa[tot]=rt,rs[rt]=tot,splay (Tot,0); -         Else thesiz[rt]++, Ins (rs[rt],x); the     } the } the  -InlinevoidPush_down (intx) { thelazy[x]=0; lazy[ls[x]]^=1, lazy[rs[x]]^=1; the swap (ls[x],rs[x]); the }94  theInlineintFindintRtintx) { the     if(Lazy[rt]) the Push_down (RT);98     if(siz[ls[rt]]+1==x) { AboutSplay (RT,0);returnRT; -     }101     Else if(siz[ls[rt]]>=x)102         returnfind (ls[rt],x);103     Else104         returnFind (rs[rt],x-siz[ls[rt]]-1); the }106 107InlinevoidDfsintRT) {108     if(Lazy[rt])109 Push_down (RT); the     if(!RT)111         return; the     if(!Ls[rt])113printf"%d", W[rt]), DFS (Rs[rt]); the     Else theDFS (Ls[rt]), printf ("%d", W[rt]), DFS (Rs[rt]); the }117 118Signed Main (void){119memset (LS,0,sizeof(LS)); -memset (RS,0,sizeof(RS));121memset (FA,0,sizeof(FA));122memset (Siz,0,sizeof(siz));123memset (Lazy,0,sizeof(lazy));124scanf"%d%d", &n,&m); root=tot=0; the      for(intI=1; i<=n;i++)126 ins (root,i);127      for(intI=1, l,r;i<=m;i++){ -scanf"%d%d",&l,&R);129         if(l==1&&r==tot) thelazy[root]^=1;131         Else if(l==1) theR=find (root,r+1), Splay (R,0), lazy[ls[r]]^=1;133         Else if(r==tot)134L=find (root,l-1), Splay (L,0), lazy[rs[l]]^=1;135         Else136L=find (root,l-1), R=find (root,r+1), Splay (L,0), Splay (r,l), lazy[ls[r]]^=1;137     }138DFS (Root);p UTS ("");139     return 0; $}
View Code

by Neighthorn

Bzoj 3223:tyvj 1729 Literary Balance Tree

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.