結果

問題 No.900 aδδitivee
ユーザー 鴨志田卓鴨志田卓
提出日時 2024-03-14 11:06:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,546 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 206,004 KB
実行使用メモリ 27,608 KB
最終ジャッジ日時 2024-03-14 11:06:23
合計ジャッジ時間 9,016 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
15,836 KB
testcase_01 AC 5 ms
15,840 KB
testcase_02 AC 5 ms
15,844 KB
testcase_03 AC 5 ms
15,844 KB
testcase_04 AC 5 ms
15,844 KB
testcase_05 AC 5 ms
15,844 KB
testcase_06 AC 5 ms
15,844 KB
testcase_07 AC 131 ms
22,440 KB
testcase_08 AC 128 ms
22,456 KB
testcase_09 AC 125 ms
22,448 KB
testcase_10 AC 129 ms
22,448 KB
testcase_11 AC 132 ms
22,448 KB
testcase_12 AC 123 ms
22,448 KB
testcase_13 AC 128 ms
22,448 KB
testcase_14 AC 127 ms
22,440 KB
testcase_15 AC 125 ms
22,444 KB
testcase_16 AC 125 ms
22,448 KB
testcase_17 AC 125 ms
22,452 KB
testcase_18 AC 125 ms
22,440 KB
testcase_19 AC 126 ms
22,448 KB
testcase_20 AC 136 ms
22,448 KB
testcase_21 AC 131 ms
22,448 KB
testcase_22 AC 105 ms
27,604 KB
testcase_23 AC 115 ms
27,608 KB
testcase_24 AC 113 ms
27,608 KB
testcase_25 AC 115 ms
27,608 KB
testcase_26 AC 105 ms
27,604 KB
testcase_27 AC 115 ms
27,608 KB
testcase_28 AC 119 ms
27,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;
using pii = pair<int, int>;
const int N = 1 << 18;

int n, dfn[N], sz[N], dep[N], dcnt;

vector<pii> g[N];

i64 tr[N * 4], buff[N * 4];

void modify(int i, int l, int r, int ql, int qr, int x, int y) {
    if(l >= qr || r <= ql) return;
    if(l >= ql && r <= qr) {
        tr[i] += x;
        buff[i] += y;
        return;
    }
    int mid = (l + r) / 2;
    modify(i * 2, l, mid, ql, qr, x, y);
    modify(i*2+1, mid, r, ql, qr, x, y);
}

void dfs(int u) {
    sz[u] = 1;
    dfn[u] = ++dcnt;

    for(auto [v, w] : g[u]) {
        dep[v] = dep[u] + 1;
        dfs(v);
        
        sz[u] += sz[v];
        modify(1, 1, n + 1, dfn[v], dfn[v] + sz[v], w, 0);
    }
}

i64 query(int i, int l, int r, int p, int d) {
    if(l + 1 == r) return tr[i] + d * buff[i];
    int mid = (l + r) / 2;
    if(p < mid) return tr[i] + d * buff[i] + query(i * 2, l, mid, p, d);
    else return tr[i] + d * buff[i] + query(i * 2 + 1, mid, r, p, d);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for(int i = 1, u, v, w; i < n; i ++) {
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
    }
    
    dfs(0);

    int q;
    cin >> q;

    for(int i = 0, op, x, y; i < q; i ++) {
        cin >> op;
        if(op == 1) {
            cin >> x >> y;
            modify(1, 1, n + 1, dfn[x], dfn[x] + sz[x], -y * dep[x], y);
        }else{
            cin >> x;
            cout << query(1, 1, n + 1, dfn[x], dep[x]) << "\n";
        }

    }
}
0