結果

問題 No.399 動的な領主
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-04-06 11:26:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 580 ms / 2,000 ms
コード長 3,357 bytes
コンパイル時間 2,054 ms
コンパイル使用メモリ 180,032 KB
実行使用メモリ 47,676 KB
最終ジャッジ日時 2023-09-22 23:01:04
合計ジャッジ時間 8,385 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
35,616 KB
testcase_01 AC 13 ms
35,712 KB
testcase_02 AC 12 ms
35,912 KB
testcase_03 AC 13 ms
35,752 KB
testcase_04 AC 15 ms
35,848 KB
testcase_05 AC 45 ms
36,760 KB
testcase_06 AC 571 ms
43,376 KB
testcase_07 AC 558 ms
43,376 KB
testcase_08 AC 568 ms
43,640 KB
testcase_09 AC 579 ms
43,568 KB
testcase_10 AC 16 ms
35,752 KB
testcase_11 AC 36 ms
36,688 KB
testcase_12 AC 380 ms
44,116 KB
testcase_13 AC 363 ms
44,172 KB
testcase_14 AC 89 ms
47,676 KB
testcase_15 AC 139 ms
47,584 KB
testcase_16 AC 223 ms
45,716 KB
testcase_17 AC 580 ms
43,228 KB
testcase_18 AC 561 ms
43,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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



struct HeavyLightDecomposition {
    const vector<vector<int>> &g;
    struct Node { int vid, head, parent, heavy, length; };
    vector<Node> nodes;
    HeavyLightDecomposition(const vector<vector<int>> &g) : g(g), nodes(g.size()) { dfs(0, -1); bfs(0); }
    void forEach(int u, int v, function<void(int, int, int, int)> f) {
        while (true) {
            if (nodes[u].vid > nodes[v].vid) swap(u, v);
            int h = nodes[v].head, l = nodes[h].length;
            if (nodes[u].head == nodes[v].head) { f(nodes[h].vid, l, nodes[u].vid, nodes[v].vid); break;
            } else { f(nodes[h].vid, l, nodes[h].vid, nodes[v].vid); v = nodes[h].parent; }
        }
    }
    int operator[](int k) { return nodes[k].vid; }
    int dfs(int curr, int prev) {
        nodes[curr].heavy = -1; nodes[curr].parent = prev; int maxSub = 0, sub = 1;
        for (int next : g[curr]) {
            if (next == prev) continue;
            int subNext = dfs(next, curr); sub += subNext;
            if (maxSub < subNext) { maxSub = subNext; nodes[curr].heavy = next; }
        }
        return sub;
    }
    void bfs(int s) {
        queue<int> q; q.push(s); int k = 0;
        while (!q.empty()) {
            int h = q.front(); q.pop();
            for (int i = h; i != -1; i = nodes[i].heavy) {
                nodes[i].vid = k++; nodes[i].head = h; nodes[h].length++;
                for (int j : g[i]) if (j != nodes[i].parent && j != nodes[i].heavy)q.push(j);
            }
        }
    }
};

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)の総和
};
//-----------------------------------------------------------------------------------
int N, Q;
LazySegTreeAddSum<1<<20> st;
//-----------------------------------------------------------------------------------
int main() {
    cin >> N;

    vector<vector<int>> G(N);
    rep(i, 0, N - 1) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    HeavyLightDecomposition hld(G);
    st.add(0, N, 1);

    ll ans = 0;
    cin >> Q;
    rep(q, 0, Q) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--; b--;

        hld.forEach(a, b, [&](int head, int len, int l, int r) {
            ans += st.get(l, r + 1);
            st.add(l, r + 1, 1);
        });
    }
    printf("%lld\n", ans);
}
0