Description
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3…N-1.

We will ask you to perfrom some instructions of the following form:

CHANGE i ti : change the cost of the i-th edge to ti
or
QUERY a b : ask for the maximum edge cost on the path from node a to node b
Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

In the first line there is an integer N (N <= 10000),
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
The next lines contain instructions “CHANGE i ti” or “QUERY a b”,
The end of each test case is signified by the string “DONE”.
There is one blank line between successive tests.

Output

For each “QUERY” operation, write one integer representing its result.

Example

Input:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3
Hint
Added by: Thanh-Vy Hua
Date: 2005-06-08
Time limit: 0.851s
Source limit: 15000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: ADA ASM BASH BF C C# C++ 5 CLPS LISP sbcl LISP clisp D FORT HASK ICON ICK JAVA LUA NEM NICE CAML PAS gpc PAS fpc PERL PHP PIKE PRLG PYTH 2.7 RUBY SCM qobi SCM guile ST TEXT WSPC

树链剖分裸题。
题目大意:给一颗树,每条边有一个权值。有两种操作:
1. 修改某条边的值
2. 询问a、b两点路径上边权的最大值
做法:树链剖分 划分轻重链,线段树维护。

#include <bits/stdc++.h>
#define N 10010
using namespace std;

struct Edge
{
    int to, next, v;
}e[N * 3];
struct Node
{
    int u, v, w;
}a[N];
int head[N], fa[N], dep[N], son[N], top[N], siz[N], tid[N], cnt, n, m, Top, tim;
int t[N * 4];
inline int read()
{
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch-'0'; ch = getchar(); }
    return x * f;
}

void ins(int u, int v, int w)
{
    e[++ cnt].next = head[u];
    e[cnt].to = v;
    e[cnt].v = w;
    head[u] = cnt;
}

void insert(int u, int v, int w)
{
    ins(u, v, w);
    ins(v, u, w);
}

void dfs1(int x, int father, int Dep)
{
    fa[x] = father;
    dep[x] = Dep;
    siz[x] = 1;
    son[x] = 0;
    for(int i = head[x]; i != -1; i = e[i].next)
        if(e[i].to != father)
        {
            int y = e[i].to;
            dfs1(y, x, Dep + 1);
            siz[x] += siz[y];
            if(son[x] == 0 || siz[y] > siz[son[x]]) son[x] = y;
        }
}

void dfs2(int x, int father)
{
    tid[x] = ++ tim;
    top[x] = father;
    if(son[x]) dfs2(son[x], father);
    for(int i = head[x]; i != -1; i = e[i].next)
    {
        int y = e[i].to;
        if(y != fa[x] && y != son[x])
            dfs2(y, y);
    }
}

void build(int l, int r, int rt)
{
    t[rt] = 0;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, rt << 1);
    build(mid + 1, r, rt << 1 | 1);
}

void pushup(int rt)
{
    t[rt] = max(t[rt << 1], t[rt << 1 | 1]);
}

void update(int l, int r, int rt, int d, int val)
{
    if(l == r) 
    {
        t[rt] = val;
        return;
    }
    int mid = (l + r) >> 1;
    if(d <= mid) update(l, mid, rt << 1, d, val);
    else update(mid + 1, r, rt << 1 | 1, d, val);
    pushup(rt);
}

int query(int l, int r, int rt, int L, int R)
{
    if(l == L && R == r) return t[rt];
    int mid = (l + r) >> 1;
    if(r <= mid) return query(l, mid, rt << 1, L, R);
    else if(l > mid) return query(mid + 1, r, rt << 1 | 1, L, R);
    else return max(query(l, mid, rt << 1, L, mid), query(mid + 1, r, rt << 1 | 1, mid + 1, R));
}

int Find(int u, int v)
{
    int ans = 0;
    while(top[u] != top[v])
    {
        if(dep[top[u]] > dep[top[v]]) swap(u,v);
        ans = max(ans, query(1, tim, 1, tid[ top[v] ], tid[v]));
        v = fa[top[v]];
    }
    if(u == v) return ans;
    if(dep[u] > dep[v]) swap(u,v);
    ans = max(ans, query(1, tim, 1, tid[ son[u] ], tid[v]));
    return ans;
}

int main()
{
    int T;
    T = read();
    while(T --)
    {
        int u, v, w;
        memset(head, -1, sizeof(head));
        memset(son, 0, sizeof(son));
        Top = 0;
        tim = 0;
        n = read();
        for(int i = 1; i < n; i ++)
        {
            a[i].u = read();
            a[i].v = read();
            a[i].w = read();
            insert(a[i].u, a[i].v, a[i].w); 
        }
        dfs1(1, -1, 0);
        dfs2(1, 1);
        build(1, tim, 1);
        for(int i = 1; i < n; i ++)
        {
            if(dep[a[i].u] > dep[a[i].v]) swap(a[i].u,a[i].v);
            update(1, tim, 1, tid[a[i].v], a[i].w);
        }
        char Op[20];
        while(scanf("%s",Op) && strcmp(Op, "DONE") != 0)
            if(Op[0] == 'Q')
            {
                scanf("%d %d",&u, &v);
                printf("%d\n", Find(u,v));
            }
            else
            {
                scanf("%d %d", &u, &w);
                update(1, tim, 1, tid[a[u].v], w);
            }
    }
    return 0;
}