# bzoj3223Tyvj 1729 文艺平衡树 splay

## 3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 5644  Solved: 3362
[Submit][Status][Discuss]

5 3

1 3

1 3

1 4

4 3 2 1 5

## HINT

N,M<=100000

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100050
using namespace std;
int n,m,rt,id[N],fa[N],ch[N][2],rev[N],siz[N];

void pushup(int u){
int l=ch[u][0],r=ch[u][1];
siz[u]=siz[l]+siz[r]+1;
}
void pushdown(int u){
if(!rev[u])return;
int l=ch[u][0],r=ch[u][1];
swap(ch[u][0],ch[u][1]);
rev[l]^=1;rev[r]^=1;
rev[u]=0;
}
void rotate(int x,int &k){
int y=fa[x],z=fa[y],l,r;
l= ch[y][1]==x;r=l^1;
if(y==k)k=x;
else ch[z][ch[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;
ch[y][l]=ch[x][r];ch[x][r]=y;
pushup(y);pushup(x);
}

void splay(int x,int &k){
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if((ch[y][0]==x)^(ch[z][0]==y))rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
void build(int l,int r,int f){
if(l>r)return;
int now=id[l],ls=id[f];
if(l==r){
fa[now]=ls;siz[now]=1;
ch[ls][l>f]=now;return;
}
int mid=(l+r)>>1;now=id[mid];
build(l,mid-1,now);build(mid+1,r,now);
fa[now]=ls;pushup(now);
ch[ls][now>ls]=now;
}
int find(int u,int rk){
pushdown(u);
int l=ch[u][0],r=ch[u][1];
if(siz[l]+1==rk)return u;
else if(siz[l]>=rk)return find(l,rk);
return find(r,rk-siz[l]-1);
}
void rever(int l,int r){
int x=find(rt,l),y=find(rt,r+2);
splay(x,rt);splay(y,ch[x][1]);
rev[ch[y][0]]^=1;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n+2;i++)id[i]=i;
build(1,n+2,0);rt=(n+3)>>1;
int l,r;
while(m--){
scanf("%d%d",&l,&r);
rever(l,r);
}
for(int i=2;i<=n+1;i++)
printf("%d ",find(rt,i)-1);
return 0;
}


If you live in the echo,
your heart never beats as loud.

posted @ 2017-12-23 11:07  _wsy  阅读(114)  评论(0编辑  收藏  举报