2843: 极地旅行社

明天省选复习一下板子。
lct板子题…操作都是基本要求…
不过一遍ac还是超出了我的预料QWQ
c++代码如下:

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x; i >= y; -- i)
typedef long long ll;
using namespace std;
template<typename T>inline void read(T&x)
{
    x = 0;char c;int sign = 1;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
    x *= sign;
}

const int N = 3e4 +50;
int n,m;
struct Link_Cut_Tree
{
    int val[N],w[N],f[N],rev[N];
    int ch[N][2];

    #define lc ch[x][0]
    #define rc ch[x][1]

    inline bool get(int x) { return ch[f[x]][1] == x; }

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

    inline void update(int x) { val[x] = val[lc] + val[rc] + w[x]; }

    inline void put_down(int x)
    {
        if(!is_root(x)) put_down(f[x]);
        if(rev[x])
        {
            rev[lc] ^= 1;rev[rc] ^= 1;
            rev[x] = 0; swap(lc,rc);
         } 
    }

    inline void rotate(int x)
    {
        int fa = f[x],t = get(x);
        if(!is_root(fa)) ch[f[fa]][get(fa)] = x;
        f[x] = f[fa]; ch[fa][t] = ch[x][t^1];
        if(ch[fa][t]) f[ch[fa][t]] = fa;
        ch[x][t^1] = fa; f[fa] = x;
        update(fa); update(x); 
    }

    inline void splay(int x)
    {
        put_down(x);
        while(!is_root(x))
        {
            if(!is_root(f[x])) rotate(get(x) == get(f[x]) ? f[x] : x);
            rotate(x);
        }
    }

    void access(int x)
    {
        int t = 0;
        while(x)
        {
            splay(x);
            ch[x][1] = t;
            update(x);
            t = x;
            x = f[x];
        }
    }

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

    inline void Link(int x,int y)
    {
        if(find(x) == find(y)) return void(puts("no"));
        mkrt(x); access(y); splay(y);
        f[x] = y; puts("yes");
    }

    inline void Modify(int x,int val)
    {
        splay(x); w[x] = val;
        update(x);
    }

    inline int find(int x)
    {
        access(x); splay(x);
        while(lc) x = lc;
        return x;
    }

    inline void query(int x,int y)
    {
        if(find(x) != find(y)) return void(puts("impossible"));
        mkrt(x); access(y); splay(y);
        printf("%d\n",val[y]);
    }
}lct;
char s[100];

int main()
{
    read(n);
    rep(i,1,n) read(lct.w[i]),lct.val[i] = lct.w[i];
    read(m);
    int a,b;
    rep(i,1,m)
    {
        scanf("%s",s);
        read(a); read(b);
        if(s[0] == 'e')
        {
            lct.query(a,b);
        }else if(s[0] == 'p')
        {
            lct.Modify(a,b);
        }else if(s[0] == 'b')
        {
            lct.Link(a,b);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值