求调(^人^)

P4114 Qtree1

qwerty111111 @ 2024-09-16 10:07:17

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;

int n , m;
int h[N] , tot;
struct node{
    int to , nxt , dis; 
} e[N];

inline void add(int u , int v , int w){
    e[++tot].to = v;
    e[tot].dis = w;
    e[tot].nxt = h[u];
    h[u] = tot;
}

int dep[N] , sze[N] , fa[N] , hson[N] , dfn[N] , iddfn[N] , cnt , A[N] , top[N];
inline void dfs(int x , int f){
    dep[x] = dep[f] + 1 , sze[x] = 1 , fa[x] = f , hson[x] = 0;
    for (int i = h[x] ; i ; i = e[i].nxt){
        int y = e[i].to , w = e[i].dis;
        if(y == f) continue;
        dfs(y , x);
        sze[x] += sze[y];
        if(sze[y] > sze[hson[x]]) hson[x] = y;
    }
}

inline void hld(int x , int topf){
    dfn[x] = ++cnt , iddfn[dfn[x]] = x ; top[x] = topf; 
    if(hson[x]) hld(hson[x] , topf);
    else return;
    for (int i = h[x] ; i ; i = e[i].nxt){
        int y = e[i].to , w = e[i].dis;
        if(y == fa[x] || y == hson[x]) continue;    
        hld(y , y);
        A[dfn[y]] = w;
    } 
}

namespace SegmentTree{
#define lc (u << 1)
#define rc (u << 1 | 1)
#define mid ((l + r) >> 1)
    int T[N << 2];

    inline void maintain(int u){
        T[u] = max(T[lc] , T[rc]);  
    }

    inline void Build(int u , int l ,int r){
        if(l == r){
            T[u] = A[l];
            return;
        }
        Build(lc , l , mid) , Build(rc , mid + 1 , r);
        maintain(u);
    }

    inline void Modify(int u , int l , int r , int p , int x){
        if(l == r){
            T[u] = x;
            return;
        }
        if(p <= mid) Modify(lc , l , mid , p , x);
        else Modify(rc , mid + 1 , r , p , x);
        maintain(u);
    }

    inline int Ask(int u , int l , int r , int L , int R){
        if(l >= L && r <= R){
            return T[u];
        }
        if(R <= mid) return (lc , l , mid , L , R);
        if(L > mid) return (rc , mid + 1 , r , L , R);
        return max(Ask(lc , l , mid , L , R) , Ask(rc , mid + 1 , r , L , R));
    }
#undef lc
#undef rc
#undef mid
}

inline int QMax(int u ,int v){
    int res = -0x3f3f3f3f;
    while(top[u] != top[v]){
        if(dep[top[u]] < dep[top[v]]) swap(u , v);
        res = max(res , SegmentTree::Ask(1 , 1 , n , dfn[top[u]] , dfn[u]));
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) swap(u , v);
    res = max(res , SegmentTree::Ask(1 , 1 , n , dfn[u] + 1 , dfn[v]));
    return res;
}

signed main(){
    ios::sync_with_stdio(false) , cin.tie(0);
    cin >> n;
    for (int i = 1 ; i < n ; i++){
        int u , v , w;
        cin >> u >> v >> w;
        add(u , v , w);
        add(v , u , w);
    }   

    dfs(1 , 0) , hld(1 , 1) , SegmentTree::Build(1 , 1 , n);

    string s;
    while(1){
        cin >> s;
        int x , y;
        if(s[0] == 'D') break;
        cin >> x >> y;  
        if(s[0] == 'Q'){
            if(x == y) cout << "0\n";
            else cout << QMax(x , y) << "\n";
        }
        if(s[0] == 'C'){
            x = x * 2 - 1;
            int a = e[x].to , b = e[x + 1].to;
            if(fa[a] == b) x = a;
            else x = b;
            SegmentTree::Modify(1 , 1 , n , dfn[x] , y);
        }
    }

    return 0;
}

by Shunpower @ 2024-09-16 10:13:01

@qwerty111111 重儿子忘记下放权值了


by Shunpower @ 2024-09-16 10:18:44

@qwerty111111

inline int Ask(int u , int l , int r , int L , int R){
        if(l >= L && r <= R){
            cout<<l<<" "<<r<<" "<<T[u]<<"!"<<endl;
            return T[u];
        }
        if(R <= mid) return (lc , l , mid , L , R);
        if(L > mid) return (rc , mid + 1 , r , L , R);
        return max(Ask(lc , l , mid , L , R) , Ask(rc , mid + 1 , r , L , R));
    }

这一段 return 了一坨逗号表达式,忘记写 Ask


by qwerty111111 @ 2024-09-16 10:19:10

@Shunpower 啥意思啊 刚学还不清楚┭┮﹏┭┮


by qwerty111111 @ 2024-09-16 10:19:54

乐了乐了 犯唐乐


by qwerty111111 @ 2024-09-16 10:20:58

但是样例还不对 输出0 3


by Rubbish_Du @ 2024-09-16 10:25:07

消愁了


by qwerty111111 @ 2024-09-16 10:29:17

还没对啊咋办


by qwerty111111 @ 2024-09-16 10:39:01

@Shunpower 为什么加了之后T了呢


by Shunpower @ 2024-09-16 10:44:44

@qwerty111111 我输出对了啊是 1\ 3


by qwerty111111 @ 2024-09-16 10:45:38

@Shunpower 样例过了但是T乐


| 下一页