# [BZOJ3223/Tyvj1729]文艺平衡树

Description

Input

Output

Sample Input
5 3
1 3
1 3
1 4

Sample Output
4 3 2 1 5

splay区间操作裸题，区间操作方法请见浅谈算法——splay

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
const int N=1e5;
int n,m;
struct Splay{
#define T(x) (tree[f[x]][1]==x)
int tree[N+10][2],f[N+10],size[N+10];
bool flag[N+10];
int len,root;
void updata(int x){size[x]=size[tree[x][0]]+size[tree[x][1]]+1;}
void build(int x){
root=x+1;
for (int i=1;i<=x;i++)	f[i]=i+1,size[i]=i+1,tree[i+1][0]=i;
tree[1][0]=x+2,f[x+2]=1,size[x+2]=1;
size[x+1]=x+2;
}
void move(int x){
int fa=f[x],son=tree[x][T(x)^1];
tree[x][T(x)^1]=fa;
tree[fa][T(x)]=son;
if (son)	f[son]=fa;
f[x]=f[fa];
if (f[x])	tree[f[x]][T(fa)]=x;
f[fa]=x;
updata(fa),updata(x);
}
void splay(int x){
while (f[x]){
if (f[f[x]])	T(x)==T(f[x])?move(f[x]):move(x);
move(x);
}
root=x;
}
void pushdown(int x){
if (!flag[x])	return;
swap(tree[x][0],tree[x][1]);
flag[tree[x][0]]^=1;
flag[tree[x][1]]^=1;
flag[x]=0;
}
int find(int x,int i){
pushdown(i);
if (size[tree[i][0]]+1==x)	return i;
if (x<=size[tree[i][0]])	return find(x,tree[i][0]);
return find(x-size[tree[i][0]]-1,tree[i][1]);
}
void print(int x){
if (!x)	return;
pushdown(x);
print(tree[x][0]);
if (x<=n)	printf("%d ",x);
print(tree[x][1]);
}
void work(){
x=find(x,root),splay(x);
y=find(y+2,root),splay(y);
if (f[x]!=root)	move(x);
flag[tree[x][1]]^=1;
}
}T;
int main(){