题意:

给出一棵树,两种操作:

  1. 修改边权
  2. 查询离某个点最远的点的距离。

对于每个询问,可以想到这个距离最远的点一定是,树的直径的一端,证明可以使用反证法。

那么就只需要动态维护直径,每次查询到两个直径端点的最长距离即可。

这些操作可以使用LCT维护。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const int N=888888;
int n;
pii e[N];
int fa[N],val[N];
int sum[N];
int siz[N];
int tag[N],son[N][2];
int lmx[N],rmx[N];
int mxd[N];

struct inandelheap
{
  	priority_queue<int>whl,del;
	inline void ist(int x){if(x!=-inf)whl.push(x);}
	inline void era(int x){if(x!=-inf)del.push(x);}
	inline int Top(){
		while(1)
		{
			if(whl.empty()) return -inf;
			if(del.empty()) return whl.top();
			if(whl.top()==del.top()) whl.pop(),del.pop();
			else return whl.top();
		}
	}
	inline int Sec() {
		int tmp=Top();era(tmp);
		int tmp2=Top();ist(tmp);
		return tmp2;
	}
}h[N],p[N];

inline void INS(int x,int y) {h[x].ist(lmx[y]),p[x].ist(mxd[y]);}
inline void ERA(int x,int y) {h[x].era(lmx[y]),p[x].era(mxd[y]);}
inline int get(int x){return x==son[fa[x]][1];}
inline int isnotroot(int x){return son[fa[x]][0]==x||son[fa[x]][1]==x;}
inline void push_up(int x)
{
	siz[x]=siz[son[x][0]]+siz[son[x][1]];
	sum[x]=sum[son[x][0]]+sum[son[x][1]]+val[x];
	int t=max(h[x].Top(),0ll),L=max(t,rmx[son[x][0]])+val[x],R=max(t,lmx[son[x][1]])+val[x];
	//int t=max(Top(H[x]),0ll),L=max(t,rmx[son[x][0]])+val[x],R=max(t,lmx[son[x][1]])+val[x];
	lmx[x]=max(lmx[son[x][0]],sum[son[x][0]]+R);rmx[x]=max(rmx[son[x][1]],sum[son[x][1]]+L);
	mxd[x]=max(max(rmx[son[x][0]]+R,lmx[son[x][1]]+L),max(mxd[son[x][0]],mxd[son[x][1]]));
	ckmax(mxd[x],max(t+val[x]+max(0ll,h[x].Sec()),p[x].Top()));
	//ckmax(mxd[x],max(t+val[x]+max(0ll,Sec(H[x])),Top(P[x])));

}
inline void push_flp(int x){Swap(lmx[x],rmx[x]);Swap(son[x][0],son[x][1]);tag[x]^=1;}
inline void push_down(int x)
{
	if(tag[x])
	{
		if(son[x][0]) push_flp(son[x][0]);
		if(son[x][1]) push_flp(son[x][1]);
		tag[x]=0;
	}
}
inline void rotate(int x){
	int f=fa[x],gf=fa[f],wh=get(x);
	if(isnotroot(f)) son[gf][get(f)]=x;
	son[f][wh]=son[x][wh^1];
	fa[son[x][wh^1]]=f;
	son[x][wh^1]=f;
	fa[f]=x;
	fa[x]=gf;
	push_up(f);
	push_up(x);
}
void update(int x) {if(isnotroot(x))update(fa[x]);push_down(x);}
inline void splay(int x)
{
	update(x);
	for(int f;f=fa[x],isnotroot(x);rotate(x))
		if(isnotroot(f)) rotate(get(x)==get(f)?f:x);
	push_up(x);
}
inline int access(int x)
{
	int y;
	for(y=0;x;y=x,x=fa[x]) {splay(x);if(y)ERA(x,y); if(son[x][1])INS(x,son[x][1]); son[x][1]=y,push_up(x);}
	return y;
}
inline void make_root(int x){access(x);splay(x);push_flp(x);}
inline int find_root(int x)
{
	access(x),splay(x);
	while(son[x][0])push_down(x),x=son[x][0];
	splay(x);
	return x;
}
inline void spilt(int x,int y){make_root(x);access(y);splay(y);}


inline void link(int x,int y) 
{
	make_root(x);splay(x);
	if(find_root(y)!=x){fa[x]=y;INS(y,x);}
}
inline void cut(int x,int y)
{
	spilt(x,y);
	if(son[y][0]==x&&!son[x][1]) son[y][0]=fa[x]=0;
}	
inline int query(int x) {access(x),splay(x); return rmx[x];}
void print()
{
	puts("");
	puts("---------------up--------------");
	R(i,0,10) printf("x:%lld ls:%lld rs:%lld\n",i,son[i][0],son[i][1]);
	printf("sum:");R(i,0,10) printf("%lld ",sum[i]);puts("");
	printf("lmx:");R(i,0,10) printf("%lld ",lmx[i]);puts("");
	printf("rmx:");R(i,0,10) printf("%lld ",rmx[i]);puts("");
	printf("mxd:");R(i,0,10) printf("%lld ",mxd[i]);puts("");
	printf("val:");R(i,0,10) printf("%lld ",val[i]);puts("");
	puts("--------------down-------------");
	puts("");
}

signed main()
{
	//h[1].ist(1),h[1].ist(2),h[1].ist(3),h[1].ist(100);
	//writeln(h[1].Sec());
	n=read();
	int u,v;R(i,1,n-1) u=read(),v=read(),e[i]=mkp(u,v),sum[i+n]=val[i+n]=read(),link(u,i+n),link(v,i+n);
	char opt[11];
	int nm,w,vl;
	for(int _=read();_;_--)
	{
		//print();
		scanf("%s",opt+1);
		if(opt[1]=='C') 
		{
			nm=read(),w=read();
			cut(e[nm].fi,nm+n),cut(e[nm].se,nm+n);
			//cut(e[nm].fi,nm+n),cut(nm+n,e[nm].se);
			sum[nm+n]=val[nm+n]=w;
			//link(e[nm].fi,nm+n),link(nm+n,e[nm].se);
			link(e[nm].fi,nm+n),link(e[nm].se,nm+n);
		}
		if(opt[1]=='Q') vl=read(),writeln(query(vl));
	}
}
/*
test #7:
	10
	10 2 8506666
	4 7 4366355
	4 6 22029
	6 9 3986792
	4 1 4712693
	6 2 4817176
	5 6 2143758
	8 5 8103920
	3 10 9511674
	10
	Q 3
	Q 3
	Q 9
	C 9 2486896
	Q 9
	C 2 5488953
	C 9 4165296
	C 8 9119928
	Q 3
	C 4 851632
	
good:
	33083194
	33083194
	26822308
	19797530
	28752824
	
bad:
	27570238
	27570238
	26822308
	19797530
	53935320
	
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(seed);  
uniform_int_distribution<long long> dist(0, 10000000);  // ¸ø¶¨·¶Î§
//printf("%lld ",dist(rand_num));
const int n=10,m=10;
int fa[1111111];
inline int find_fa(int x){return x==fa[x]?x:fa[x]=find_fa(fa[x]);}
signed main()
{
	R(i,1,n) fa[i]=i;
	printf("%lld\n",n);
	int u,v,d,fu,fv;
	R(i,1,n-1)
	{
		while(1)
		{
			u=dist(rand_num)%n+1,v=dist(rand_num)%n+1,d=dist(rand_num);
			fu=find_fa(u),fv=find_fa(v);
			if(fu!=fv&&u!=v) break;
		}
		fa[fu]=fv;
		printf("%lld %lld %lld\n",u,v,d);
	}
	printf("%lld\n",m);
	int opt,x,y;
	R(i,1,m)
	{
		opt=dist(rand_num)%2;
		int fuck=dist(rand_num)%n;if(fuck==0) fuck=1;
		if(!opt) printf("Q %lld\n",dist(rand_num)%n+1);
		else printf("C %lld %lld\n",fuck,dist(rand_num));
	}
}
*/