結果

問題 No.1637 Easy Tree Query
ユーザー hayatenhayaten
提出日時 2021-08-06 21:42:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 1,839 bytes
コンパイル時間 4,692 ms
コンパイル使用メモリ 278,104 KB
最終ジャッジ日時 2025-01-23 14:55:37
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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