Hướng giải của Nhánh cây
Nhớ rằng hướng dẫn giải này chỉ nên sử dụng khi bế tắc, và tuyệt đối không nên sao chép mã nguồn kèm theo. Hãy tôn trọng tác giả bài tập và người viết hướng dẫn giải.
Nộp mã nguồn lời giải chính thức trước khi giải bài tập đó có thể khiến bạn bị ban.
Nộp mã nguồn lời giải chính thức trước khi giải bài tập đó có thể khiến bạn bị ban.
Tác giả:
Lời giải cho Nhánh cây
Thuật toán
Dùng Heavy Light Decomposition để phân tách cây thành các chuỗi, mỗi truy vấn xử lý trong \(O(log^2 N)\).
HLD có tính chất quan trọng: subtree của một đỉnh là đoạn liên tục trong mảng DFS order.
Với mỗi đỉnh \(u\):
- Đầu đoạn: \(pos[u]\)
- Cuối đoạn: \(pos[u] + sz[u] - 1\)
Do đó truy vấn subtree chỉ cần một lần query trên Segment Tree: \(O(\log N)\).
Cập nhật điểm cũng \(O(\log N)\).
Độ phức tạp
\(O((N + Q) \log N)\)
Hướng dẫn giải
Sử dụng thuật toán phù hợp để giải quyết bài toán.
Mã nguồn C++
// Giải thuật hld cho bài toán hld-subtree\n#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 100005;
int n, q;
vector<int> adj[MAXN];
int parent[MAXN], depth[MAXN], sz[MAXN], heavy[MAXN];
int head[MAXN], pos[MAXN], cur_pos;
ll val[MAXN], arr[MAXN];
void dfs(int u, int p) {
parent[u] = p;
sz[u] = 1;
heavy[u] = -1;
int max_sz = 0;
for (int v : adj[u]) {
if (v == p) continue;
depth[v] = depth[u] + 1;
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > max_sz) {
max_sz = sz[v];
heavy[u] = v;
}
}
}
void decompose(int u, int h) {
head[u] = h;
pos[u] = cur_pos;
arr[cur_pos] = val[u];
cur_pos++;
if (heavy[u] != -1)
decompose(heavy[u], h);
for (int v : adj[u]) {
if (v == parent[u] || v == heavy[u]) continue;
decompose(v, v);
}
}
struct SegTree {
vector<ll> tree;
void init(int n) { tree.assign(4 * n, 0); }
void build(int nd, int tl, int tr, ll a[]) {
if (tl == tr) { tree[nd] = a[tl]; return; }
int tm = (tl + tr) / 2;
build(2*nd, tl, tm, a);
build(2*nd+1, tm+1, tr, a);
tree[nd] = tree[2*nd] + tree[2*nd+1];
}
void update(int nd, int tl, int tr, int p, ll v) {
if (tl == tr) { tree[nd] = v; return; }
int tm = (tl + tr) / 2;
if (p <= tm) update(2*nd, tl, tm, p, v);
else update(2*nd+1, tm+1, tr, p, v);
tree[nd] = tree[2*nd] + tree[2*nd+1];
}
ll query(int nd, int tl, int tr, int l, int r) {
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return tree[nd];
int tm = (tl + tr) / 2;
return query(2*nd, tl, tm, l, r) + query(2*nd+1, tm+1, tr, l, r);
}
} st;
ll subtree_sum(int u) {
return st.query(1, 0, n-1, pos[u], pos[u] + sz[u] - 1);
}
int main() {
// Doc du lieu dau vao
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> val[i];
for (int i = 0; i < n-1; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
cur_pos = 0;
decompose(1, 1);
st.init(n);
st.build(1, 0, n-1, arr);
while (q--) {
int type; cin >> type;
if (type == 1) {
int u; ll v; cin >> u >> v;
st.update(1, 0, n-1, pos[u], v);
} else {
int u; cin >> u;
cout << subtree_sum(u) << '\n';
}
}
return 0;
}
Nhận xét