Transmission Door
Time Limit:10 Sec Memory limit:128 MB
submit:2445 solved:1368
[Submit] [Status] [Discuss]
Description
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 Input
5 3
1 3
1 3
1 4
Sample Output
4 3 2) 1 5
HINT
n,m<=100000
Source
Balance Tree
#include <cstdio> #include <cstdlib> #include <climits> #include <iostream> using namespace std;
struct node{Node *ch[2];
int R,v,s;int b;
void Pushdown () {if (b) {B=0;swap (ch[0],ch[1]); ch[0]->b^=1;ch[1]->b^=1;}}
Node (int v,node *nl): V (v) {r=rand (); B=0;ch[0]=ch[1]=nl;s=1;} void maintain () {s=1;s+=ch[0]->s;s+=ch[1]->s;}}
*root,*null;
void Rotate (node* &o,int d) {Node *k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
O->maintain (); K->maintain (); o=k;
void Insert (node* &o,int x) {if (o==null) {o=new Node (x,null); return;}
Insert (O->CH[1],X);
if (o->ch[1]->r>o->r) rotate (o,0);
else O->maintain ();
} int CMPRK (node* o,int k) {if (o->ch[0]->s+1==k) return-1;
if (o->ch[0]->s>=k) return 0;else return 1;
} void Splay (node* &o,int k) {if (o==null) return;
O->pushdown ();
int D=CMPRK (O,K);
if (d==1) k-=o->ch[0]->s+1; if (D!=-1&&O->ch[d]!=null) {Node *p=o->ch[d];p->pushdown ();
int D2=CMPRK (P,K);
if (d2!=-1&&p->ch[d2]!=null) {int k2= (D2==0?K:K-P->CH[0]->S-1);
Splay (P->CH[D2],K2);
if (d==d2) rotate (o,d^1); else rotate (o->ch[d],d);
} rotate (o,d^1);
}} node *merge (node *left,node *right) {//merge splay (left,left->s);left->ch[1]=right;
Left->maintain (); return left; } void Split (Node *o,int k,node* &left,node* &right) {//split before K small in left rest in right if (k==0) {Left=null,right=o;retur
n;}
if (k==o->s) {Left=o,right=null;return;}
Splay (o,k);left=o;right=o->ch[1];
O->ch[1]=null;left->maintain ();
} void Init () {null=new Node (0,0);null->r=int_max;null->s=null->v=0;
null->ch[0]=null->ch[1]=null;root=null;
} void Print (Node *o) {if (o==null) return;
O->pushdown ();
Print (o->ch[0]);
if (o->r!=-888) printf ("%d", o->v); Print (O-> ch[1]); } int n,m,x,l,r;
Node *LL,*TMP,*MM,*RR;
int main () {scanf ("%d%d", &n,&m); init ();
Insert (root,0);root->r=-888;
for (int i=1;i<=n;i++) insert (root,i);
Print (Root);p Utchar (' \ n ');
while (m--) {scanf ("%d%d", &l,&r);
Split (ROOT,L,LL,RR);
Split (RR,R-L+1,MM,RR);
mm->b^=1;
Root=merge (merge (LL,MM), RR);
Print (Root);p Utchar (' \ n ');
} print (root);
return 0; }