結果
問題 | No.2439 Fragile Apple Tree |
ユーザー |
|
提出日時 | 2023-08-19 00:00:08 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,892 ms / 10,000 ms |
コード長 | 2,879 bytes |
コンパイル時間 | 3,455 ms |
コンパイル使用メモリ | 268,668 KB |
実行使用メモリ | 91,340 KB |
最終ジャッジ日時 | 2024-11-28 13:00:22 |
合計ジャッジ時間 | 31,839 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/lazysegtree>using namespace std;using namespace atcoder;const int64_t INF = 1E18 + 7;int64_t op(int64_t u, int64_t v) { return min(u, v); }int64_t e() { return INF; }int64_t mapping(int64_t s, int64_t e) { return s + e; }int64_t compose(int64_t s, int64_t e) { return s + e; }int64_t id() { return 0; }bool f(int64_t v) { return v > 0; }int main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);int n, q; cin >> n >> q;vector<vector<pair<int, int64_t>>> adj(n);for (int i = 1; i < n; i++) {int u, v; int64_t c; cin >> u >> v >> c; u--; v--;adj[u].push_back({v, c});adj[v].push_back({u, c});}vector<int> in(n), rt = {0}, col(n), pat, par(n);lazy_segtree<int64_t, op, e, int64_t, mapping, compose, id> seg(n);{// HLDvector<int> sub(n);vector<pair<int, int64_t>> mc(n, {-1, 0});function<void(int, int)> DFS = [&](int u, int p) {par[u] = p; sub[u] = 1;if (u != 0) {adj[u].erase(lower_bound(adj[u].begin(), adj[u].end(), pair(p, 0L)));}for (auto [v, c] : adj[u]) {DFS(v, u);sub[u] += sub[v];if (mc[u].first == -1 || sub[v] > sub[mc[u].first]) {mc[u] = {v, c};}}};DFS(0, -1);int tot = 0, pos = 0;function<void(int, int64_t)> DFS_2 = [&](int u, int64_t w) {col[u] = tot; pat.push_back(u); in[u] = pos++;seg.set(in[u], w);if (mc[u].first != -1) {DFS_2(mc[u].first, mc[u].second);}for (auto [v, c] : adj[u]) {if (v != mc[u].first) {tot++; rt.push_back(v);DFS_2(v, c);}}};DFS_2(0, INF);};vector<bool> off(n);vector<int64_t> cur(n);int ans = n;auto update = [&](int u, int64_t x) {cur[u] += x;while (u != -1) {seg.apply(in[rt[col[u]]], in[u] + 1, -x);u = par[rt[col[u]]];}};function<void(int)> remove_from_tree = [&](int u) {update(u, -cur[u]);ans--;off[u] = true;for (auto [v, _] : adj[u]) {if (!off[v]) {remove_from_tree(v);}}};while (q--) {int t; cin >> t;if (t == 1) {int u, x; cin >> u >> x; u--;if (!off[u]) {update(u, x);}int l = seg.min_left(n, f);if (l != 0) {assert(!off[pat[l - 1]]);remove_from_tree(pat[l - 1]);}} else {cout << ans << '\n';}}}