結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
35,776 KB
testcase_01 AC 12 ms
35,920 KB
testcase_02 AC 12 ms
35,668 KB
testcase_03 AC 12 ms
35,892 KB
testcase_04 AC 14 ms
35,672 KB
testcase_05 AC 43 ms
36,804 KB
testcase_06 AC 495 ms
43,468 KB
testcase_07 AC 483 ms
43,400 KB
testcase_08 AC 481 ms
43,672 KB
testcase_09 AC 487 ms
43,696 KB
testcase_10 AC 15 ms
35,688 KB
testcase_11 AC 36 ms
36,804 KB
testcase_12 AC 352 ms
44,096 KB
testcase_13 AC 327 ms
44,088 KB
testcase_14 AC 87 ms
47,632 KB
testcase_15 AC 134 ms
47,576 KB
testcase_16 AC 215 ms
45,900 KB
testcase_17 AC 503 ms
43,292 KB
testcase_18 AC 480 ms
43,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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



class HeavyLightDecomposition {
private:
    const vector<vector<int>> &g;

public:
    struct Node {
        int vid;
        int head;
        int parent;
        int heavy;
        int 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;
            int 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;
    }

private:
    int dfs(int curr, int prev) {
        nodes[curr].heavy = -1;
        nodes[curr].parent = prev;
        int maxSub = 0;
        int 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;
vector<vector<int>> G;
LazySegTreeAddSum<1<<20> sst;
//-----------------------------------------------------------------------------------
int main() {
    cin >> N;
    G.resize(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);
    sst.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) {
            //printf("<%d : %lld>\n", hld[l] + 1, sst.get(l, l + 1));
            ans += sst.get(l, r + 1);
            sst.add(l, r + 1, 1);
        });
    }
    printf("%lld\n", ans);
}
0