結果

問題 No.2439 Fragile Apple Tree
ユーザー kuronikuroni
提出日時 2023-08-18 23:53:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,821 bytes
コンパイル時間 4,196 ms
コンパイル使用メモリ 266,048 KB
実行使用メモリ 86,424 KB
最終ジャッジ日時 2023-08-18 23:54:31
合計ジャッジ時間 31,960 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1,515 ms
51,052 KB
testcase_02 WA -
testcase_03 AC 381 ms
86,416 KB
testcase_04 AC 437 ms
86,424 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,251 ms
51,348 KB
testcase_08 AC 1,293 ms
51,196 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 775 ms
37,536 KB
testcase_27 WA -
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
    {
        // HLD
        vector<int> sub(n);
        vector<pair<int, int64_t>> mc(n, {-1, 0});
        function<void(int, int)> DFS = [&](int u, int p) {
            par[u] = p;
            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, int 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) {
                remove_from_tree(pat[l - 1]);
            }
        } else {
            cout << ans << '\n';
        }
    }
}
0