P1001 A+B Problem 题解

· · 题解

注意到这道题需要把 a 和 b 加起来,这需要在 a 和 b 之间建立起联系,也就是一个 Link 的操作,故可以想到使用 Link-Cut Tree 维护。

Link-Cut Tree 目前还是 10 级算法,甚至没有下放到 noi 考点,我认为这题是红题多少有点恶评了。

代码:

#include<bits/stdc++.h>
#define cint const int
#define uint unsigned int
#define cuint const unsigned int
#define ll long long
#define cll const long long
#define ull unsigned long long
#define cull const unsigned long long
#define sh short
#define csh const short
#define ush unsigned short
#define cush const unsigned short
using namespace std;
int read()
{
    int x=0,zf=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')zf=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=getchar();
    }
    return x*zf;
}
void print(cint x)
{
    if(x<10)
    {
        putchar(x+'0');
        return;
    }
    print(x/10);
    putchar(x%10+'0');
}
void princh(cint x,const char ch)
{
    if(x<0)
    {
        putchar('-');
        print(-x);
        putchar(ch);
        return;
    }
    print(x);
    putchar(ch);
}
cint N=2;
int n;
int val[N+1];
namespace LCT{
    int f[N+1];
    struct noe{
        int son[2],sum;
        bool tag;
    }t[N+1];
    inline bool notroot(cint x)
    {
        return (t[f[x]].son[0]==x||t[f[x]].son[1]==x);
    }
    inline void rev(cint x)
    {
        swap(t[x].son[0],t[x].son[1]);
        t[x].tag^=1;
    }
    void push_up(cint p)
    {
        t[p].sum=t[t[p].son[0]].sum+t[t[p].son[1]].sum+val[p];
    }
    void push_down(cint p)
    {
        if(!t[p].tag)return;
        t[p].tag=0;
        rev(t[p].son[0]);
        rev(t[p].son[1]);
    }
    void rotate(cint x)
    {
        cint y=f[x],z=f[y],p=(t[y].son[1]==x),q=(t[z].son[1]==y),w=t[x].son[!p];
        if(notroot(y))
        {
            t[z].son[q]=x;
        }
        t[x].son[!p]=y;
        t[y].son[p]=w;
        if(w)
        {
            f[w]=y;
        }
        f[y]=x;
        f[x]=z;
        push_up(y);
    }
    void rotate2(cint x)
    {
        cint y=f[x],z=f[y];
        if(notroot(y))
        {
            rotate((t[y].son[0]==x)^(t[z].son[0]==y)?x:y);
        }
        rotate(x);
    }
    void push_all(cint x)
    {
        if(notroot(x))push_all(f[x]);
        push_down(x);
    }
    void splay(cint x)
    {
        push_all(x);
        while(notroot(x))
        {
            rotate2(x);
        }
        push_up(x);
    }
    void access(int x)
    {
        for(int y=0;x;y=x,x=f[x])
        {
            splay(x);
            t[x].son[1]=y;
            push_up(x);
        }
    }
    void make_root(cint x)
    {
        access(x);
        splay(x);
        rev(x);
    }
    int find_root(int x)
    {
        access(x);
        splay(x);
        while(t[x].son[0])
        {
            push_down(x);
            x=t[x].son[0];
        }
        splay(x);
        return x;
    }
    void split(cint x,cint y)
    {
        make_root(x);
        access(y);
        splay(y);
    }
    bool link(cint x,cint y)
    {
        make_root(x);
        if(find_root(y)==x)return 0;
        f[x]=y;
        return 1;
    }
}
using namespace LCT;
int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    n=2;
    val[1]=read();
    val[2]=read();
    link(1,2);
    split(1,2);
    princh(t[2].sum,'\n');
    return 0;
}