LCT求助QAQ 不过样例
查看原帖
LCT求助QAQ 不过样例
519384
Link_Cut_Y楼主2022/2/15 12:58
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define ls tr[x].s[0]
#define rs tr[x].s[1]

using namespace std;

const int N = 100010;

int n, m;
struct Tree
{
    int s[2], v, p;
    int maxn, sum;
    int rev;
}tr[N];

void push_rev(int x)
{
    swap(ls, rs);
    tr[x].rev ^= 1;
}

void pushup(int x)
{
    tr[x].maxn = max(tr[x].v, max(tr[ls].maxn, tr[rs].maxn));
    tr[x].sum = tr[x].v + tr[ls].sum + tr[rs].sum;
}

bool is_root(int x)
{
    return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x;
}

void rotate(int x)
{
    int y = tr[x].p, z = tr[y].p;
    int k = tr[y].s[1] == x;
    
    if (!is_root(y)) tr[z].s[tr[z].s[1] == y] = x; tr[x].p = z;
    tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
    tr[x].s[k ^ 1] = y, tr[y].p = x;
    
    pushup(y), pushup(x);
}

void splay(int x)
{
    while (!is_root(x))
    {
        int y = tr[x].p, z = tr[y].p;
        if (!is_root(y)) rotate(((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) ? x : y);
        rotate(x);
    }
}

void access(int x)
{
    for (int y = 0; x; y = x, x = tr[x].p)
    {
        splay(x);
        tr[x].s[1] = y;
        pushup(x);
    }
}

void make_root(int x)
{
    access(x);
    splay(x);
    push_rev(x);
}

void make_path(int x, int y)
{
    make_root(x);
    access(y);
    splay(x);
}

void link(int x, int y)
{
    make_root(x);
    tr[y].p = x;
}

int main()
{
    scanf("%d", &n);
    
    for (int i = 1; i <= n - 1; i ++ )
    {
        int x, y;
        scanf("%d%d", &x, &y);
        link(x, y);
    }
    
    for (int i = 1; i <= n; i ++ )
        scanf("%d", &tr[i].v);
        
    scanf("%d", &m);
    
    while (m -- )
    {
        char op[7];
        int x, y;
        scanf("%s%d%d", op, &x, &y);
        if (*op == 'C') splay(x), tr[x].v = y, pushup(x);
        if (*op == 'Q' && op[1] == 'M') make_path(x, y), printf("%d\n", tr[x].maxn);
        if (*op == 'Q' && op[1] == 'S') make_path(x, y), printf("%d\n", tr[x].sum);
    }
    
    return 0;
}
2022/2/15 12:58
加载中...