算法笔记-树链剖分

树链剖分

一些性质:

  • 重链互不相交
  • 一个点到根节点的路径上,经过的轻链和重链均最多 O ( n l o g n ) \mathcal{O(nlogn)} O(nlogn)

模板:

void dfs1(int u, int p){
	dep[u] = dep[p] + 1;
	siz[u] = 1;
	fa[u] = p;
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == p)
			continue;
		dfs1(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])
			son[u] = v;
	}
}
void dfs2(int u, int tp){
	top[u] = tp;
	dfn[u] = ++dfntot;
	pre[dfntot] = u;	
	if(son[u])
		dfs2(son[u], tp);
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == fa[u] || v == son[u])
			continue;
		dfs2(v, v);
	}
}



P3384 【模板】重链剖分/树链剖分

题意:

一棵 n n n 节点的树,支持四种操作:

  • 路径上点权加 z z z
  • 求路径点权和
  • 子树内点权加 z z z
  • 求子树点权和
解析:

d f s dfs dfs 序将树上问题转化为区间问题,一棵子树内的 d f s dfs dfs 序是连续的,一条链内的 d f s dfs dfs 序是连续的。

线段树维护区间和。对第一、二种操作,在每条链上进行修改和查询,即区间 [ d f n ( t o p u ) , d f n ( u ) ] [dfn(top_u), dfn(u)] [dfn(topu),dfn(u)] ,不断向上跳;对第二种操作,在子树内修改和查询,即区间 [ d f n ( u ) , d f n ( u ) + s i z e ( u ) − 1 ] [dfn(u), dfn(u) + size(u)-1] [dfn(u),dfn(u)+size(u)1]

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e6+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
#define int ll

int head[maxn], tot;
struct edge{
	int to, nxt;
}e[maxn<<1];
void add(int a, int b){
	e[++tot].nxt = head[a];
	e[tot].to = b;
	head[a] = tot;
}
ll n, m, root, mod;
int dep[maxn], top[maxn], siz[maxn], son[maxn], fa[maxn];
int dfn[maxn], pre[maxn], dfntot;
ll v[maxn];//点权 
void dfs1(int u, int p){
	dep[u] = dep[p] + 1;
	siz[u] = 1;
	fa[u] = p;
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == p)
			continue;
		dfs1(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])
			son[u] = v;
	}
}
void dfs2(int u, int tp){
	top[u] = tp;
	dfn[u] = ++dfntot;
	pre[dfntot] = u;
	if(son[u])
		dfs2(son[u], tp);
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == fa[u] || v == son[u])
			continue;
		dfs2(v, v);
	}				
}
struct sgt{
	ll s, tag;
}t[maxn<<2];
inline int ls(int x){return x << 1;}
inline int rs(int x){return x << 1 | 1;}
void pushup(int k){
	t[k].s = (t[ls(k)].s + t[rs(k)].s) % mod;
}
void pushdown(int k, int l, int r){
	if(t[k].tag){
		ll tag = t[k].tag;
		int mid = (l+r) >> 1;
		
		t[ls(k)].tag = (tag + t[ls(k)].tag) % mod;
		t[ls(k)].s = (tag * (mid - l + 1) + t[ls(k)].s) % mod;
		
		t[rs(k)].tag = (tag + t[rs(k)].tag) % mod;
		t[rs(k)].s = (tag * (r - mid) + t[rs(k)].s) % mod;
				
		t[k].tag = 0;
	}
}
void build(int k, int l, int r){
	t[k].tag = 0;
	if(l == r){
		t[k].s = v[pre[l]] % mod;
		return; 
	}
	int mid = (l+r) >> 1;
	build(ls(k), l, mid);
	build(rs(k), mid+1, r);
	pushup(k);
}
void modify(int k, int l, int r, int x, int y, int w){ //区间修 
	if(x <= l && y >= r){
		t[k].s = (t[k].s + w * (r - l + 1) % mod) % mod;
		t[k].tag = (t[k].tag + w) % mod;
		return;
	}
	pushdown(k, l, r);
	int mid = (l+r) >> 1;
	if(x <= mid)
		modify(ls(k), l, mid, x, y, w);
	if(y > mid)
		modify(rs(k), mid+1, r, x, y, w);
	pushup(k); 
}
ll query(int k, int l, int r, int x, int y){
	if(x <= l && y >= r)
		return t[k].s;
	pushdown(k, l, r);
	ll res = 0;
	int mid = (l+r) >> 1;
	if(x <= mid)
		res = (res + query(ls(k), l, mid, x, y)) % mod;
	if(y > mid)
		res = (res + query(rs(k), mid+1, r, x, y)) % mod;
	return res;
}

ll query_route(int x, int y){
	ll res = 0;
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]])
			swap(x, y);
		res = (res + query(1, 1, n, dfn[top[x]], dfn[x])) % mod;
		x = fa[top[x]];
	}
	if(dep[x] < dep[y])
		swap(x, y); //x深
	res = (res + query(1, 1, n, dfn[y], dfn[x])) % mod;
	return res; 
}
void modify_route(int x, int y, int w){
	w %= mod;
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]])
			swap(x, y);
		modify(1, 1, n, dfn[top[x]], dfn[x], w);
		x = fa[top[x]];
	}
	if(dep[x] < dep[y])
		swap(x, y); //x深
	modify(1, 1, n, dfn[y], dfn[x], w);
}
ll query_tree(int x){
	ll res = query(1, 1, n, dfn[x], dfn[x] + siz[x] - 1);
	return res;
}
void modify_tree(int x, int w){
	w %= mod;
	modify(1, 1, n, dfn[x], dfn[x] + siz[x] - 1, w);
}
 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	

	cin >> n >> m >> root >> mod;
	for(int i = 1; i <= n; i++)
		cin >> v[i];
		
	for(int i = 1; i < n; i++){
		int a, b;
		cin >> a >> b;
		add(a, b); add(b, a);
	}
	
	dfs1(root, 0);
	dfs2(root, root);
	build(1, 1, n); 
	for(int i = 1; i <= m; i++){
		int op, x, y, z;
		cin >> op;
		if(op == 1){
			cin >> x >> y >> z;
			modify_route(x, y, z);			
		}
		else if(op == 2){
			cin >> x >> y;
			ll res = query_route(x, y);
			cout << res << endl;	
		}
		else if(op == 3){
			cin >> x >> z;
			modify_tree(x, z);
		}
		else{
			cin >> x;
			ll res = query_tree(x);
			cout << res << endl;
		}
	}
	return 0;
}




P3833 [SHOI2012]魔法树

题意:

支持路径上点权加,和查询子树点权和

解析:

同上题

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;


int n, q;

int head[maxn], tot;
struct edge{
	int to, nxt;
}e[maxn << 1];

int dep[maxn], siz[maxn], fa[maxn], son[maxn], top[maxn];
int dfn[maxn], dfntot, pre[maxn];
int val[maxn];

int ls(int x){return x << 1;}
int rs(int x){return x << 1 | 1;}

struct sgt{
	ll s, tag;
}t[maxn << 2];

void add(int a, int b){
	e[++tot].nxt = head[a];
	e[tot].to = b;
	head[a] = tot;
}
void dfs1(int u, int p){
	dep[u] = dep[p] + 1;
	siz[u] = 1;
	fa[u] = p;
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == p)
			continue;
		dfs1(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])
			son[u] = v;
	}
}
void dfs2(int u, int tp){
	top[u] = tp;
	dfn[u] = ++dfntot;
	pre[dfntot] = u;	
	if(son[u])
		dfs2(son[u], tp);
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == fa[u] || v == son[u])
			continue;
		dfs2(v, v);
	}
}
void pushup(int k){
	t[k].s = t[ls(k)].s + t[rs(k)].s;
}
void build(int k, int l, int r){
	t[k].tag = 0;
	if(l == r){
		t[k].s = val[pre[l]];
		return;
	}
	int mid = (l+r) >> 1;
	build(ls(k), l, mid);
	build(rs(k), mid+1, r);
	pushup(k);
}
void pushdown(int k, int l, int r){
	if(t[k].tag){
		int mid = (l+r) >> 1;
		t[ls(k)].s += (mid - l + 1) * t[k].tag;
		t[ls(k)].tag += t[k].tag;
		
		t[rs(k)].s += (r - mid) * t[k].tag;
		t[rs(k)].tag += t[k].tag;
		
		t[k].tag = 0;
	}
}
void modify(int k, int l, int r, int x, int y, int w){
	if(x <= l && y >= r){
		t[k].s += w * (r - l + 1);
		t[k].tag += w;
		return;
	}
	pushdown(k, l, r);
	int mid = (l+r) >> 1;
	if(x <= mid)
		modify(ls(k), l, mid, x, y, w);
	if(y > mid)
		modify(rs(k), mid+1, r, x, y, w);
	pushup(k);
}
ll query(int k, int l, int r, int x, int y){
	if(x <= l && y >= r)
		return t[k].s;
	pushdown(k, l, r);
	int mid = (l+r) >> 1;
	ll res = 0;
	if(x <= mid)
		res += query(ls(k), l, mid, x, y);
	if(y > mid)
		res += query(rs(k), mid+1, r, x, y);
	return res;
	res += query(ls(k), l, mid, x, y);
}
void Add(int u, int v, int w){
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])
			swap(u, v);
		modify(1, 1, n, dfn[top[u]], dfn[u], w);
		u = fa[top[u]];
	}
	if(dep[u] < dep[v])
		swap(u, v);
	modify(1, 1, n, dfn[v], dfn[u], w);
}
void Query(int u){
	ll res = query(1, 1, n, dfn[u], dfn[u] + siz[u] - 1);
	cout << res << endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for(int i = 1; i < n; i++){
		int a, b;
		cin >> a >> b;
		a++, b++;
		add(a, b);
		add(b, a);
	}
	dfs1(1, 0);
	dfs2(1, 1);
	build(1, 1, n);
	
	cin >> q;
	string op;
	int u, v, d;
	for(int i = 1; i <= n; i++){
		cin >> op;
		if(op == "A"){
			cin >> u >> v >> d;
			u++, v++;
			Add(u, v, d);
		}
		else if(op == "Q"){
			cin >> u;
			u++;
			Query(u);
		}
	}
	return 0;
}


P3401 洛谷树

题意:

边有边权,支持修改边权,询问 ( u , v ) (u,v) (u,v) 之间的子路径异或值的和

子路径是路径连续的一段,路径的异或值是路径上边权的异或值

解析:

首先考虑对于树上一条路径,如果快速求出该路径的异或值。

很自然的想法的利用前缀异或和,设 s s s 为前缀异或数组, s i s_i si 的值是根节点到节点 i i i 路径上边权的异或值。

则路径 P = ( u , . . . v ) P = (u,...v) P=(u,...v) 的异或值为 s i ⊕ s j s_i \oplus s_j sisj

然后考虑如何快速求出一个路径上的所有子路径的异或值之和。

注意到最大边权为 1023,可以考虑每一位。假设当前考虑第 k k k 位,路径 P = ( u , . . . v ) P = (u,...v) P=(u,...v) 中有 a a a 个第 k k k 位为1的 s s s,有 b b b 个第 k k k 为0 的 s s s,则对答案的贡献为 a × b × 2 k a \times b \times 2^k a×b×2k 。所以需要维护每一位的 01 数量,也就是 1 的数量。

对于修改操作,如果修改边 ( u , v ) (u,v) (u,v) ,不妨有 d e p u > d e p v dep_u > dep_v depu>depv s s s 受影响的节点一定位于子树 u u u 内。依次考虑每一位的影响。对于第 k k k 位,如果新边权和旧边权相同,则不会产生影响;如果不同,则原来的1变成0,原来的0变成1。

细节可以看代码,时间复杂度为 O ( 10 × n l o g n ) \mathcal{O(10\times nlog n)} O(10×nlogn)

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 3e4+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

int n, q;

int head[maxn], tot;
struct edge{
	int to, nxt, w;
}e[maxn<<1];

int dep[maxn], siz[maxn], fa[maxn], son[maxn], top[maxn];
int dfn[maxn], dfntot, pre[maxn];
int s[maxn], a[maxn];

struct sgt{
	int cnt[12];
	int tag[12];
}t[maxn<<2];

struct node{
	int cnt[12];
	node(){
		memset(cnt, 0, sizeof(cnt));
	}
	node operator + (const node &b) const{
		node res;
		for(int i = 0; i <= 10; i++)
			res.cnt[i] = cnt[i] + b.cnt[i];
		return res;
	}
};

void add(int a, int b, int c){
	e[++tot].nxt = head[a];
	e[tot].to = b;
	e[tot].w = c;
	head[a] = tot;
}
void dfs1(int u, int p){
	dep[u] = dep[p] + 1;
	siz[u] = 1;
	fa[u] = p;
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == p)
			continue;
		s[v] = s[u] ^ e[i].w;
		a[v] = e[i].w;
		dfs1(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])
			son[u] = v;
	}
}
void dfs2(int u, int tp){
	top[u] = tp;
	dfn[u] = ++dfntot;
	pre[dfntot] = u;
	if(son[u])
		dfs2(son[u], tp);
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == fa[u] || v == son[u])
			continue;
		dfs2(v, v);
	}
}
ll qpow(ll a, ll b){
	ll res = 1;
	while(b){
		if(b&1)
			res *= a;
		b = b >> 1;
		a *= a;
	}
	return res;
}
int ls(int x){return x << 1;}
int rs(int x){return x << 1 | 1;}
void pushup(int k){
	for(int i = 0; i <= 10; i++)
		t[k].cnt[i] = t[ls(k)].cnt[i] + t[rs(k)].cnt[i];
}
void build(int k, int l, int r){
	memset(t[k].cnt, 0, sizeof(t[k].cnt));
	memset(t[k].tag, 0, sizeof(t[k].tag));
	if(l == r){
		int x = s[pre[l]];
		for(int i = 0; i <= 10; i++){
			if(x & (1 << i))
				t[k].cnt[i]++;
		}
		return;
	}
	int mid = (l+r) >> 1;
	build(ls(k), l, mid);
	build(rs(k), mid+1, r);
	pushup(k);
}
void pushdown(int k, int l, int r){
	int mid = (l+r) >> 1;
	for(int i = 0; i <= 10; i++){
		if(!t[k].tag[i])
			continue;
		t[ls(k)].cnt[i] = (mid-l+1) - t[ls(k)].cnt[i];
		t[rs(k)].cnt[i] = (r-mid) - t[rs(k)].cnt[i];
		
		t[ls(k)].tag[i] ^= 1;
		t[rs(k)].tag[i] ^= 1;
		
		t[k].tag[i] = 0;
	}
}
void modify(int k, int l, int r, int x, int y, int pos){
	if(x <= l && y >= r){
		t[k].tag[pos] ^= 1;
		t[k].cnt[pos] = (r - l + 1) - t[k].cnt[pos];
		return;
	}
	pushdown(k, l, r);
	int mid = (l+r) >> 1;
	if(x <= mid)
		modify(ls(k), l, mid, x, y, pos);
	if(y > mid)
		modify(rs(k), mid+1, r, x, y, pos);
	//pushup(k);
	for(int i = 0; i <= 10; i++)
		t[k].cnt[i] = t[ls(k)].cnt[i] + t[rs(k)].cnt[i];	
}

node query(int k, int l, int r, int x, int y){
	node res;
	if(x <= l && y >= r){
		for(int i = 0; i <= 10; i++)
			res.cnt[i] = t[k].cnt[i];
		return res;
	}
	pushdown(k, l, r);
	int mid = (l+r) >> 1;
	if(x <= mid){
		node tmp = query(ls(k), l, mid, x, y);
		res = res + tmp;
	}
	if(y > mid){
		node tmp = query(rs(k), mid+1, r, x, y);
		res = res + tmp;
	}
	return res;
}
node query_zero(node x, int y){
	node res;
	for(int i = 0; i <= 10; i++)
		res.cnt[i] = y - x.cnt[i];
	return res;
}
int geti(int x, int i){
	if(x & (1 << i))
		return 1;
	else
		return 0;
}
void change(int u, int v, int w){
	int x = (dep[u] > dep[v] ? u : v);
	int p = a[x];
	for(int i = 0; i <= 10; i++){
		if(geti(w, i) != geti(p, i))
			modify(1, 1, n, dfn[x], dfn[x] + siz[x] - 1, i);
	}
	a[x] = w;
}
void query_res(int u, int v){
	node res_one;
	node res_zero;
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])
			swap(u, v);
		node tmp = query(1, 1, n, dfn[top[u]], dfn[u]);
		res_one = res_one + tmp;
		res_zero = res_zero + query_zero(tmp, dfn[u] - dfn[top[u]] + 1);
		u = fa[top[u]]; 
	}
	if(dep[u] < dep[v])
		swap(u, v);
	node tmp = query(1, 1, n, dfn[v], dfn[u]);
	res_one = res_one + tmp;
	res_zero = res_zero + query_zero(tmp, dfn[u] - dfn[v] + 1);
	
	ll res = 0;
	for(int i = 0; i <= 10; i++){
		res += res_one.cnt[i] * res_zero.cnt[i] * qpow(2, i);
	}
	cout << res << endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n >> q;
	for(int i = 1; i < n; i++){
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	dfs1(1, 0);
	dfs2(1, 1);
	
	build(1, 1, n);
	
	for(int i = 1; i <= q; i++){
		int op, u, v, w;
		cin >> op >> u >> v;
		if(op == 1){
			query_res(u, v);
		}
		else if(op == 2){
			cin >> w;
			change(u, v, w);			
		}
	}
	return 0;
}




P4315 月下“毛景树”

题意:

边有边权。支持四种操作:

  • 修改边权为 w w w
  • 修改路径上边权为 w w w
  • 路径上边权增加 w w w
  • 询问路径上边权最大值
解析:

对于边权,首先转换成点权。除根节点以外,每个节点有唯一的父节点,所以 u u u 的点权为 u u u f a ( u ) fa(u) fa(u) 之间的边权。

有两种修改操作,一种是“推平”,一种是加。所以线段树需要维护两个标记。注意到,如果有推平操作,之前的加就无效了,在pushdown的时候,把加标记清零。

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

inline int ls(int x){return x << 1;}
inline int rs(int x){return x << 1 | 1;}
struct sgt{
	int v;
	int tag;//推平
	int lazy;//懒 
}t[maxn<<2];
struct edge{
	int to, nxt, w;
}e[maxn<<1];
struct node{
	int fr, to, w;
	node(int fr, int to, int w) : fr(fr), to(to), w(w){} 
};
vector<node> edg;
int head[maxn], tot;
int dep[maxn], top[maxn], siz[maxn], fa[maxn], son[maxn];
int dfn[maxn], pre[maxn], dfntot;
int a[maxn];
int n;
void add(int a, int b, int c){
	e[++tot].nxt = head[a];
	e[tot].to = b;
	e[tot].w = c;
	head[a] = tot;
}
void dfs1(int u, int p){
	dep[u] = dep[p] + 1;
	siz[u] = 1;
	fa[u] = p;
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == p)
			continue;
		a[v] = e[i].w;
		dfs1(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])
			son[u] = v;
	}
}
void dfs2(int u, int tp){
	dfn[u] = ++dfntot;
	pre[dfntot] = u;
	top[u] = tp;
	if(son[u])
		dfs2(son[u], tp);
	for(int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v == fa[u] || v == son[u])
			continue;
		dfs2(v, v);
	}
}
void pushup(int k){
	t[k].v = max(t[ls(k)].v, t[rs(k)].v);
}
void pushdown(int k){
	if(t[k].tag >= 0){
		t[ls(k)].lazy = t[rs(k)].lazy = 0;
		t[ls(k)].v = t[ls(k)].tag = t[rs(k)].v = t[rs(k)].tag = t[k].tag;
		t[k].tag = -1;
	}
	
	if(t[k].lazy){
		t[ls(k)].lazy += t[k].lazy;
		t[rs(k)].lazy += t[k].lazy;
		t[ls(k)].v += t[k].lazy;
		t[rs(k)].v += t[k].lazy;
		t[k].lazy = 0;
	} 
}
void build(int k, int l, int r){
	t[k].lazy = 0;
	t[k].tag = -1;
	if(l == r){
		t[k].v = a[pre[l]];
		return;
	}
	int mid = (l+r) >> 1;
	build(ls(k), l, mid);
	build(rs(k), mid+1, r);
	pushup(k);
}

void modify(int k, int l, int r, int x, int y, int w){ //推平 
	if(x <= l && y >= r){
		t[k].v = w;
		t[k].tag = w;
		t[k].lazy = 0;
		return;
	}
	int mid = (l+r) >> 1;
	pushdown(k);
	if(x <= mid)
		modify(ls(k), l, mid, x, y, w);
	if(y > mid)
		modify(rs(k), mid+1, r, x, y, w);
	pushup(k);
	
}
void update(int k, int l, int r, int x, int y, int w){ //区间加 
	if(x <= l && y >= r){
		t[k].v += w;
		t[k].lazy += w;
		return;
	}
	int mid = (l+r) >> 1;
	pushdown(k);
	if(x <= mid)
		update(ls(k), l, mid, x, y, w);
	if(y > mid)
		update(rs(k), mid+1, r, x, y, w);
	pushup(k);
} 

ll query(int k, int l, int r, int x, int y){
	if(x <= l && y >= r)
		return t[k].v;
	pushdown(k);
	int mid = (l+r) >> 1;
	ll res = -1;
	if(x <= mid)
		res = max(res, query(ls(k), l, mid, x, y));
	if(y > mid)
		res = max(res, query(rs(k), mid+1, r, x, y));
	return res;
} 
void Change(int k, int w){
	node s = edg[k-1];
	int u = s.fr;
	int v = s.to;	
	if(fa[v] != u)
		swap(u, v);
	modify(1, 1, n, dfn[v], dfn[v], w);
}
void Cover(int u, int v, int w){
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])
			swap(u, v);
		modify(1, 1, n, dfn[top[u]], dfn[u], w);
		u = fa[top[u]];
	}
	if(dep[u] < dep[v])
		swap(u, v);
	modify(1, 1, n, dfn[v]+1, dfn[u], w);
}
void Add(int u, int v, int w){
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])
			swap(u, v);
		update(1, 1, n, dfn[top[u]], dfn[u], w);
		u = fa[top[u]];
	}
	if(dep[u] < dep[v])
		swap(u, v);
	update(1, 1, n, dfn[v]+1, dfn[u], w);
}
void Max(int u, int v){
	ll res = -1;
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])
			swap(u, v);
		res = max(res, query(1, 1, n, dfn[top[u]], dfn[u]));
		u = fa[top[u]];
	}
	if(dep[u] < dep[v])
		swap(u, v);
	res = max(res, query(1, 1, n, dfn[v]+1, dfn[u]));
	cout << res << endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for(int i = 1; i < n; i++){
		int a, b, c;
		cin >> a >> b >> c;
		edg.push_back(node(a, b, c));
		add(a, b, c);
		add(b, a, c);
	}
	dfs1(1, 0);
	dfs2(1, 1);
	build(1, 1, n);
	string op;
	int k, w, u, v;
	while(1){
		cin >> op;
		if(op == "Stop")
			break;
		else if(op == "Change"){
			cin >> k >> w;
			Change(k, w);
		}
		else if(op == "Cover"){
			cin >> u >> v >> w;
			Cover(u, v, w);
		}
		else if(op == "Add"){
			cin >> u >> v >> w;
			Add(u, v, w);
		}
		else if(op == "Max"){
			cin >> u >> v;
			Max(u, v);
		}
	} 
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值