hdu 3487

splay

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 300009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std; int son[maxn][],fa[maxn],size[maxn],val[maxn],st[maxn];
int root,cnt;
int num[maxn],flg[maxn];
int n,m; void push_up(int rt)
{
size[rt]=size[lch(rt)]+size[rch(rt)]+;
} void push_down(int rt)
{
if(flg[rt])
{
int tmp=rch(rt);
rch(rt)=lch(rt);
lch(rt)=tmp;
if(rch(rt))
flg[rch(rt)]^=;
if(lch(rt))
flg[lch(rt)]^=;
flg[rt]=;
}
} void newnode(int &rt,int f,int v)
{
rt=++cnt;
rch(rt)=lch(rt)=;
fa[rt]=f;
val[rt]=v;
size[rt]=;
flg[rt]=;
} void build(int l,int r,int &rt,int f)
{
if(l>r)return;
int m=(l+r)>>;
newnode(rt,f,num[m]);
build(l,m-,lch(rt),rt);
build(m+,r,rch(rt),rt);
push_up(rt);
} void ini()
{
lch()=rch()=;
fa[]=size[]=val[]=;
root=;
cnt=;
newnode(root,,);
newnode(rch(root),root,n+);
build(,n,lch(rch(root)),rch(root));
push_up(rch(root));
push_up(root);
} void rotate(int x,int kind)//0->left,1->right
{
push_down(x);
int y=fa[x];
son[y][kind^]=son[x][kind];
fa[son[x][kind]]=y;
if(fa[y])
son[fa[y]][son[fa[y]][]==y]=x;
fa[x]=fa[y];
son[x][kind]=y;
fa[y]=x;
push_up(y);
} void splay(int rt,int goal)//将rt节点旋转到goal的右子节点
{
if(rt!=goal)
{
push_down(rt);
while(fa[rt]!=goal)
{
if(lch(fa[rt])==rt)
rotate(rt,);
else rotate(rt,);
}
push_up(rt);
if(!goal)root=rt;
}
} int select(int k)
{
int rt=root;
push_down(rt);
while(size[lch(rt)]+!=k)
{
if(size[lch(rt)]+>=k)
rt=lch(rt);
else
{
k-=(size[lch(rt)]+);
rt=rch(rt);
}
push_down(rt);//不加就超时;
}
return rt;
} void cut(int a,int b,int c)
{
a=select(a-);
splay(a,);
b=select(b+);
splay(b,a);
int res=lch(b);
lch(b)=;
push_up(b);push_up(a);
a=select(c);b=select(c+);
splay(a,);splay(b,a);
lch(b)=res;
fa[res]=b;
} void flip(int a,int b)
{
a=select(a-);
splay(a,);
b=select(b+);
splay(b,a);
flg[lch(b)]^=;
} int cot=;
void dfs(int rt)
{
push_down(rt);
if(lch(rt))
dfs(lch(rt));
if(val[rt]>&&val[rt]<=n&&cot<n)
{
printf("%d ",val[rt]);
cot++;
}
else if(val[rt]>&&val[rt]<=n&&cot==n)
{
printf("%d",val[rt]);
}
if(rch(rt))
dfs(rch(rt));
} char s[]; int main()
{
for(int i=; i<maxn; i++)
num[i]=i;
while(scanf("%d%d",&n,&m)&&n>)
{ ini();
int a,b,c;
while(m--)
{
scanf("%s",s);
if(s[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
cut(a+,b+,c+);
}
else
{
scanf("%d%d",&a,&b);
flip(a+,b+);
}
}
cot=;
a=select();b=select(n+);
splay(a,);splay(b,a);
dfs(root);
puts("");
}
return ;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 300009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std; int son[maxn][],fa[maxn],size[maxn],val[maxn],st[maxn];
int root,cnt;
int num[maxn],flg[maxn];
int n,m; void push_up(int rt)
{
size[rt]=size[lch(rt)]+size[rch(rt)]+;
} void push_down(int rt)
{
if(flg[rt])
{
int tmp=rch(rt);
rch(rt)=lch(rt);
lch(rt)=tmp;
flg[rch(rt)]^=;
flg[lch(rt)]^=;
flg[rt]=;
}
} void newnode(int &rt,int f,int v)
{
rt=++cnt;
rch(rt)=lch(rt)=;
fa[rt]=f;
val[rt]=v;
size[rt]=;
flg[rt]=;
} void build(int l,int r,int &rt,int f)
{
if(l>r)return;
int m=(l+r)>>;
newnode(rt,f,num[m]);
build(l,m-,lch(rt),rt);
build(m+,r,rch(rt),rt);
push_up(rt);
} void rotate(int x,int kind)//0->left,1->right
{
push_down(x);
int y=fa[x];
son[y][kind^]=son[x][kind];
fa[son[x][kind]]=y;
if(fa[y])
son[fa[y]][son[fa[y]][]==y]=x;
fa[x]=fa[y];
son[x][kind]=y;
fa[y]=x;
push_up(y);
} void splay(int rt,int goal)//将rt节点旋转到goal的右子节点
{
push_down(rt);
while(fa[rt]!=goal)
{
int y=fa[rt];
if(fa[y]==goal)
rotate(rt,son[y][]==rt);
else
{
int kind=son[fa[y]][]==y;
if(son[y][kind]==rt)
{
rotate(rt,kind^);
rotate(rt,kind);
}
else
{
rotate(y,kind);
rotate(rt,kind);
}
}
}
push_up(rt);
if(goal==) root=rt;
} int select(int k)
{
int rt=root;
while(size[lch(rt)]!=k)
{
if(size[lch(rt)]>k)
rt=lch(rt);
else
{
k-=(size[lch(rt)]+);
rt=rch(rt);
}
push_down(rt);
}
return rt;
} void rotate_to(int k,int goal)//将第k节点旋转到goal的右儿子节点;
{
int rt=root;
rt=select(k);
splay(rt,goal);
} void cut(int a,int b,int c)
{
rotate_to(a-,);
rotate_to(b+,root);
int x=rch(root);
int tmp=lch(x);
lch(x)=;
push_up(x);
push_up(root);
rotate_to(c,);
rotate_to(c+,root);
fa[tmp]=rch(root);
lch(rch(root))=tmp;
push_up(rch(root));
push_up(root);
} void flip(int a,int b)
{
rotate_to(a-,);
rotate_to(b+,root);
flg[lch(rch(root))]^=;
} void dfs(int rt)
{
push_down(rt);
if(lch(rt))
dfs(lch(rt));
if(val[rt]!=)
printf(" %d",val[rt]);
if(rch(rt))
dfs(rch(rt));
} void ini()
{
lch()=rch()=;
fa[]=size[]=;
root=;
cnt=;
newnode(root,,);
newnode(rch(root),root,);
push_up(root);
build(,n,lch(rch(root)),rch(root));
push_up(rch(root));
push_up(root);
} char s[]; int main()
{
while(scanf("%d%d",&n,&m)&&n>)
{
for(int i=; i<=n; i++)
num[i]=i;
ini();
int a,b,c;
while(m--)
{
scanf("%s",s);
if(s[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
cut(a,b,c);
// rotate_to(1,0);
// printf("%d",val[root]);
// dfs(rch(root));
// puts("");
}
else
{
scanf("%d%d",&a,&b);
flip(a,b);
}
}
rotate_to(,);
printf("%d",val[root]);
dfs(rch(root));
puts("");
}
return ;
}
上一篇:python 代码片段13


下一篇:CWnd与HWND的区别与转换