[BZOJ1180][CROATIAN2009][LCT]OTOCI

LCT裸题吧….就是没有cut而已

#include <cstdio>
#define N 50010

int n,m;
int v[N],f[N],ch[N][2],rev[N],fa[N],t[N],sta[N],tp;
char op[20];

inline char C(){
    static char buf[100000],*p1=buf,*p2=buf;
    if(p1==p2){
        p2=(p1=buf)+fread(buf,1,100000,stdin);
        if(p1==p2) return EOF;
    }
    return *p1++;
}

inline void reaD(int &x){
    char Ch=C();x=0;
    for(;Ch>'9'||Ch<'0';Ch=C());
    for(;Ch>='0'&&Ch<='9';x=x*10+Ch-'0',Ch=C());
}

inline void reaD(char *x){
    char Ch=C();int len=0;
    for(;Ch>'z'||Ch<'a';Ch=C());
    for(;Ch>='a'&&Ch<='z';x[++len]=Ch,Ch=C());
}

int fifa(int x){return x==fa[x]?x:fa[x]=fifa(fa[x]);}

inline void swap(int &x,int &y){int z=x;x=y;y=z;}

inline int isr(int x){return ch[f[x]][0]!=x&&ch[f[x]][1]!=x;}
inline int isl(int x){return ch[f[x]][1]==x;}

inline void pushdown(int x){
    if(!(x&&rev[x])) return;
    swap(ch[x][0],ch[x][1]);
    if(ch[x][0]) rev[ch[x][0]]^=1;
    if(ch[x][1]) rev[ch[x][1]]^=1;
    rev[x]=0;
}

inline void upd(int x){
    t[x]=v[x];
    if(ch[x][0]) t[x]+=t[ch[x][0]];
    if(ch[x][1]) t[x]+=t[ch[x][1]];
}

inline void rot(int x){
    int y=f[x],z=f[y],lor=isl(x);
    if(!isr(y)) ch[z][ch[z][1]==y]=x;
    f[x]=z;ch[y][lor]=ch[x][lor^1];
    if(ch[y][lor]) f[ch[y][lor]]=y;
    ch[x][lor^1]=y;f[y]=x;
    upd(y);upd(x);
}

inline void splay(int x){
    sta[tp=1]=x;
    for(int i=x;!isr(i);i=f[i]) sta[++tp]=f[i];
    for(;tp;tp--) pushdown(sta[tp]);
    for(;!isr(x);rot(x))if(!isr(f[x]))
        if(isl(x)^isl(f[x])) rot(x); else rot(f[x]);
}

inline void access(int x){int t=0;for(;x;t=x,x=f[x])splay(x),ch[x][1]=t,upd(x);}
inline void reverse(int x){access(x);splay(x);rev[x]^=1;}
inline int query(int x,int y){reverse(x);access(y);splay(y);return t[y];}
inline void link(int x,int y){reverse(x);f[x]=y;splay(x);}
inline void change(int x,int y){reverse(x);v[x]=y;upd(x);}

int w[20],wt;

inline void Pt(int x){
    if(!x){putchar(48);putchar('\n');return;}
    while(x)w[++wt]=x%10,x/=10;
    for(;wt;wt--)putchar(48+w[wt]);putchar('\n');
}

int main(){
     reaD(n);
     for(int i=1;i<=n;i++) reaD(v[i]),fa[i]=i;
     reaD(m);
     for(int i=1,x,y;i<=m;i++){
        reaD(op);reaD(x);reaD(y);
        if(op[1]=='e')
            if(fifa(x)==fifa(y)) Pt(query(x,y)); else puts("impossible");
        else if(op[1]=='b')
            if(fifa(x)==fifa(y)) puts("no"); else puts("yes"),link(x,y),fa[fifa(x)]=fifa(y);
        else change(x,y);
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值