题意:给n个数,1到n,然后每个操作a,b是将a到b的所有数翻转后添加到末尾
思路:白书的例题,代码参考这位大神的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=1e5+10;
struct Node{
Node *ch[2];
int v,s,flip;
Node(int v):v(v){ch[0]=ch[1]=NULL;s=1;flip=0;}
int cmp(int x) const{
int d=x-(ch[0]==NULL?0:ch[0]->s);
if(d==1) return -1;
return d<=0?0:1;
}
void maintain(){
s=1;if(ch[0]!=NULL) s+=ch[0]->s;
if(ch[1]!=NULL) s+=ch[1]->s;
}
void pushdown(){
if(flip){
flip=0;
swap(ch[0],ch[1]);
if(ch[0]!=NULL) ch[0]->flip=!ch[0]->flip;
if(ch[1]!=NULL) ch[1]->flip=!ch[1]->flip;
}
}
};
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 build(Node* &o,int l,int r){
if(l>r) return ;
int mid=(l+r)>>1;
o=new Node(mid);
build(o->ch[0],l,mid-1);
build(o->ch[1],mid+1,r);
o->maintain();
}
void pri(Node* &o){
o->pushdown();
if(o->ch[0]!=NULL) pri(o->ch[0]);
if(o->v) printf("%d\n",o->v);
if(o->ch[1]!=NULL) pri(o->ch[1]);
}
void removetree(Node* &x){
if(x->ch[0]!=NULL) removetree(x->ch[0]);
if(x->ch[1]!=NULL) removetree(x->ch[1]);
delete x;x=NULL;
}
void splay(Node* &o,int k){
o->pushdown();
int d=o->cmp(k);
if(d==1) k-=(o->ch[0]==NULL?0:o->ch[0]->s)+1;
if(d!=-1){
Node* p=o->ch[d];
p->pushdown();
int d2=p->cmp(k);
int k2=(d2==0?k:k-(p->ch[0]==NULL?0:p->ch[0]->s)-1);
if(d2!=-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* L,Node* R){
splay(L,L->s);
L->ch[1]=R;
L->maintain();
return L;
}
void split(Node* o,int k,Node* &L,Node* &R){
splay(o,k);
L=o;R=o->ch[1];
o->ch[1]=NULL;
L->maintain();
}
void debug(Node* r){
if (r==NULL)return;
printf("key:%d lson:%d rson:%d\n",r->v,r->ch[0]->v,r->ch[1]->v);
debug(r->ch[0]);
debug(r->ch[1]);
}
int main(){
int n,m,x,y;
while(scanf("%d%d",&n,&m)!=-1){
Node *root=NULL;
Node *l,*r,*mid,*o;
build(root,0,n);
while(m--){
scanf("%d%d",&x,&y);
split(root,x,l,o);
split(o,y-x+1,mid,r);
mid->flip^=1;
root=Merge(Merge(l,r),mid);
}
pri(root);
removetree(root);
}
return 0;
}