結果

問題 No.1038 TreeAddQuery
ユーザー potoooooooopotoooooooo
提出日時 2020-04-25 21:06:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,286 ms / 4,000 ms
コード長 3,504 bytes
コンパイル時間 2,802 ms
コンパイル使用メモリ 219,244 KB
実行使用メモリ 162,540 KB
最終ジャッジ日時 2023-08-16 15:02:07
合計ジャッジ時間 31,770 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,120 KB
testcase_01 AC 7 ms
13,916 KB
testcase_02 AC 7 ms
14,076 KB
testcase_03 AC 18 ms
15,920 KB
testcase_04 AC 20 ms
15,884 KB
testcase_05 AC 19 ms
16,128 KB
testcase_06 AC 17 ms
15,744 KB
testcase_07 AC 22 ms
16,104 KB
testcase_08 AC 1,383 ms
119,528 KB
testcase_09 AC 1,545 ms
124,868 KB
testcase_10 AC 1,570 ms
125,784 KB
testcase_11 AC 1,596 ms
125,492 KB
testcase_12 AC 1,606 ms
126,416 KB
testcase_13 AC 2,286 ms
162,540 KB
testcase_14 AC 2,004 ms
135,236 KB
testcase_15 AC 1,899 ms
132,428 KB
testcase_16 AC 1,845 ms
131,044 KB
testcase_17 AC 1,810 ms
130,036 KB
testcase_18 AC 485 ms
70,416 KB
testcase_19 AC 613 ms
75,844 KB
testcase_20 AC 492 ms
76,588 KB
testcase_21 AC 616 ms
78,004 KB
testcase_22 AC 817 ms
93,024 KB
testcase_23 AC 878 ms
94,344 KB
testcase_24 AC 1,267 ms
124,036 KB
testcase_25 AC 2,205 ms
162,376 KB
testcase_26 AC 1,382 ms
117,320 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
メンバ関数 ‘Fenwick<long long int>& Fenwick<long long int>::operator=(Fenwick<long long int>&&)’ 内,
    inlined from ‘Info& Info::operator=(Info&&)’ at main.cpp:24:8,
    inlined from ‘centroid(int)::<ラムダ(int, int)>’ at main.cpp:67:30:
main.cpp:6:26: 警告: ‘<名前なし>.Info::fw.Fenwick<long long int>::N’ may be used uninitialized [-Wmaybe-uninitialized]
    6 | template<class T> struct Fenwick {
      |                          ^~~~~~~
main.cpp: ラムダ関数内:
main.cpp:67:30: 備考: ‘<無名>’ はここで宣言されています
   67 |         F[curr][root] = Info();
      |                              ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define MAX_SIZE 100010

// zero-based numbering
template<class T> struct Fenwick {
    vector<T> bit; int N;
    Fenwick() {}
    Fenwick(int n) : N(n) { bit.assign(n+1, 0); }
    // add w to a
    void add(int a, T w) {
        for (int x = ++a; x <= N; x += x & -x) bit[x] += w;
    }
    // sum [0, a)
    T sum(int a) {
        T ret = 0;
        for (int x = a; x > 0; x -= x & -x) ret += bit[x];
        return ret;
    }
    // sum [a, b)
    T sum(int a, int b) { return sum(b) - sum(a); }
};

struct Info {
    vector<int> h;
    Fenwick<long long> fw;
    int max_h = 0;
    Info() : h{{0}} {}
    Info(int sz) : fw(sz+2), h{{0}} {};
};

vector<int> e[MAX_SIZE];
int sz[MAX_SIZE];
bool cut[MAX_SIZE];
unordered_map<int, Info> F[MAX_SIZE];
vector<array<int, 4>> v[MAX_SIZE]; 

int find_centroid(int curr) {
    auto dfs = [&](auto f, int curr, int prev) -> void {
        sz[curr] = 1;
        for (auto &to: e[curr]) {
            if (to == prev || cut[to]) continue;
            f(f, to, curr);
            sz[curr] += sz[to];
        }
    };
    dfs(dfs, curr, -1);
    int all = sz[curr];
    while (true) {
        int next = -1;
        for (auto &to: e[curr]) {
            if (cut[to] || sz[to] > sz[curr]) continue;
            if (sz[to] * 2 >= all) next = to;
        }
        if (next == -1) break;
        curr = next;
    }
    cut[curr] = true;
    return curr;
}

long long cntt = 0;

void centroid(int curr) {
    curr = find_centroid(curr);
    auto bfs = [&](int root, int prev) {
        F[curr][root] = Info();
        cntt += sz[root];
        auto &u = F[curr][root];
        queue<array<int, 3>> q;
        q.push({root, prev, 0});
        int cnt = 0;
        while (!q.empty()) {
            auto c = q.front(); q.pop();
            v[c[0]].push_back({curr, root, cnt, c[2]});
            if (u.max_h < c[2]) {
                u.max_h = c[2];
                u.h.emplace_back(0);
            }
            u.h.back() = cnt++;
            for (auto &x: e[c[0]]) {
                if (x == c[1] || cut[x]) continue;
                q.push({x, c[0], c[2]+1});
            }
        }
        F[curr][root].fw = Fenwick<long long>(cnt);
    };
    bfs(curr, -1);
    for (auto &to: e[curr]) {
        if (cut[to]) continue;
        bfs(to, curr);
    }
    for (auto &to: e[curr]) {
        if (!cut[to]) centroid(to);
    }
}

long long f(int x) {
    long long ans = 0;
    for (auto &[c, r, i, h] : v[x]) {
        ans += F[c][r].fw.sum(i+1);
    }
    return ans;
}

long long query(int x, int y, long long z) {
    long long ans = 0;
    for (auto &[c, r, i, h] : v[x]) {
        ans += F[c][r].fw.sum(i+1);
        int d = (c == r) ? y-h : y-h-2;
        d = min(d, F[c][r].max_h);
        if (d >= 0) {
            long long w = (c == r) ? z : -z;
            F[c][r].fw.add(0, w);
            F[c][r].fw.add(F[c][r].h[d]+1, -w);
        }
    }
    return ans;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q; cin >> n >> q;
    for (int i = 0; i < n-1; i++) {
        int u, v; cin >> u >> v; u--; v--;
        e[u].emplace_back(v);
        e[v].emplace_back(u);
    }
    centroid(0);
    
    auto debug = [&]() {
        for (int j = 0; j < n; j++) cout << f(j) << " ";
        cout << "\n";
    };

    for (int i = 0; i < q; i++) {
        int x, y; long long z;
        cin >> x >> y >> z;
        cout << query(--x, y, z) << "\n";
        // debug();
    }
    return 0;
}
0