SPOJ QTREE Query on a tree (lct)

修改边权,询问最大值

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#pragma comment(linxer, "/STACK:102400000,102400000")
#define LL long long 
#define pii pair<int, int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 10010
#define M 20020

int fst[N], vv[M], nxt[M], cost[M], e;
int tot, pre[N], ch[N][2], val[N];
int mx[N], buttom[N];
bool rt[N];
int n, m;
void init(){
    memset(fst, -1, sizeof fst);
    for(int i = 0; i <= n; ++i)
        ch[i][0] = ch[i][1] = pre[i] = 0;
    memset(rt, 1, sizeof rt);
    mx[0] = -inf;
    e = 0;
}
void add(int u, int v, int c){
    vv[e] = v, nxt[e] = fst[u], cost[e] = c, fst[u] = e++;
}
void dfs(int u, int p){
    pre[u] = p;
    for(int i = fst[u]; ~i; i = nxt[i]){
        int v = vv[i], c = cost[i];
        if(v == p) continue;
        buttom[i/2+1] = v;
        val[v] = mx[v] = c;
        dfs(v, u);
    }
}
bool judge(int u, int v){
    while(pre[u]) u = pre[u];
    while(pre[v]) v = pre[v];
    return u == v;
}
void push_up(int x){
    mx[x] = max(val[x], max(mx[ch[x][0]], mx[ch[x][1]]));
}
void push_down(int x){
}
void P(int x){
    while(!rt[x])
        P(pre[x]);
    push_down(x);
}
void rot(int x){
    int y = pre[x], d = ch[y][1] == x;
    ch[y][d] = ch[x][!d];
    if(ch[x][!d]) pre[ch[x][!d]] = y;
    ch[x][!d] = y;
    pre[x] = pre[y];
    pre[y] = x;
    if(rt[y])
        rt[y] = 0, rt[x] = 1;
    else 
        ch[pre[x]][ch[pre[x]][1] == y] = x;
    push_up(y);
}
void splay(int x){
    //P(x);
    while(!rt[x]){
        int f = pre[x], ff = pre[f];
        if(rt[f])
            rot(x);
        else if((ch[ff][1] == f) == (ch[f][1] == x))
            rot(f), rot(x);
        else rot(x), rot(x);
    }
    push_up(x);
}
int Access(int x){
    int y = 0;
    for(; x; y = x, x = pre[x]){
        splay(x);
        rt[ch[x][1]] = 1;
        ch[x][1] = y;
        rt[y] = 0;
        push_up(x);
    }
    return y;
}
void lca(int &u, int &v){
    Access(v), v = 0;
    for(splay(u); pre[u]; v = u, u = pre[u], splay(u)){
        rt[ch[u][1]] = 1;
        ch[u][1] = v;
        rt[v] = 0;
        push_up(u);
    }
}
int main(){
    int cas;
    scanf("%d", &cas);
    while(cas--){
        scanf("%d", &n);
        init();
        for(int i = 1; i < n; ++i){
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            add(u, v, c);
            add(v, u, c);
        }
        dfs(1, 0);
        char s[10];
        while(1){
            scanf("%s", s);
            if(s[0] == 'D') break;
            if(s[0] == 'C'){
                int u, c;
                scanf("%d%d", &u, &c);
                u = buttom[u];
                splay(u);
                val[u] = c;
                push_up(u);
            }
            else{
                int u, v;
                scanf("%d%d", &u, &v);
                lca(u, v);
                printf("%d\n", max(mx[v], mx[ch[u][1]]));
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值