結果

問題 No.1637 Easy Tree Query
ユーザー hayatenhayaten
提出日時 2021-08-06 21:42:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 1,839 bytes
コンパイル時間 5,151 ms
コンパイル使用メモリ 285,292 KB
実行使用メモリ 13,332 KB
最終ジャッジ日時 2023-10-17 05:04:26
合計ジャッジ時間 10,402 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 2 ms
4,688 KB
testcase_02 AC 125 ms
12,896 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 44 ms
7,872 KB
testcase_05 AC 92 ms
9,436 KB
testcase_06 AC 60 ms
7,588 KB
testcase_07 AC 28 ms
6,012 KB
testcase_08 AC 35 ms
7,184 KB
testcase_09 AC 90 ms
10,184 KB
testcase_10 AC 45 ms
7,124 KB
testcase_11 AC 113 ms
11,904 KB
testcase_12 AC 106 ms
10,912 KB
testcase_13 AC 24 ms
6,292 KB
testcase_14 AC 69 ms
9,640 KB
testcase_15 AC 112 ms
12,028 KB
testcase_16 AC 94 ms
11,252 KB
testcase_17 AC 33 ms
6,668 KB
testcase_18 AC 89 ms
9,368 KB
testcase_19 AC 87 ms
9,744 KB
testcase_20 AC 110 ms
10,868 KB
testcase_21 AC 64 ms
8,824 KB
testcase_22 AC 131 ms
12,748 KB
testcase_23 AC 36 ms
6,932 KB
testcase_24 AC 57 ms
8,352 KB
testcase_25 AC 92 ms
9,292 KB
testcase_26 AC 118 ms
11,164 KB
testcase_27 AC 148 ms
12,632 KB
testcase_28 AC 68 ms
8,924 KB
testcase_29 AC 94 ms
10,500 KB
testcase_30 AC 49 ms
7,856 KB
testcase_31 AC 62 ms
7,368 KB
testcase_32 AC 50 ms
7,824 KB
testcase_33 AC 83 ms
8,640 KB
testcase_34 AC 65 ms
13,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region Macros
// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define popcount(bit) __builtin_popcount(bit)
#define popcountll(bit) __builtin_popcountll(bit)
#define pb push_back
#define eb emplace_back
using namespace std;
using namespace atcoder;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Graph = vector<vector<int>>;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int fx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int fy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll INF = (1LL <<60);

Graph G;
vector<ll> ans(1e5, 0);
vector<int> depth(1e5);

void dfs(int u, int p){
  if(p != -1) ans[u] += ans[p];
  for(auto v: G[u]){
    if(v == p) continue;
    dfs(v, u);
  }
}

int dfs2(int u, int p){
  depth[u] = 1;
  for(auto v: G[u]){
    if(v == p) continue;
    depth[u] += dfs2(v, u);
  }
  return depth[u];
}

int main() {
  int n, q;
  cin >> n >> q;
  G.resize(n);
  rep(i, n-1){
    int a, b;
    cin >> a >> b;
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<PL> res;
  vector<ll> re(q);
  rep(i,q) {
    int p;
    ll x;
    cin >> p >> x;
    p--;
    ans[p] += x;
    res.push_back({p, x});
  }
  dfs(0, -1);
  dfs2(0, -1);
  ll sum = accumulate(ans.begin(), ans.end(), 0LL);
  rrep(i, q){
    //cout << sum << endl;
    re[i] = sum;
    sum -= res[i].second * depth[res[i].first];
  }
  rep(i, q){
    cout << re[i] << endl;
  }
}
0