BZOJ1180: [CROATIAN2009]OTOCI

题目链接

LCT裸题。

【代码】

 #include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#define N 30005
#define INF 1e9
using namespace std;
typedef unsigned int ll;
typedef pair<ll,ll> pa;

int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
}

int n,m,top;
int son[N][2],st[N],fa[N],sum[N],a[N];
bool rev[N];

void Input_Init()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();  
} 

void pushup(int k){
    sum[k]=sum[son[k][0]]+sum[son[k][1]]+a[k];
}

void pushdown(int k)
{
    if(!rev[k]) return;
    rev[k]=0;rev[son[k][0]]^=1;rev[son[k][1]]^=1;
    swap(son[k][0],son[k][1]);  
}

bool isroot(int x){
    return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}

void Rotate(int x)
{
    int y=fa[x],z=fa[y],l,r;
    l=son[y][1]==x;r=l^1;
    if(!isroot(y)) son[z][son[z][1]==y]=x;
    fa[x]=z;fa[y]=x;fa[son[x][r]]=y;
    son[y][l]=son[x][r];son[x][r]=y;
    pushup(y);pushup(x);    
}

void Splay(int x)
{
    st[++top]=x;
    for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
    while(top) pushdown(st[top--]);
    while(!isroot(x))
    {
        int y=fa[x],z=fa[y];
        if(!isroot(y)) {
            if(son[z][0]==y^son[y][0]==x) Rotate(x);
            Rotate(y);
        }
        Rotate(x);
    }
}

void Access(int x){
    for(int t=0;x;t=x,x=fa[x])
        Splay(x),son[x][1]=t,pushup(x);
}

void Make_Root(int x){
    Access(x);Splay(x);rev[x]^=1;
}

void Link(int x,int y){
    Make_Root(x);fa[x]=y;
}

void Split(int x,int y){
    Make_Root(x);Access(y);Splay(y);
}

int Find(int x){
    Access(x);Splay(x);
    while(son[x][0]) x=son[x][0];
    return x;
}

void Solve_Link(int x,int y){
    if(Find(x)==Find(y)) {
        printf("no\n");
        return;
    }
    printf("yes\n");Link(x,y);
}

void Solve_Change(int x,int y){
    Splay(x);a[x]=y;
}

void Solve_Sum(int x,int y){
    if(Find(x)!=Find(y)) {
        printf("impossible\n");
        return;
    }
    Split(x,y);printf("%d\n",sum[y]);
}

void Solve()
{
    m=read();
    while(m--)
    {
        char ch[10];static int x,y;
        scanf("%s",ch);x=read(),y=read();
        if(ch[0]=='b') Solve_Link(x,y);
        else if(ch[0]=='p') Solve_Change(x,y);
        else Solve_Sum(x,y);
    }
}

int main()
{
    Input_Init();
    Solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值