P3391 【模板】文艺平衡树
//非旋转treap
#include<bits/stdc++.h>
using namespace std;
const int N=10e5+5;
int ch[N][2],tag[N],val[N],siz[N],key[N];
int tn,root,n,m;
inline void pushup(int x)
{
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}
inline void pushdown(int x)
{
if (x&&tag[x])
{
tag[x]^=1;
swap(ch[x][0],ch[x][1]);
if (ch[x][0]) tag[ch[x][0]]^=1;
if (ch[x][1]) tag[ch[x][1]]^=1;
}
}
inline int makenode(int x)
{
++tn;siz[tn]=1;val[tn]=x;key[tn]=rand();return tn;
}
inline int merge(int x,int y)
{
if(!x||!y) return x+y;
pushdown(x),pushdown(y);
if (key[x]<key[y])
{
ch[x][1]=merge(ch[x][1],y);
pushup(x);return x;
}
else
{
ch[y][0]=merge(x,ch[y][0]);
pushup(y);return y;
}
}
inline void split(int now,int k,int &x,int &y)
{
if (!now) x=y=0;
else{
pushdown(now);
if(k<=siz[ch[now][0]])
{
y=now;
split(ch[now][0],k,x,ch[now][0]);
}
else
{
x=now;
split(ch[now][1],k-siz[ch[now][0]]-1,ch[now][1],y);
}
pushup(now);
}
}
inline void rever(int l,int r)
{
int a,b,c,d;
split(root,r,a,b);
split(a,l-1,c,d);
tag[d]^=1;
root=merge(merge(c,d),b);
}
inline void Inorder(int x)
{
if (!x) return ;
pushdown(x);
Inorder(ch[x][0]);
cout<<val[x]<<" ";
Inorder(ch[x][1]);
}
void pre(int x)
{
if (!x) return;
cout<<x<<",left"<<ch[x][0]<<",rihgt"<<ch[x][1]<<",size"<<siz[x]<<",val"<<val[x]<<endl;
pre(ch[x][0]);
pre(ch[x][1]);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
root=merge(root,makenode(i));
}
//pre(root);
while(m--)
{
int a,b;
cin>>a>>b;
rever(a,b);
// Inorder(root);
// cout<<endl;
// pre(root);
}
Inorder(root);
}