不难的树链剖分。。。用线段树维护最大值最小值即可。。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 2000005 #define maxm 4000005 #define eps 1e-10 #define mod 1000000007 #define INF 1e9 #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R //#pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head int mark[maxn << 2]; int maxv[maxn << 2]; int minv[maxn << 2]; int size[maxn]; int son[maxn]; int dep[maxn]; int top[maxn]; int fa[maxn]; int w[maxn]; char ss[maxn]; int n, z; struct Edge { int v; Edge *next; }pool[maxm], *H[maxn], *edges; struct node { int u, v, c; node(int u = 0, int v = 0, int c = 0) : u(u), v(v), c(c) {} }edge[maxn]; void init(void) { z = 0; edges = pool; memset(H, 0, sizeof H); } void addedges(int u, int v) { edges->v = v; edges->next = H[u]; H[u] = edges++; } void read(void) { int u, v, c; scanf("%d", &n); for(int i = 1; i < n; i++) { scanf("%d%d%d", &u, &v, &c); edge[i] = node(u, v, c); addedges(u, v); addedges(v, u); } } void dfs1(int u) { size[u] = 1, son[u] = 0; for(Edge *e = H[u]; e; e = e->next) { int v = e->v; if(v != fa[u]) { dep[v] = dep[u] + 1; fa[v] = u; dfs1(v); size[u] += size[v]; if(size[son[u]] < size[v]) son[u] = v; } } } void dfs2(int u, int tp) { w[u] = ++z, top[u] = tp; if(son[u]) dfs2(son[u], tp); for(Edge *e = H[u]; e; e = e->next) { int v = e->v; if(v != fa[u] && v != son[u]) dfs2(v, v); } } void build(int o, int L, int R) { maxv[o] = -INF, minv[o] = INF, mark[o] = 0; if(L == R) return; int mid = (L + R) >> 1; build(lson); build(rson); } void pushup(int o) { maxv[o] = max(maxv[ls], maxv[rs]); minv[o] = min(minv[ls], minv[rs]); } void pushdown(int o) { if(!mark[o]) return; swap(maxv[ls], minv[ls]); maxv[ls] = -maxv[ls]; minv[ls] = -minv[ls]; swap(maxv[rs], minv[rs]); maxv[rs] = -maxv[rs]; minv[rs] = -minv[rs]; mark[ls] ^= 1; mark[rs] ^= 1; mark[o] = 0; } void updata1(int o, int L, int R, int q, int v) { if(L == R) { maxv[o] = minv[o] = v; return; } pushdown(o); int mid = (L + R) >> 1; if(q <= mid) updata1(lson, q, v); else updata1(rson, q, v); pushup(o); } void updata2(int o, int L, int R, int ql, int qr) { if(ql <= L && qr >= R) { mark[o] ^= 1; swap(maxv[o], minv[o]); maxv[o] = -maxv[o]; minv[o] = -minv[o]; return; } pushdown(o); int mid = (L + R) >> 1; if(ql <= mid) updata2(lson, ql, qr); if(qr > mid) updata2(rson, ql, qr); pushup(o); } int query(int o, int L, int R, int ql, int qr) { if(ql <= L && qr >= R) return maxv[o]; int mid = (L + R) >> 1, ans = -INF; pushdown(o); if(ql <= mid) ans = max(ans, query(lson, ql, qr)); if(qr > mid) ans = max(ans, query(rson, ql, qr)); pushup(o); return ans; } void solve(int a, int b, int ok) { int f1 = top[a], f2 = top[b], ans = -INF; while(f1 != f2) { if(dep[f1] < dep[f2]) { swap(a, b); swap(f1, f2); } if(ok) updata2(1, 1, z, w[f1], w[a]); else ans = max(ans, query(1, 1, z, w[f1], w[a])); a = fa[f1], f1 = top[a]; } if(a == b) { if(!ok) printf("%d\n", ans); return; } if(dep[a] > dep[b]) swap(a, b); if(ok) updata2(1, 1, z, w[son[a]], w[b]); else ans = max(ans, query(1, 1, z, w[son[a]], w[b])); if(!ok) printf("%d\n", ans); } void work(void) { dfs1(1); dfs2(1, 1); build(1, 1, z); for(int i = 1; i < n; i++) if(dep[edge[i].v] < dep[edge[i].u]) swap(edge[i].u, edge[i].v); for(int i = 1; i < n; i++) updata1(1, 1, z, w[edge[i].v], edge[i].c); int a, b; while(scanf("%s", ss) == 1) { if(ss[0] == 'D') break; scanf("%d%d", &a, &b); if(ss[0] == 'C') updata1(1, 1, z, w[edge[a].v], b); if(ss[0] == 'Q') solve(a, b, 0); if(ss[0] == 'N') solve(a, b, 1); } } int main(void) { int _; while(scanf("%d", &_)!=EOF) { while(_--) { init(); read(); work(); } } return 0; }