結果

問題 No.2439 Fragile Apple Tree
ユーザー kuronikuroni
提出日時 2023-08-18 23:59:08
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,875 bytes
コンパイル時間 3,970 ms
コンパイル使用メモリ 266,112 KB
実行使用メモリ 86,480 KB
最終ジャッジ日時 2023-08-19 00:00:29
合計ジャッジ時間 45,739 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1,429 ms
50,780 KB
testcase_02 RE -
testcase_03 AC 367 ms
86,400 KB
testcase_04 AC 430 ms
86,480 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 1,250 ms
51,212 KB
testcase_08 AC 1,254 ms
51,256 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 WA -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 699 ms
37,660 KB
testcase_27 RE -
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

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; 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, 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) {
                assert(!off[pat[l - 1]]);
                remove_from_tree(pat[l - 1]);
            }
        } else {
            cout << ans << '\n';
        }
    }
}
0