洛谷 [ZJOI2008]树的统计
题意
给定一棵树,有 n n n 个结点,每个结点都是一个权值 a [ i ] a[i] a[i] ,有 m m m 次操作。
- 把结点 u u u 的权值改为 t t t 。
- 询问从点 u u u 到点 v v v 的路径上结点的最大权值。
- 询问从点 u u u 到点 v v v 的路径上结点的权值和。
( n ≤ 3 ⋅ 1 0 4 , m ≤ 2 ⋅ 1 0 5 n\le 3\cdot 10^4,m\le 2 \cdot 10^5 n≤3⋅104,m≤2⋅105)
解法
-
树链剖分模板题。
-
维护树上路径的信息,考虑树链剖分。
-
用线段树维护单点修改,区间查询权值和以及最大权值。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (sign)
x = -x;
return true;
}
inline bool R(double &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (ch == '.') {
double tmp = 1;
ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
tmp /= 10.0, x += tmp * (ch - '0');
}
if (sign)
x = -x;
return true;
}
inline bool R(char *s) {
char ch = nc();
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
for (; !blank(ch) && !IOerror; ch = nc())
*s++ = ch;
*s = 0;
return true;
}
inline bool R(char &c) {
c = nc();
if (IOerror) {
c = -1;
return false;
}
return true;
}
template <class T, class... U>
bool R(T &h, U &... tmp) { return R(h) && R(tmp...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 3e4 + 5;
int n, m, a[maxn];
vector<int> g[maxn];
int son[maxn], fa[maxn], dep[maxn], sz[maxn];
int cnt, id[maxn], wt[maxn], top[maxn];
void dfs1(int u, int f, int deep) {
fa[u] = f, dep[u] = deep, sz[u] = 1;
for (auto v : g[u]) {
if (v == f) continue;
dfs1(v, u, deep + 1);
sz[u] += sz[v];
if (sz[son[u]] < sz[v]) son[u] = v;
}
}
void dfs2(int u, int topf) {
id[u] = ++cnt, wt[cnt] = a[u], top[u] = topf;
if (!son[u]) return;
dfs2(son[u], topf);
for (auto v : g[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
struct node {
int l, r, val, maxx;
} t[maxn << 2];
void build(int root, int l, int r) {
tr.l = l, tr.r = r;
if (l == r) {
tr.val = tr.maxx = wt[l];
return;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
tr.val = lson.val + rson.val;
tr.maxx = max(lson.maxx, rson.maxx);
}
void update(int root, int pos, int x) {
if (tr.l == tr.r) {
tr.maxx = tr.val = x;
return;
}
int mid = (tr.l + tr.r) >> 1;
if (pos <= mid) update(root << 1, pos, x);
if (pos > mid) update(root << 1 | 1, pos, x);
tr.val = lson.val + rson.val;
tr.maxx = max(lson.maxx, rson.maxx);
}
int qSum(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) return tr.val;
int ans = 0;
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) ans += qSum(root << 1, l, r);
if (r > mid) ans += qSum(root << 1 | 1, l, r);
return ans;
}
int qMax(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) return tr.maxx;
int ans = -0x3f3f3f3f;
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) ans = max(ans, qMax(root << 1, l, r));
if (r > mid) ans = max(ans, qMax(root << 1 | 1, l, r));
return ans;
}
int qSum(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans = (ans + qSum(1, id[top[x]], id[x]));
x = fa[top[x]];
}
if (dep[x] < dep[y]) swap(x, y);
ans = (ans + qSum(1, id[y], id[x]));
return ans;
}
int qMax(int x, int y) {
int ans = -0x3f3f3f3f;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans = max(ans, qMax(1, id[top[x]], id[x]));
x = fa[top[x]];
}
if (dep[x] < dep[y]) swap(x, y);
ans = max(ans, qMax(1, id[y], id[x]));
return ans;
}
int main() {
R(n);
rep(i, 1, n - 1) {
int u, v;
R(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
rep(i, 1, n) R(a[i]);
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
R(m);
while (m--) {
char op[20];
R(op + 1);
int u, v;
R(u, v);
if (op[2] == 'M') W(qMax(u, v));
if (op[2] == 'S') W(qSum(u, v));
if (op[2] == 'H') update(1, id[u], v);
}
}