搜索
您的当前位置:首页正文

bzoj3282(Tree lct模板)

来源:独旅网


#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#define m(a,b) memset(a,b,sizeof a)
using namespace std;
const int N=3e5+5;
int f[N],ch[N][2],val[N],sum[N],st[N],tag[N];
map<int,int>mp[N];
inline void pushup(int x){
    sum[x]=sum[ch[x][0]]^sum[ch[x][1]]^val[x];
}
inline void pushdown(int x){
    if(!tag[x]) return;
    swap(ch[ch[x][0]][0],ch[ch[x][0]][1]),tag[ch[x][0]]^=1;
    swap(ch[ch[x][1]][0],ch[ch[x][1]][1]),tag[ch[x][1]]^=1;
    tag[x]=0;
}
inline int notroot(int x){//不是当前所在splay的根
    return ch[f[x]][0]==x||ch[f[x]][1]==x;
}
inline void route(int x){
    int y=f[x],z=f[y],w=(ch[y][1]==x);
    if(notroot(y)) ch[z][ch[z][1]==y]=x;//先这句话!!!
    f[x]=z;
    ch[y][w]=ch[x][w^1];if(ch[y][w]) f[ch[y][w]]=y;
    ch[x][w^1]=y,f[y]=x;
    pushup(y),pushup(x);
}
inline void splay(int x){
    int y=x,num=0;st[++num]=y;
    while(notroot(y)) st[++num]=(y=f[y]);
    while(num) pushdown(st[num--]);
    if(!notroot(x)) return;
    for(int fa=f[x];notroot(x);route(x),fa=f[x])
        if(notroot(fa)) ((ch[fa][1]==x)^(ch[f[fa]][1]==fa))?route(x):route(fa);
}
inline void access(int x){
    for(int y=0;x;x=f[y=x])
        splay(x),ch[x][1]=y,pushup(x);//一定要有pushup 
}
inline void makeroot(int x){
    access(x),splay(x);
    swap(ch[x][0],ch[x][1]),tag[x]^=1;
}
inline int findroot(int x){//整颗联通树的根
    access(x),splay(x);
    while(ch[x][0]) pushdown(x),x=ch[x][0];
    splay(x);
    return x;
}
inline void split(int x,int y){//假如x-y这条边存在且处于一个splay中,那access(y)时 y就成了这颗splay的根哇。
    makeroot(x);access(y),splay(y);//个人觉得需要以y为根把。。因为access不小心就splay成了根?? 
}
inline void link(int x,int y){
    makeroot(x);//将x作为这颗联通树的根 而且也是这颗splay的root
    if(findroot(y)!=x) f[x]=y;//将两颗联通树连起来
}
inline void cut(int x,int y){
    if(!mp[x][y]) return;
    makeroot(x);//x为根了 x就是这棵树深度最小的了
    if(findroot(y)==x&&f[y]==x&&!ch[y][0]&&!ch[y][1])
        f[y]=ch[x][1]=0,pushup(x);
}
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i) scanf("%d",&val[i]);
    while(m--){
        int f,x,y;scanf("%d%d%d",&f,&x,&y);
        switch(f){
            case 0:split(x,y);printf("%d\n",sum[y]);break;
            case 1:link(x,y);mp[x][y]=mp[y][x]=1;break;
            case 2:cut(x,y);break;
            case 3:splay(x);val[x]=y;//一定要将x置于其所在splay的root处,觉着直接这样就行了呀,反正当前这个splay的信息都是对的,而且我们的sum维护的当然没有跨树的呀
        }
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

热门图文

Top