[bzoj2959] 长跑

[bzoj2959] 长跑


用LCT维护双联通分量。

手贱把pushnow写rotate上,,,我还能说什么

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int par[N];
inline int find(int x){
    return par[x]==x?par[x]:par[x]=find(par[x]);
}
struct Splay{
    int ch[2],fa,sonsz,sz;bool rev;
}t[N];
int cnt;
int newnode(int _val){++cnt;t[cnt].sonsz=t[cnt].sz=_val;return cnt;}
bool son(int x){return t[t[x].fa].ch[1]==x;}
bool isroot(int x){return t[t[x].fa].ch[0]!=x&&t[t[x].fa].ch[1]!=x;}
inline void pushdown(int x){
    if(t[x].rev){
        swap(t[x].ch[1],t[x].ch[0]); 
        if(t[x].ch[1])t[t[x].ch[1]].rev^=1;
        if(t[x].ch[0])t[t[x].ch[0]].rev^=1;
        t[x].rev=0;
    }
}
inline void pushnow(int x){
    if(!isroot(x))pushnow(t[x].fa);
    else t[x].fa=find(t[x].fa);
    pushdown(x);
}
inline void pushup(int x){
    t[x].sonsz=t[x].sz;
    if(t[x].ch[0])t[x].sonsz+=t[t[x].ch[0]].sonsz;
    if(t[x].ch[1])t[x].sonsz+=t[t[x].ch[1]].sonsz;
}
inline void rotate(int x){
    int f=t[x].fa,g=t[t[x].fa].fa;
    bool a=son(x),b=son(x)^1;
    if(!isroot(f))t[g].ch[son(f)]=x;
    t[x].fa=g;
    t[t[x].ch[b]].fa=f;t[f].ch[a]=t[x].ch[b];
    t[x].ch[b]=f;t[f].fa=x;
    pushup(f);pushup(x);
}
inline void splay(int x){
    pushnow(x);
    while(!isroot(x)){
        int f=t[x].fa;
        if(!isroot(f)){
            if(son(x)^son(f))rotate(x);
            else rotate(f);
        }
        rotate(x);
    }
}
void access(int x){
    int tmp=0;
    do{
        splay(x);
        t[x].ch[1]=tmp;
        pushup(x);
        tmp=x;x=t[x].fa;
    }while(x);
}

inline void makeroot(int x){
    access(x);
    splay(x);
    t[x].rev^=1;
}

inline void link(int x,int y){
    makeroot(x);
    access(y);
    t[y].ch[1]=x,t[x].fa=y;
    pushup(y);
    splay(y);
}

void dfs(int x,int tar){
    if(t[x].ch[0]){
        dfs(t[x].ch[0],tar);
        par[t[x].ch[0]]=tar;
    }if(t[x].ch[1]){
        dfs(t[x].ch[1],tar);
        par[t[x].ch[1]]=tar;
    }
}
inline void operate(int x,int y){//保证存在
    makeroot(x);
    access(y);
    splay(x);
    dfs(x,x);
    t[x].ch[0]=0;
    t[x].sz=t[x].sonsz;
    pushup(x);
}
int n,m;
int par_link[N];
inline int find_link(int x){
    return par_link[x]==x?x:par_link[x]=find_link(par_link[x]);
}
inline int read(){
    char x=0;int res=0;
    while(x<'0'||x>'9')x=getchar();
    while(x<='9'&&x>='0'){res=res*10+x-'0';x=getchar();}
    return res;
}

int val[N];
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        par[i]=par_link[i]=i;
    }
    for(int i=1;i<=n;i++){
        val[i]=read();
        newnode(val[i]);
    }
    while(m--){
        int u,v,opt=0;
        opt=read();u=read();v=read();
        if(opt==1){
            int uu=find(u),vv=find(v);
            int x=find_link(u),y=find_link(v);
            if(x^y){
                par_link[x]=y;
                link(uu,vv);
            }else{
                operate(uu,vv);
            }
        }else if(opt==2){
            int uu=find(u);
            splay(uu);
            t[uu].sz+=v-val[u];
            val[u]=v;
            pushup(uu);
        }else{
            int uu=find(u),vv=find(v);
            int x=find_link(u),y=find_link(v);
            if(x^y){
                puts("-1");
                continue;
            }else{
                makeroot(uu);
                access(vv);
                splay(vv);
                printf("%d\n",t[vv].sonsz);
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值