题意:有一棵树,n个点,n-1条边每条边有一个权值,有三种操作,query a b,询问a到b所有路径的最大权值,negate a b,把a到b所有路径权值设置为对应权值的相反数,change a b,把第a条边权值设为b。
题解:线段树维护最大值和最小值,negate a b的时候把对应区间最大最小值交换并取反,注意要用一个标记每个区间取反的次数,因为偶数次取反相当于不变。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10005;
const int INF = 0x3f3f3f3f;
struct Edge {
    int u, v, w, nxt;
    Edge() {}
    Edge(int a, int b, int c, int d): u(a), v(b), w(c), nxt(d) {}
}e[N << 1];
int size[N], son[N], fa[N], top[N], id[N], dep[N];
int n, cnt, tot, head[N], minn[N << 2], maxx[N << 2], flag[N << 2];

void AddEdge(int u, int v, int w) {
    e[cnt] = Edge(u, v, w, head[u]);
    head[u] = cnt++;
    e[cnt] = Edge(v, u, w, head[v]);
    head[v] = cnt++;
}

void dfs2(int u, int tp) {
    top[u] = tp;
    id[u] = ++tot;
    if (son[u]) dfs2(son[u], tp);
    for (int i = head[u]; i + 1; i = e[i].nxt) {
        int v = e[i].v;
        if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

void dfs1(int u, int f, int depth) {
    size[u] = 1, son[u] = 0, dep[u] = depth, fa[u] = f;
    for (int i = head[u]; i + 1; i = e[i].nxt) {
        int v = e[i].v;
        if (v == f) continue;
        dfs1(v, u, depth + 1);
        size[u] += size[v];
        if (size[v] > size[son[u]]) son[u] = v;
    }
}

void pushup(int k) {
    minn[k] = min(minn[k * 2], minn[k * 2 + 1]);
    maxx[k] = max(maxx[k * 2], maxx[k * 2 + 1]);
}

void pushdown(int k) {
    if (flag[k]) {
        flag[k * 2] ^= 1;
        flag[k * 2 + 1] ^= 1;
        swap(minn[k * 2], maxx[k * 2]);
        minn[k * 2] = -minn[k * 2];
        maxx[k * 2] = -maxx[k * 2];
        swap(minn[k * 2 + 1], maxx[k * 2 + 1]);
        minn[k * 2 + 1] = -minn[k * 2 + 1];
        maxx[k * 2 + 1] = -maxx[k * 2 + 1];
        flag[k] = 0;
    }
}

void modify(int k, int left, int right, int pos, int v) {
    if (left == right) {
        minn[k] = maxx[k] = v;
        return;
    }
    pushdown(k);
    int mid = (left + right) / 2;
    if (pos <= mid)
        modify(k * 2, left, mid, pos, v);
    else
        modify(k * 2 + 1, mid + 1, right, pos, v);
    pushup(k);
}

void negate(int k, int left, int right, int l, int r) {
    if (l <= left && right <= r) {
        swap(minn[k], maxx[k]);
        minn[k] = -minn[k];
        maxx[k] = -maxx[k];
        flag[k] ^= 1;
        return;
    }
    pushdown(k);
    int mid = (left + right) / 2;
    if (l <= mid)
        negate(k * 2, left, mid, l, r);
    if (r > mid)
        negate(k * 2 + 1, mid + 1, right, l, r);
    pushup(k);
}

int query(int k, int left, int right, int l, int r) {
    if (l <= left && right <= r)
        return maxx[k];
    pushdown(k);
    int mid = (left + right) / 2, res = -INF;
    if (l <= mid)
        res = max(res, query(k * 2, left, mid, l, r));
    if (r > mid)
        res = max(res, query(k * 2 + 1, mid + 1, right, l, r));
    return res;
}

void solve(int u, int v, int flag) {
    int tp1 = top[u], tp2 = top[v], res = -INF;
    while (tp1 != tp2) {
        if (dep[tp1] < dep[tp2]) {
            swap(tp1, tp2);
            swap(u, v);
        }
        if (!flag) res = max(res, query(1, 1, tot, id[tp1], id[u]));
        else negate(1, 1, tot, id[tp1], id[u]);
        u = fa[tp1];
        tp1 = top[u];
    }
    if (dep[u] > dep[v]) swap(u, v);
    if (!flag) res = max(res, query(1, 1, tot, id[son[u]], id[v]));
    else negate(1, 1, tot, id[son[u]], id[v]);
    if (!flag) printf("%d\n", res);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int u, v, w;
        memset(head, -1, sizeof(head));
        memset(minn, INF, sizeof(minn));
        memset(maxx, -INF, sizeof(maxx));
        memset(flag, 0, sizeof(flag));
        cnt = tot = 0;
        scanf("%d", &n);
        for (int i = 0; i < n - 1; i++) {
            scanf("%d%d%d", &u, &v, &w);
            AddEdge(u, v, w);
        }
        dfs1(1, 0, 1);
        dfs2(1, 1);
        for (int i = 0; i < cnt; i += 2) {
            u = e[i].u, v = e[i].v;
            if (dep[u] < dep[v]) swap(u, v);
            modify(1, 1, tot, id[u], e[i].w);
        }
        char op[10];
        int a, b;
        while (scanf("%s", op) && op[0] != 'D') {
            scanf("%d%d", &a, &b);
            if (op[0] == 'Q')
                solve(a, b, 0);
            else if (op[0] == 'N')
                solve(a, b, 1);
            else {
                u = e[a - 1 << 1].u;
                v = e[a - 1 << 1].v;
                if (dep[u] < dep[v]) swap(u, v);
                modify(1, 1, tot, id[u], b);
            }
        }
    }
    return 0;
}