裸的区间翻转..直接写个splay就行...
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <memory.h>
#include <string>
#include <cstring>
using namespace std;
const int maxn=130030;
int pre[maxn],ch[maxn][2],size[maxn],key[maxn];
bool flip[maxn];
int n,m,tot,root;
struct splytree
{
void pushup(int r)
{
if (r==0) return;
size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
}
void pushdown(int r)
{
if (flip[r])
{
go_f(ch[r][0]);
go_f(ch[r][1]);
flip[r]=0;
}
}
void go_f(int r)
{
if (!r) return;
flip[r]^=1;
swap(ch[r][0],ch[r][1]);
}
void rotate(int x,int kind)
{
int y=pre[x];
pushdown(y);
pushdown(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if (pre[y])
{
ch[pre[y]][ch[pre[y]][1]==y]=x;
}
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
pushup(x);
}
void splay(int x,int tgt)
{
pushdown(x);
while (pre[x]!=tgt)
{
int y=pre[x];
pushdown(pre[y]);
pushdown(y);
pushdown(x);
if (pre[pre[x]]==tgt)
{
rotate(x,ch[pre[x]][0]==x);
}
else
{
int kind=ch[pre[y]][0]==y;
if (ch[y][kind]==x)
{
rotate(x,kind^1);
rotate(x,kind);
}
else
{
rotate(y,kind);
rotate(x,kind);
}
}
}
pushup(x);
if (tgt==0) root=x;
}
void select(int k,int tgt)
{
int rt=root;
pushdown(rt);
while (true)
{
if(k<=size[ch[rt][0]]) rt=ch[rt][0];
else if (k==size[ch[rt][0]]+1) break;
else k-=(size[ch[rt][0]]+1),rt=ch[rt][1];
pushdown(rt);
}
splay(rt,tgt);
}
void newnode(int &r,int father,int k)
{
r=++tot;
pre[r]=father;
size[r]=1;
flip[r]=0;
key[r]=k;
ch[r][0]=ch[r][1]=0;
}
void build(int l,int r,int &x,int rt)
{
if (l>r) return;
int m=(l+r)>>1;
newnode(x,rt,m);
build(l,m-1,ch[x][0],x);
build(m+1,r,ch[x][1],x);
pushup(x);
}
void init()
{
tot=root=0;
newnode(root,0,-1);
newnode(ch[root][1],root,-1);
build(1,n,ch[ch[root][1]][0],ch[root][1]);
pushup(ch[root][1]);
pushup(root);
}
void reverse(int posi,int num)
{
select(posi,0);
select(posi+num+1,root);
go_f(ch[ch[root][1]][0]);
// int k=ch[ch[root][1]][0];
// splay(k,0);
}
void print(int r)
{
if (r==0) return;
pushdown(r);
print(ch[r][0]);
if (key[r]!=-1) printf("%d ",key[r]);
print(ch[r][1]);
}
}spt;
char tt[20],tt2[20];
int x,y;
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
spt.init();
for (int i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
spt.reverse(x,y-x+1);
}
spt.print(root);
puts("");
return 0;
}