Hướng giải của Đa truy vấn


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.

Tác giả: kienadmin

Lời giải cho Đa truy vấn

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)\).

Kết hợp HLD với Segment Tree tổng để hỗ trợ cả 3 loại truy vấn:

  1. Cập nhật điểm: update trên SegTree tại \(pos[u]\).
  2. Path sum: nhảy chain + query SegTree.
  3. Subtree sum: query SegTree trên đoạn \([pos[u], pos[u] + sz[u] - 1]\).

Tận dụng tính chất: subtree là đoạn liên tục.

Độ phức tạp

\(O((N + Q) \log^2 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-all-path\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 path_sum(int u, int v) {
    ll res = 0;
    while (head[u] != head[v]) {
        if (depth[head[u]] > depth[head[v]]) swap(u, v);
        res += st.query(1, 0, n-1, pos[head[v]], pos[v]);
        v = parent[head[v]];
    }
    if (depth[u] > depth[v]) swap(u, v);
    res += st.query(1, 0, n-1, pos[u], pos[v]);
    return res;
}

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 if (type == 2) {
            int u, v; cin >> u >> v;
            cout << path_sum(u, v) << '\n';
        } else {
            int u; cin >> u;
            cout << subtree_sum(u) << '\n';
        }
    }
    return 0;
}

Nhận xét

Không có ý kiến tại thời điểm này.