题目 https://cn.vjudge.net/problem/POJ-3237
难点是线段树
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 100000 + 100;
const int inf = 0x3f3f3f3f;
int n,cnt;
struct node
{
int to,next;
}e[maxn];
struct edge
{
int u,v,w;
}edge[maxn];
int head[maxn];
void add(int u,int v)
{
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt++;
}
int fa[maxn],deep[maxn];
int son[maxn],tid[maxn],top[maxn],siz[maxn];
void dffs(int u,int f,int d)
{
fa[u] = f,deep[u] = d;
siz[u] = 1,son[u] = -1;
for(int i = head[u];i != -1;i=e[i].next)
{
int v = e[i].to;
if(v != f)
{
dffs(v,u,d+1);
siz[u] += siz[v];
if(son[u] == -1 || siz[v] > siz[son[u]])
{
son[u] = v;
}
}
}
}
void dfss(int u,int t)
{
top[u] = t,tid[u] = ++cnt;
if(son[u] != -1) dfss(son[u],t);
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(v != son[u]&&v != fa[u])
{
dfss(v,v);
}
}
}
struct Tree
{
int MAX,lazy,MIN;
}tree[maxn << 2];
int id_data[maxn];
void build(int o, int l, int r)
{
if(l == r)
{
tree[o].lazy = 1;
tree[o].MAX = tree[o].MIN = id_data[l];
return ;
}
int m = (l+r) / 2;
build(o*2,l,m);
build(o*2+1,m+1,r);
tree[o].lazy = 1;
tree[o].MAX = max(tree[o*2].MAX, tree[o*2+1].MAX);
tree[o].MIN = min(tree[o*2].MIN, tree[o*2+1].MIN);
}
void push_down(int o)
{
if(tree[o].lazy == -1)
{
int maxx = tree[o*2].MAX;
int minn = tree[o*2].MIN;
tree[o*2].MAX = -minn;
tree[o*2].MIN = -maxx;
maxx = tree[o*2+1].MAX;
minn = tree[o*2+1].MIN;
tree[o*2+1].MAX = -minn;
tree[o*2+1].MIN = -maxx;
tree[o].lazy = 1;
tree[o*2].lazy *= -1;
tree[o*2+1].lazy *= -1;
}
}
void updata1(int o,int l, int r, int x, int d)
{
if(l > x || r < x) return ;
if(l == x && r == x)
{
tree[o].MAX = d;
tree[o].MIN = d;
return ;
}
push_down(o);
int m = (l+r)/2;
updata1(o*2,l,m,x,d);
updata1(o*2+1,m+1,r,x,d);
tree[o].MAX = max(tree[o*2].MAX, tree[o*2+1].MAX);
tree[o].MIN = min(tree[o*2].MIN, tree[o*2+1].MIN);
}
void updata2(int o, int l, int r, int ul, int ur)
{
if(l > ur || r < ul) return ;
if(ul <= l && ur >= r)
{
int maxn = tree[o].MAX;
int minn = tree[o].MIN;
tree[o].MAX = -minn;
tree[o].MIN = -maxn;
tree[o].lazy *= -1;
return ;
}
push_down(o);
int m = (l+r)/2;
updata2(o*2,l,m,ul,ur);
updata2(o*2+1,m+1,r,ul,ur);
tree[o].MAX = max(tree[o*2].MAX, tree[o*2+1].MAX);
tree[o].MIN = min(tree[o*2].MIN, tree[o*2+1].MIN);
}
void Updata(int x,int y)
{
int tx = top[x],ty = top[y];
while(tx != ty)
{
if(deep[tx] < deep[ty]) swap(x,y),swap(tx,ty);
updata2(1,1,n,tid[tx],tid[x]);
x = fa[tx],tx = top[x];
}
if(x == y) return ;
if(deep[x] > deep[y]) swap(x,y);
updata2(1,1,n,tid[x]+1,tid[y]);
}
int query(int o, int l, int r, int ul, int ur)
{
if(l > ur || r < ul) return -inf;
if(ul <= l && ur >= r)
{
return tree[o].MAX;
}
push_down(o);
int m = (l+r)/2;
return max(query(o*2,l,m,ul,ur),query(o*2+1,m+1,r,ul,ur));
}
int Query(int x, int y)
{
int ans = -inf;
int tx = top[x], ty = top[y];
while(tx != ty)
{
if(deep[tx] < deep[ty]) swap(x,y),swap(tx,ty);
ans = max(ans,query(1,1,n,tid[tx],tid[x]));
x = fa[tx],tx = top[x];
}
if(x == y) return ans;
if(deep[x] > deep[y]) swap(x,y);
ans = max(ans,query(1,1,n,tid[x]+1,tid[y]));
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(head,-1,sizeof(head));
memset(son,-1,sizeof(son));
cnt = 0;
for(int i = 1;i < n;i++)
{
scanf("%d %d %d", &edge[i].u, & edge[i].v, &edge[i].w);
add(edge[i].u, edge[i].v);
add(edge[i].v, edge[i].u);
}
cnt = 0;
dffs(1,-1,0);
dfss(1,1);
// build(1,1,n);
for(int i = 1;i < n;i++)
{
if(deep[edge[i].u] < deep[edge[i].v]) swap(edge[i].u, edge[i].v);
id_data[tid[edge[i].u]] = edge[i].w;
}
build(1,1,n);
while(1)
{
char str[20];
int a,b;
scanf("%s", str);
if(str[0] == 'C')
{
scanf("%d %d", &a, &b);
updata1(1,1,n,tid[edge[a].u],b);
}
else if(str[0] == 'N')
{
scanf("%d %d", &a, &b);
Updata(a,b);
}
else if(str[0] == 'Q')
{
scanf("%d %d", &a, &b);
printf("%d\n", Query(a,b));
}
else
break;
}
}
return 0;
}