結果
問題 |
No.900 aδδitivee
|
ユーザー |
![]() |
提出日時 | 2019-10-05 13:48:48 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 196 ms / 2,000 ms |
コード長 | 1,876 bytes |
コンパイル時間 | 1,875 ms |
コンパイル使用メモリ | 181,628 KB |
実行使用メモリ | 24,060 KB |
最終ジャッジ日時 | 2024-10-05 19:05:18 |
合計ジャッジ時間 | 7,921 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
#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; }