結果

問題 No.1637 Easy Tree Query
ユーザー ktr216
提出日時 2021-08-06 21:26:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 494 ms / 2,000 ms
コード長 961 bytes
コンパイル時間 1,707 ms
コンパイル使用メモリ 174,228 KB
実行使用メモリ 256,644 KB
最終ジャッジ日時 2024-09-17 03:35:39
合計ジャッジ時間 16,338 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;

int N, MAX_N = 2000001, idx = 0, MAX_LOG_V = 20;
vector<vector<int>> edge(MAX_N), parent(MAX_LOG_V, vector<int>(MAX_N, -1));
vector<int> tp(MAX_N), l(MAX_N), r(MAX_N), par(MAX_N, -1), depth(MAX_N, 0);
vector<bool> done(MAX_N, false);

void tps(int u) {
    done[u] = true;
    l[u] = idx;
    tp[idx] = u;
    idx++;
    rep(i, 0, edge[u].size()) {
        int v = edge[u][i];
        if (done[v]) continue;
        par[v] = u;
        depth[v] = depth[u] + 1;
        tps(v);
    }
    r[u] = idx;
}

int main() {
    int Q, a, b, p;
    cin >> N >> Q;
    rep(i, 0, N - 1) {
        cin >> a >> b;
        edge[a - 1].push_back(b - 1);
        edge[b - 1].push_back(a - 1);
    }
    tps(0);
    ll x, ans = 0;
    rep(i, 0, Q) {
        cin >> p >> x;
        p--;
        ans += x * (r[p] - l[p]);
        cout << ans << endl;
    }
}
0