結果

問題 No.399 動的な領主
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-06-10 14:04:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 4,105 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 188,164 KB
実行使用メモリ 57,436 KB
最終ジャッジ日時 2023-10-24 20:27:16
合計ジャッジ時間 7,957 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
36,000 KB
testcase_01 AC 11 ms
36,000 KB
testcase_02 AC 11 ms
36,000 KB
testcase_03 AC 10 ms
36,000 KB
testcase_04 AC 12 ms
36,000 KB
testcase_05 AC 40 ms
37,704 KB
testcase_06 AC 464 ms
52,684 KB
testcase_07 AC 452 ms
52,684 KB
testcase_08 AC 458 ms
52,684 KB
testcase_09 AC 476 ms
52,684 KB
testcase_10 AC 15 ms
36,000 KB
testcase_11 AC 35 ms
37,704 KB
testcase_12 AC 347 ms
52,684 KB
testcase_13 AC 345 ms
52,684 KB
testcase_14 AC 89 ms
57,436 KB
testcase_15 AC 129 ms
57,436 KB
testcase_16 AC 209 ms
55,324 KB
testcase_17 AC 499 ms
52,684 KB
testcase_18 AC 481 ms
52,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
struct HLDecomposition {
    vector<set<int>> g;
    // vid : id -> vid          head, heavy, parent : unknown
    // depth : id -> depth      inv : vid -> id
    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) {}
    void add(int u, int v) { g[u].insert(v); g[v].insert(u); }
    void build(int root) { dfs(root, -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);}}}
    void foreach(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]) foreach(u, parent[head[v]], f);}
    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のd個上の頂点を返す(無いなら0)
    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]]); }
    int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }};
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)の総和
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/





ll ans = 0;
LazySegTreeAddSum<1 << 20> st;
//---------------------------------------------------------------------------------------------------
void _main() {
    int N; cin >> N;
    HLDecomposition hld(N);
    rep(i, 0, N - 1) {
        int a, b; cin >> a >> b; a--; b--;
        hld.add(a, b);
    }
    hld.build(0);
    st.add(0, N, 1);

    int Q; cin >> Q;
    rep(q, 0, Q) {
        int a, b; cin >> a >> b; a--; b--;

        hld.foreach(a, b, [](int x, int y) {
            ans += st.get(x, y + 1);
            st.add(x, y + 1, 1);
        });
    }
    cout << ans << endl;
}
0