結果
問題 | No.900 aδδitivee |
ユーザー | QCFium |
提出日時 | 2019-10-05 13:48:48 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 189 ms / 2,000 ms |
コード長 | 1,876 bytes |
コンパイル時間 | 1,754 ms |
コンパイル使用メモリ | 177,692 KB |
実行使用メモリ | 24,188 KB |
最終ジャッジ日時 | 2024-04-15 17:45:47 |
合計ジャッジ時間 | 7,537 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 185 ms
21,588 KB |
testcase_08 | AC | 177 ms
21,624 KB |
testcase_09 | AC | 178 ms
21,600 KB |
testcase_10 | AC | 174 ms
21,584 KB |
testcase_11 | AC | 175 ms
21,480 KB |
testcase_12 | AC | 188 ms
21,684 KB |
testcase_13 | AC | 189 ms
21,492 KB |
testcase_14 | AC | 180 ms
21,584 KB |
testcase_15 | AC | 176 ms
21,516 KB |
testcase_16 | AC | 174 ms
21,628 KB |
testcase_17 | AC | 182 ms
21,640 KB |
testcase_18 | AC | 182 ms
21,632 KB |
testcase_19 | AC | 179 ms
21,552 KB |
testcase_20 | AC | 179 ms
21,548 KB |
testcase_21 | AC | 179 ms
21,544 KB |
testcase_22 | AC | 171 ms
23,988 KB |
testcase_23 | AC | 169 ms
24,052 KB |
testcase_24 | AC | 169 ms
24,136 KB |
testcase_25 | AC | 173 ms
24,132 KB |
testcase_26 | AC | 168 ms
24,048 KB |
testcase_27 | AC | 171 ms
24,136 KB |
testcase_28 | AC | 171 ms
24,188 KB |
ソースコード
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } std::vector<std::vector<std::pair<int, int> > > hen; std::vector<int64_t> toured; std::vector<int64_t> dist; std::vector<int64_t> depth; void dfs(int i) { toured.push_back(i); for (auto j : hen[i]) { if (!j.first || depth[j.first]) continue; dist[j.first] = dist[i] + j.second; depth[j.first] = depth[i] + 1; dfs(j.first); toured.push_back(i); } } struct SegTree { int n; std::vector<std::pair<int64_t, int64_t> > data; // depth * first + second SegTree(int n_) { for (n = 1; n < n_; n <<= 1); data.resize(2 * n); } void add(int l, int r, int64_t first, int64_t second) { for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (r & 1) data[--r].first += first, data[r].second += second; if (l & 1) data[l].first += first, data[l++].second += second; } } std::pair<int64_t, int64_t> get(int i) { std::pair<int64_t, int64_t> res = {0, 0}; for (i += n; i; i >>= 1) res.first += data[i].first, res.second += data[i].second; return res; } }; int main() { int n = ri(); hen.resize(n); for (int i = 0; i + 1 < n; i++) { int a = ri(); int b = ri(); int c = ri(); hen[a].push_back({b, c}); hen[b].push_back({a, c}); } dist.resize(n); depth.resize(n); dfs(0); std::vector<std::pair<int, int> > lr(n, {1000000000, -1000000000}); for (int i = 0; i < (int) toured.size(); i++) { int j = toured[i]; lr[j].first = std::min(lr[j].first, i); lr[j].second = std::max(lr[j].second, i); } SegTree tree(toured.size()); int q = ri(); for (int i = 0; i < q; i++) { if (ri() == 1) { int a = ri(); int x = ri(); tree.add(lr[a].first, lr[a].second + 1, x, (int64_t) -x * depth[a]); } else { int b = ri(); auto res = tree.get(lr[b].first); std::cout << dist[b] + depth[b] * res.first + res.second << std::endl; } } return 0; }