結果

問題 No.399 動的な領主
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-04-10 20:33:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 574 ms / 2,000 ms
コード長 5,240 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 181,820 KB
実行使用メモリ 48,696 KB
最終ジャッジ日時 2023-09-25 06:53:22
合計ジャッジ時間 7,995 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
35,720 KB
testcase_01 AC 12 ms
35,924 KB
testcase_02 AC 12 ms
35,836 KB
testcase_03 AC 12 ms
35,852 KB
testcase_04 AC 14 ms
35,836 KB
testcase_05 AC 45 ms
36,696 KB
testcase_06 AC 559 ms
43,788 KB
testcase_07 AC 546 ms
43,820 KB
testcase_08 AC 551 ms
43,800 KB
testcase_09 AC 554 ms
43,876 KB
testcase_10 AC 16 ms
35,904 KB
testcase_11 AC 36 ms
36,824 KB
testcase_12 AC 387 ms
44,360 KB
testcase_13 AC 373 ms
44,400 KB
testcase_14 AC 88 ms
48,696 KB
testcase_15 AC 138 ms
48,576 KB
testcase_16 AC 219 ms
46,444 KB
testcase_17 AC 574 ms
43,808 KB
testcase_18 AC 556 ms
43,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)




typedef long long ll;
template<int NV> struct LazySegTreeAddSum {
    vector<ll> a, b;
    explicit LazySegTreeAddSum() { a.resize(2 * NV - 1); b.resize(2 * NV - 1); }

    void add(int i, int il, int ir, int l, int r, int z) {
        if (l <= il and ir <= r) { a[i] += z; b[i] += z * (ir - il); }
        else if (ir <= l or r <= il) {}
        else {
            add(2 * i + 1, il, (il + ir) / 2, l, r, z);
            add(2 * i + 2, (il + ir) / 2, ir, l, r, z);
            b[i] = a[i] * (ir - il) + b[2 * i + 1] + b[2 * i + 2];
        }
    }

    ll get(int i, int il, int ir, int l, int r) {
        if (l <= il and ir <= r) return b[i];
        else if (ir <= l or r <= il) return 0;
        else return a[i] * (min(ir, r) - max(il, l)) + get(2 * i + 1, il, (il + ir) / 2, l, r) + get(2 * i + 2, (il + ir) / 2, ir, l, r);
    }
    void add(int l, int r, int z) { add(0, 0, NV, l, r, z); } // [l,r)に+z
    ll get(int l, int r) { return get(0, 0, NV, l, r); } // [l,r)の総和
};
//-----------------------------------------------------------------------------------
struct HLDecomposition {
    vector<vector<int>> g;

    // vid, head, heavy, parent は必須
    // depth, inv は使用する機能によっては不要
    vector<int> vid, head, heavy, parent, depth, inv;

    HLDecomposition(int n) : g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {}

    // 辺 (u, v) を追加する
    void add(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }

    // 構築する
    void build() {
        dfs(0, -1);
        bfs();
    }

    int dfs(int curr, int prev) {
        parent[curr] = prev;
        int sub = 1, max_sub = 0;
        for (int next : g[curr]) if (next != prev) {
            depth[next] = depth[curr] + 1;
            int sub_next = dfs(next, curr);
            sub += sub_next;
            if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;
        }
        return sub;
    }

    void bfs() {
        int k = 0;
        queue<int> q({ 0 });
        while (!q.empty()) {
            int h = q.front(); q.pop();
            for (int i = h; i != -1; i = heavy[i]) {
                vid[i] = k++;
                inv[vid[i]] = i;
                head[i] = h;
                for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j);
            }
        }
    }

    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //   以下の関数は必要に応じて実装
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    // 頂点属性の for_each
    void for_each(int u, int v, function<void(int, int)> f) {
        if (vid[u] > vid[v]) swap(u, v);
        f(max(vid[head[v]], vid[u]), vid[v]);
        if (head[u] != head[v]) for_each(u, parent[head[v]], f);
    }

    // 頂点属性の for_each (有向
    // fの3番目の引数には順方向なら0、逆方向なら1が渡される
    void for_each_directed(int u, int v, function<void(int, int, int)> f) {
        if (vid[u] > vid[v]) {
            f(max(vid[head[u]], vid[v]), vid[u], 1);
            if (head[u] != head[v]) for_each_directed(parent[head[u]], v, f);
        }
        else {
            f(max(vid[head[v]], vid[u]), vid[v], 0);
            if (head[u] != head[v]) for_each_directed(u, parent[head[v]], f);
        }
    }

    // 辺属性の for_each
    void for_each_edge(int u, int v, function<void(int, int)> f) {
        if (vid[u] > vid[v]) swap(u, v);
        if (head[u] != head[v]) {
            f(vid[head[v]], vid[v]);
            for_each_edge(u, parent[head[v]], f);
        }
        else {
            if (u != v) f(vid[u] + 1, vid[v]);
        }
    }

    // 頂点 u の d 個上の頂点を求める(存在しないなら0を返す)
    int ancestor(int u, int d) {
        while (true) {
            if (depth[head[u]] > depth[u] - d) {
                d -= depth[u] - depth[head[u]] + 1;
                if (head[u] == 0) return 0;
                u = parent[head[u]];
            }
            else {
                return inv[vid[u] - d];
            }
        }
    }

    // 頂点 u と頂点 v の LCA を求める
    int lca(int u, int v) {
        if (vid[u] > vid[v]) swap(u, v);
        if (head[u] == head[v]) return u;
        return lca(u, parent[head[v]]);
    }

    // 頂点 u と頂点 v の距離を求める
    int distance(int u, int v) {
        return depth[u] + depth[v] - 2 * depth[lca(u, v)];
    }
};
//-----------------------------------------------------------------------------------
int N;
LazySegTreeAddSum<1 << 20> st;
//-----------------------------------------------------------------------------------
int main() {
    cin >> N;
    HLDecomposition hld(N);
    rep(i, 0, N - 1) {
        int a, b; scanf("%d%d", &a, &b); a--; b--;
        hld.add(a, b);
    }

    hld.build();

    st.add(0, N, 1);

    int Q; cin >> Q;
    ll ans = 0;
    rep(q, 0, Q) {
        int a, b; scanf("%d%d", &a, &b); a--; b--;
        hld.for_each(a, b, [&](int u, int v) {
            ans += st.get(u, v + 1);
            st.add(u, v + 1, 1);
        });
    }
    cout << ans << endl;
}
0