結果

問題 No.900 aδδitivee
ユーザー SSRSSSRS
提出日時 2023-04-16 22:15:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 3,222 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 185,232 KB
実行使用メモリ 23,280 KB
最終ジャッジ日時 2024-04-20 08:41:17
合計ジャッジ時間 13,952 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 407 ms
22,404 KB
testcase_08 AC 411 ms
22,284 KB
testcase_09 AC 412 ms
22,272 KB
testcase_10 AC 409 ms
22,280 KB
testcase_11 AC 417 ms
22,292 KB
testcase_12 AC 406 ms
22,416 KB
testcase_13 AC 408 ms
22,260 KB
testcase_14 AC 408 ms
22,404 KB
testcase_15 AC 406 ms
22,276 KB
testcase_16 AC 409 ms
22,408 KB
testcase_17 AC 401 ms
22,412 KB
testcase_18 AC 413 ms
22,280 KB
testcase_19 AC 410 ms
22,280 KB
testcase_20 AC 403 ms
22,380 KB
testcase_21 AC 407 ms
22,296 KB
testcase_22 AC 330 ms
23,148 KB
testcase_23 AC 326 ms
23,152 KB
testcase_24 AC 326 ms
23,152 KB
testcase_25 AC 325 ms
23,280 KB
testcase_26 AC 497 ms
23,144 KB
testcase_27 AC 324 ms
23,148 KB
testcase_28 AC 324 ms
23,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct lazy_segment_tree{
  int N;
  vector<int> sz;
  vector<long long> ST, lazy;
  lazy_segment_tree(vector<long long> &A){
    int N2 = A.size();
    N = 1;
    while (N < N2){
      N *= 2;
    }
    sz.resize(N * 2 - 1, 1);
    ST.resize(N * 2 - 1, 0);
    for (int i = 0; i < N2; i++){
      ST[N - 1 + i] = A[i];
    }
    for (int i = N - 2; i >= 0; i--){
      sz[i] = sz[i * 2 + 1] + sz[i * 2 + 2];
      ST[i] = ST[i * 2 + 1] + ST[i * 2 + 2];
    }
    lazy.resize(N * 2 - 1, 0);
  }
  void push(int i){
    if (i < N - 1){
      lazy[i * 2 + 1] += lazy[i];
      lazy[i * 2 + 2] += lazy[i];
    }
    ST[i] += lazy[i] * sz[i];
    lazy[i] = 0;
  }
  void range_add(int L, int R, int x, int i, int l, int r){
    push(i);
    if (r <= L || R <= l){
    } else if (L <= l && r <= R){
      lazy[i] = x;
      push(i);
    } else {
      int m = (l + r) / 2;
      range_add(L, R, x, i * 2 + 1, l, m);
      range_add(L, R, x, i * 2 + 2, m, r);
      ST[i] = ST[i * 2 + 1] + ST[i * 2 + 2];
    }
  }
  void range_add(int L, int R, int x){
    range_add(L, R, x, 0, 0, N);
  }
  long long range_sum(int L, int R, int i, int l, int r){
    push(i);
    if (r <= L || R <= l){
      return 0;
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return range_sum(L, R, i * 2 + 1, l, m) + range_sum(L, R, i * 2 + 2, m, r);
    }
  }
  long long range_sum(int L, int R){
    return range_sum(L, R, 0, 0, N);
  }
};
int main(){
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v, w;
    cin >> u >> v >> w;
    E[u].push_back(make_pair(w, v));
    E[v].push_back(make_pair(w, u));
  }
  vector<int> p(N, -1);
  vector<long long> s(N, 0);
  vector<vector<int>> c(N);
  vector<int> b;
  queue<int> q;
  q.push(0);
  while (!q.empty()){
    int v = q.front();
    q.pop();
    b.push_back(v);
    for (pair<int, int> e : E[v]){
      int w = e.second;
      if (w != p[v]){
        p[w] = v;
        s[w] = e.first;
        c[v].push_back(w);
        q.push(w);
      }
    }
  }
  reverse(b.begin(), b.end());
  vector<int> sz(N, 1);
  for (int v : b){
    for (int &w : c[v]){
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0]]){
        swap(w, c[v][0]);
      }
    }
  }
  reverse(b.begin(), b.end());
  vector<int> in(N), out(N), next(N);
  in[0] = 0;
  out[0] = N;
  next[0] = 0;
  for (int v : b){
    int t = in[v] + 1;
    for (int w : c[v]){
      if (w == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      in[w] = t;
      t += sz[w];
      out[w] = t;
    }
  }
  vector<long long> s2(N);
  for (int i = 0; i < N; i++){
    s2[in[i]] = s[i];
  }
  lazy_segment_tree ST(s2);
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 1){
      int a, x;
      cin >> a >> x;
      ST.range_add(in[a] + 1, out[a], x);
    }
    if (t == 2){
      int b;
      cin >> b;
      long long ans = 0;
      while (true){
        ans += ST.range_sum(in[next[b]], in[b] + 1);
        if (next[b] == 0){
          break;
        }
        b = p[next[b]];
      }
      cout << ans << endl;
    }
  }
}
0