【题解】LuoGu2590:[ZJOI2008]树的统计

原题传送门
我又双叒叕来水博客啦!
又见树剖裸题,在dfs序上维护线段树,记录max值与sum值即可

Code:

#include <bits/stdc++.h>
#define maxn 200010
#define ls rt << 1
#define rs rt << 1 | 1
#define inf 2147483647
using namespace std;
struct Edge{
	int to, next;
}edge[maxn << 1];
struct Seg{
	int l, r, sum, m;
}seg[maxn << 2];
int head[maxn], num, size[maxn], son[maxn], fa[maxn], d[maxn], id[maxn], cnt, top[maxn], w[maxn], wt[maxn], n, m;

inline int read(){
    int s = 0, w = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
    for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
    return s * w;
}

void add_edge(int x, int y){ edge[++num].to = y; edge[num].next = head[x]; head[x] = num; }

void dfs(int u){
	size[u] = 1, son[u] = -1;
	for (int i = head[u]; i; i = edge[i].next){
		int v = edge[i].to;
		if (v != fa[u]){
			fa[v] = u, d[v] = d[u] + 1;
			dfs(v);
			size[u] += size[v];
			if (son[u] == -1) son[u] = v; else
			if (size[son[u]] < size[v]) son[u] = v;
		}
	}
}

void dfs(int u, int x){
	id[u] = ++cnt, top[u] = x, wt[cnt] = w[u];
	if (son[u] == -1) return;
	dfs(son[u], x);
	for (int i = head[u]; i; i = edge[i].next){
		int v = edge[i].to;
		if (v != fa[u] && v != son[u]) dfs(v, v);
	}
}

void pushup(int rt){ seg[rt].m = max(seg[ls].m, seg[rs].m); seg[rt].sum = seg[ls].sum + seg[rs].sum; }

void build(int rt, int l, int r){
	seg[rt].l = l, seg[rt].r = r;
	if (l == r){
		seg[rt].sum = seg[rt].m = wt[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(ls, l, mid); build(rs, mid + 1, r);
	pushup(rt);
}

void update(int rt, int x, int change){
	if (seg[rt].l == seg[rt].r){
		seg[rt].sum = seg[rt].m = change; return;
	}
	if (seg[ls].r >= x) update(ls, x, change); else update(rs, x, change);
	pushup(rt);
}

int query_max(int rt, int l, int r){
	if (seg[rt].l > r || seg[rt].r < l) return -inf;
	if (seg[rt].l >= l && seg[rt].r <= r) return seg[rt].m;
	return max(query_max(ls, l, r), query_max(rs, l, r));
}

int query_sum(int rt, int l, int r){
	if (seg[rt].l > r || seg[rt].r < l) return 0;
	if (seg[rt].l >= l && seg[rt].r <= r) return seg[rt].sum;
	return query_sum(ls, l, r) + query_sum(rs, l ,r);
}

int Query_max(int u, int v){
	int ans = -inf;
	while (top[u] != top[v]){
		if (d[top[u]] < d[top[v]]) swap(u, v);
		ans = max(ans, query_max(1, id[top[u]], id[u]));
		u = fa[top[u]];
	}
	if (d[u] < d[v]) swap(u, v);
	return max(ans, query_max(1, id[v], id[u]));
}

int Query_sum(int u, int v){
	int ans = 0;
	while (top[u] != top[v]){
		if (d[top[u]] < d[top[v]]) swap(u, v);
		ans += query_sum(1, id[top[u]], id[u]);
		u = fa[top[u]];
	}
	if (d[u] < d[v]) swap(u, v);
	return ans + query_sum(1, id[v], id[u]);
}

int main(){
	n = read();
	for (int i = 1; i < n; ++i){
		int x = read(), y = read();
		add_edge(x, y); add_edge(y, x);
	}
	for (int i = 1; i <= n; ++i) w[i] = read();
	dfs(1); dfs(1, 1); build(1, 1, n);
	m = read();
	while (m--){
		char c = getchar();
		for (; c != 'H' && c != 'M' && c != 'S'; c = getchar());
		if (c == 'H'){
			int x = read(), y = read();
			update(1, id[x], y);
		} else
		if (c == 'M'){
			int x = read(), y = read();
			printf("%d\n", Query_max(x, y));
		} else{
			int x = read(), y = read();
			printf("%d\n", Query_sum(x, y));
		}
	}
	return 0;
}
上一篇:D-温暖的签到题


下一篇:【模板】树链剖分