经典题,树链剖分。由于是边上加权值,然而并不能线段树叶子节点表示区间,而还是表示点,但把每一边用它下面的第一个节点来表示,这样就把边转换为点,就很好更新了。
同样,没有用系统栈,就是不想用不知道为什么,但一定要用的时候相信没有问题。以后有时间再写系列第2题。
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define clr(a) memset(a,0,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define MAXN 10010
#define N
#define M 20
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
class Stack{
public:
int st[MAXN],num;
inline void clear(){
num=0;
}
inline void push(int x){
st[num++]=x;
}
inline void pop(){
num--;
}
inline int top(){
return st[num-1];
}
inline int size(){
return num;
}
inline bool empty(){
return !num;
}
}st1,st2;
int n,topval[MAXN],vto[MAXN],rvto[MAXN];
char str[20];
#define lch (id<<1)
#define rch (id<<1|1)
#define mid ((tree[id].left+tree[id].right)>>1)
class Segment_Tree{
public:
struct Tree{
int left,right;
int max;
}tree[MAXN<<2];
inline void plant(int id,int l,int r){
tree[id].left=l,tree[id].right=r;
if(l==r){
tree[id].max=topval[rvto[l]];
return;
}
plant(lch,l,mid);
plant(rch,mid+1,r);
tree[id].max=max(tree[lch].max,tree[rch].max);
}
inline void update(int id,int pos,int v){
if(tree[id].left==pos&&pos==tree[id].right){
tree[id].max=v;
return;
}
if(pos<=mid) update(lch,pos,v);
else update(rch,pos,v);
tree[id].max=max(tree[lch].max,tree[rch].max);
}
inline int query_max(int id,int l,int r){
if(tree[id].left==l&&tree[id].right==r){
return tree[id].max;
}
if(r<=mid) return query_max(lch,l,r);
else if(mid<l) return query_max(rch,l,r);
else return max(query_max(lch,l,mid),query_max(rch,mid+1,r));
}
}segtree;
struct Edge{
int from,to,next,val,id;
bool heavy;
}edge[MAXN<<1];
int head[MAXN],num_edge;
inline void add_Edge(int from,int to,int val,int id){
int t=++num_edge;
edge[t].from=from;
edge[t].to=to;
edge[t].next=head[from];
edge[t].val=val;
edge[t].heavy=false;
edge[t].id=id;
head[from]=t;
}
int fa[MAXN],dep[MAXN],siz[MAXN],upto[MAXN],f[MAXN][M];
int dfs_clocks,uptemp,maxidep,edgeto[MAXN];
inline void dfs1(){
st1.clear();
st2.clear();
st1.push(1);
fa[1]=-1;
dep[1]=0;
int now,to;
maxidep=0;
while(!st1.empty()){
now=st1.top();
st1.pop();
st2.push(now);
for(int i=head[now];i!=-1;i=edge[i].next){
to=edge[i].to;
if(fa[now]==to) continue;
topval[to]=edge[i].val;
edgeto[edge[i].id]=to;
fa[to]=now;
dep[to]=dep[now]+1;
maxidep=max(maxidep,dep[to]);
st1.push(to);
}
}
while(!st2.empty()){
now=st2.top();
st2.pop();
siz[now]=1;
int e=-1,maxi=-INT;
for(int i=head[now];i!=-1;i=edge[i].next){
to=edge[i].to;
if(fa[now]==to) continue;
siz[now]+=siz[to];
if(siz[to]>maxi){
e=i;
maxi=siz[to];
}
}
if(e!=-1) edge[e].heavy=true;
}
}
inline void dfs2(){
dfs_clocks=0;
st1.clear();
int now=1,e=-1;
vto[now]=++dfs_clocks;
rvto[dfs_clocks]=now;
upto[1]=uptemp=1;
for(int i=head[now];i!=-1;i=edge[i].next){
if(edge[i].heavy) e=i;
else st1.push(i);
}
if(e!=-1) st1.push(e);
while(!st1.empty()){
int nowe=st1.top();
st1.pop();
now=edge[nowe].to;
vto[now]=++dfs_clocks;
rvto[dfs_clocks]=now;
if(!edge[nowe].heavy) uptemp=now;
upto[now]=uptemp;
int e=-1;
for(int i=head[now];i!=-1;i=edge[i].next){
if(fa[now]==edge[i].to) continue;
if(edge[i].heavy) e=i;
else st1.push(i);
}
if(e!=-1) st1.push(e);
}
}
inline void lca_init(){
repin(i,1,n){
f[i][0]=fa[i];
}
for(int j=1;1<<j<=maxidep;j++){
repin(i,1,n){
f[i][j]=-1;
if(f[i][j-1]!=-1) f[i][j]=f[f[i][j-1]][j-1];
}
}
}
inline int lca_query(int x,int y){
if(x==y) return x;
if(dep[x]<dep[y]) swap(x,y);
int j=0;
for(j=0;1<<j<=dep[x];j++);
for(j--;j>=0;j--){
if(f[x][j]!=-1 && dep[f[x][j]]>=dep[y]) x=f[x][j];
}
if(x==y) return x;
for(j=0;1<<j<=dep[x];j++);
for(j--;j>=0;j--){
if(f[x][j]!=f[y][j]){
x=f[x][j];
y=f[y][j];
}
}
return fa[x];
}
inline int jump_max(int x,int aim){
int res=-INT;
while(vto[x]>vto[aim]){
int from=vto[upto[x]];
int to=vto[x];
if(from<=vto[aim]){
res=max(res,segtree.query_max(1,vto[aim]+1,to));
break;
}
res=max(res,segtree.query_max(1,vto[upto[x]],to));
x=fa[upto[x]];
}
return res;
}
inline int query_max(int x,int y){
int lca=lca_query(x,y);
if(x==lca) return jump_max(y,lca);
else if(y==lca) return jump_max(x,lca);
else return max(jump_max(x,lca),jump_max(y,lca));
}
int main(){;
int T;
T=read();
while(T--){
n=read();
num_edge=0;
repin(i,1,n){
head[i]=-1;
}
repin(i,1,n-1){
int a,b,c;
a=read(),b=read(),c=read();
add_Edge(a,b,c,i);
add_Edge(b,a,c,i);
}
dfs1();
dfs2();
lca_init();
segtree.plant(1,1,n);
while(1){
scanf("%s",str);
if(str[0]=='D') break;
if(str[0]=='Q'){
int a,b;
a=read(),b=read();
int maxi=query_max(a,b);
printf("%d\n",maxi);
}
else{
int i,t;
i=read(),t=read();
i=edgeto[i];
i=vto[i];
segtree.update(1,i,t);
}
}
}
return 0;
}