Nuclear_Pasta @ 2023-07-23 15:43:41
#include<cstdio>
#include<cstdlib>
#include<utility>
using namespace std;
namespace FHQ_Treap{
struct FHQ_node{
int val;
int outp;
double rank;
bool flip;
int sz,min;
FHQ_node* lc,*rc;
FHQ_node(int _v,int _o,int _m){
val=_v;
outp=_o;
lc=rc=NULL;
int u=rand(),v=rand();
rank=(double)u+(double)v/178923767;
if(rand()%2==0)srand(rank);
flip=false;
sz=1;
min=_m;
}
};
FHQ_node* FHQ_upd(FHQ_node* cur){
int u=0,v=0;
if(cur->lc!=NULL)u=cur->lc->sz;
if(cur->rc!=NULL)v=cur->rc->sz;
if(cur->lc!=NULL)cur->min=cur->lc->min;
cur->val=cur->min+u;
cur->sz=u+v+1;
return cur;
}
FHQ_node* FHQ_gs(FHQ_node*cur){
int u=0;
if(cur->lc!=NULL)u=cur->lc->sz;
cur->val=cur->min+u;
return cur;
}
FHQ_node* FHQ_flip(FHQ_node* cur){
if(cur==NULL){
return NULL;
}
FHQ_node* tmp=cur->lc;
cur->lc=cur->rc;
cur->rc=tmp;
cur->val=cur->min;
if(cur->lc!=NULL){
cur->lc->min=cur->min;
cur->lc=FHQ_gs(cur->lc);
cur->lc->flip=!cur->lc->flip;
cur->val+=cur->lc->sz;
}
cur=FHQ_upd(cur);
if(cur->rc!=NULL){
cur->rc->min=cur->val+1;
cur->rc=FHQ_gs(cur->rc);
cur->rc->flip=!cur->rc->flip;
}
cur=FHQ_upd(cur);
cur->flip=false;
return cur;
}
pair<FHQ_node*,FHQ_node*> FHQ_spilt(FHQ_node* cur,int x){
FHQ_node *L=NULL,*R=NULL;
if(cur==NULL)return make_pair(L,R);
if(cur->flip){
cur=FHQ_flip(cur);
}
if(cur->val<x){
pair<FHQ_node*,FHQ_node*> p=FHQ_spilt(cur->rc,x);
L=p.first,R=p.second;
FHQ_node* o=cur;
o->rc=L;
L=o;
L=FHQ_upd(L);
}
else{
pair<FHQ_node*,FHQ_node*> p=FHQ_spilt(cur->lc,x);
L=p.first,R=p.second;
cur->lc=R;
R=cur;
R=FHQ_upd(R);
}
return make_pair(L,R);
}
FHQ_node* FHQ_mrg(FHQ_node*X,FHQ_node*Y){
FHQ_node* ret;
if(X==NULL||Y==NULL){
ret=X==NULL?Y:X;
if(ret!=NULL){
if(ret->flip)FHQ_flip(ret);
FHQ_upd(ret);
}
return ret;
}
if(X->flip){
X=FHQ_flip(X);
}
if(Y->flip){
Y=FHQ_flip(Y);
}
if(X->rank>Y->rank){
FHQ_node *tmp=FHQ_mrg(X->rc,Y);
X->rc=tmp;
ret=X;
ret=FHQ_upd(ret);
return ret;
}
else{
FHQ_node*tmp=FHQ_mrg(X,Y->lc);
Y->lc=tmp;
ret=Y;
ret=FHQ_upd(ret);
return ret;
}
}
FHQ_node* FHQ_del(FHQ_node*cur,int val){
FHQ_node*X=NULL,*Y=NULL,*Z=NULL,*S=NULL;
pair<FHQ_node*,FHQ_node*> k=FHQ_spilt(cur,val);
X=k.first,Y=k.second;
k=FHQ_spilt(Y,val+1);
Z=k.first,S=k.second;
Y=FHQ_mrg(Z->lc,Z->rc);
Z=FHQ_mrg(Y,S);
cur=FHQ_mrg(X,Z);
return cur;
}
void FHQ_show(FHQ_node*cur){
if(cur==NULL)return;
if(cur->flip)FHQ_flip(cur);
FHQ_show(cur->lc);
//printf(" %d %d\n",cur->val,cur->outp);
printf("%d ",cur->outp);
FHQ_show(cur->rc);
}
FHQ_node* FHQ_add(FHQ_node*cur,int val){
FHQ_node*X=NULL,*Y=NULL,*Z=NULL,*S=NULL;
pair<FHQ_node*,FHQ_node*>p=FHQ_spilt(cur,val);
X=p.first,Y=p.second;
Z=new FHQ_node(val,val,val);
S=FHQ_mrg(Z,Y);
cur=FHQ_mrg(X,S);
return cur;
}
};
using namespace FHQ_Treap;
int n,m,l,r;
int main(){
FHQ_node*p=NULL;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
p=FHQ_add(p,i);
}
//FHQ_show(p);
for(int i=1;i<=m;i++){
scanf("%d%d",&l,&r);
FHQ_node*x=NULL,*y=NULL,*z=NULL,*d=NULL;
pair<FHQ_node*,FHQ_node*>k=FHQ_spilt(p,r+1);
x=k.first,y=k.second;
k=FHQ_spilt(x,l);
z=k.first,d=k.second;
/*printf("z:\n");
FHQ_show(z);
printf("d:\n");
FHQ_show(d);
printf("Y:\n");
FHQ_show(y);*/
FHQ_flip(d);
/*printf("d:\n");
FHQ_show(d);*/
x=FHQ_mrg(z,d);
p=FHQ_mrg(x,y);
/*printf("P:\n");
FHQ_show(p);
printf("\n");*/
}
FHQ_show(p);
return 0;
}