結果

問題 No.900 aδδitivee
ユーザー tnakao0123tnakao0123
提出日時 2019-10-05 20:09:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 3,422 bytes
コンパイル時間 952 ms
コンパイル使用メモリ 91,372 KB
実行使用メモリ 21,700 KB
最終ジャッジ日時 2024-10-06 23:09:31
合計ジャッジ時間 6,057 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,276 KB
testcase_01 AC 4 ms
11,292 KB
testcase_02 AC 4 ms
11,048 KB
testcase_03 AC 4 ms
10,884 KB
testcase_04 AC 4 ms
10,284 KB
testcase_05 AC 4 ms
11,224 KB
testcase_06 AC 5 ms
11,264 KB
testcase_07 AC 146 ms
21,524 KB
testcase_08 AC 145 ms
20,976 KB
testcase_09 AC 146 ms
21,464 KB
testcase_10 AC 144 ms
21,100 KB
testcase_11 AC 143 ms
21,700 KB
testcase_12 AC 142 ms
20,868 KB
testcase_13 AC 144 ms
20,988 KB
testcase_14 AC 141 ms
21,012 KB
testcase_15 AC 143 ms
21,548 KB
testcase_16 AC 143 ms
21,600 KB
testcase_17 AC 143 ms
21,508 KB
testcase_18 AC 141 ms
21,360 KB
testcase_19 AC 147 ms
20,788 KB
testcase_20 AC 142 ms
21,000 KB
testcase_21 AC 144 ms
21,588 KB
testcase_22 AC 124 ms
19,332 KB
testcase_23 AC 124 ms
19,300 KB
testcase_24 AC 125 ms
19,204 KB
testcase_25 AC 125 ms
19,300 KB
testcase_26 AC 122 ms
19,356 KB
testcase_27 AC 124 ms
19,292 KB
testcase_28 AC 124 ms
17,172 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:118:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  118 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:122:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  122 |     scanf("%d%d%d", &u, &v, &w);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:153:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  153 |   scanf("%d", &qn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:157:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  157 |     scanf("%d", &op);
      |     ~~~~~^~~~~~~~~~~
main.cpp:161:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  161 |       scanf("%d%d", &a, &x);
      |       ~~~~~^~~~~~~~~~~~~~~~
main.cpp:166:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  166 |       scanf("%d", &b);
      |       ~~~~~^~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 900.cc:  No.900 a隆隆itivee - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 19; // = 524288

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

template <typename T, const int MAX_E2>
struct SegTreeDly {
  int n, e2;
  T nodes[MAX_E2], dls[MAX_E2];
  int ss[MAX_E2];
  SegTreeDly() {}

  void init(int _n) {
    n = _n;
    for (e2 = 1; e2 < n; e2 <<= 1);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void set(int i, T v, int s) {
    int j = e2 - 1 + i;
    nodes[j] = v, ss[j] = s;
  }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--) {
      int j0 = j * 2 + 1, j1 = j0 + 1;
      nodes[j] = nodes[j0] + nodes[j1];
      ss[j] = ss[j0] + ss[j1];
    }
  }

  void _update(int k) {
    if (dls[k] != 0) {
      int k0 = k * 2 + 1, k1 = k0 + 1;
      nodes[k0] += dls[k] * ss[k0]; dls[k0] += dls[k];
      nodes[k1] += dls[k] * ss[k1]; dls[k1] += dls[k];
      dls[k] = 0;
    }
  }

  void add_range(int r0, int r1, T v, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      nodes[k] += v * ss[k];
      dls[k] += v;
      return;
    }

    _update(k);

    int im = (i0 + i1) / 2;
    int k0 = k * 2 + 1, k1 = k0 + 1;
    add_range(r0, r1, v, k0, i0, im);
    add_range(r0, r1, v, k1, im, i1);
    nodes[k] = nodes[k0] + nodes[k1];
  }
  void add_range(int r0, int r1, T v) { add_range(r0, r1, v, 0, 0, e2); }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    _update(k);

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

/* global variables */

vpii nbrs[MAX_N];
int prts[MAX_N], pws[MAX_N], cns[MAX_N], rls[MAX_N], rrs[MAX_N];
SegTreeDly<ll,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    nbrs[u].push_back(pii(v, w));
    nbrs[v].push_back(pii(u, w));
  }

  st.init(2 * n);
  prts[0] = -1; pws[0] = 0;
  rls[0] = 0;

  for (int u = 0, k = 0; u >= 0;) {
    int &up = prts[u], &cnu = cns[u];
    vpii &nbru = nbrs[u];

    if (cnu < nbru.size() && nbru[cnu].first == up) cnu++;
    if (cnu >= nbru.size()) {
      rrs[u] = k;
      st.set(k++, -pws[u], -1);
      u = up;
    }
    else {
      int &v = nbru[cnu].first, &w = nbru[cnu].second;
      cnu++;
      prts[v] = u, pws[v] = w;
      st.set(k++, w, 1);
      rls[v] = k;
      u = v;
    }
  }
  st.setall();

  int qn;
  scanf("%d", &qn);

  while (qn--) {
    int op;
    scanf("%d", &op);

    if (op == 1) {
      int a, x;
      scanf("%d%d", &a, &x);
      if (x > 0) st.add_range(rls[a], rrs[a], x);
    }
    else {
      int b;
      scanf("%d", &b);

      ll sum = st.sum_range(0, rls[b]);
      printf("%lld\n", sum);
    }
  }
  return 0;
}
0