結果

問題 No.2439 Fragile Apple Tree
ユーザー kuronikuroni
提出日時 2023-08-19 00:00:08
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,512 ms / 10,000 ms
コード長 2,879 bytes
コンパイル時間 4,251 ms
コンパイル使用メモリ 265,020 KB
実行使用メモリ 86,544 KB
最終ジャッジ日時 2023-08-19 00:48:42
合計ジャッジ時間 26,090 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,014 ms
40,008 KB
testcase_01 AC 1,225 ms
50,828 KB
testcase_02 AC 1,288 ms
51,212 KB
testcase_03 AC 324 ms
86,544 KB
testcase_04 AC 419 ms
86,400 KB
testcase_05 AC 1,177 ms
51,032 KB
testcase_06 AC 1,230 ms
50,392 KB
testcase_07 AC 1,065 ms
51,376 KB
testcase_08 AC 1,112 ms
51,232 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1,512 ms
49,624 KB
testcase_15 AC 1,009 ms
50,544 KB
testcase_16 AC 1,203 ms
50,252 KB
testcase_17 AC 1,233 ms
50,280 KB
testcase_18 AC 657 ms
39,472 KB
testcase_19 AC 406 ms
21,184 KB
testcase_20 AC 222 ms
18,064 KB
testcase_21 AC 36 ms
4,380 KB
testcase_22 AC 646 ms
28,356 KB
testcase_23 AC 280 ms
14,868 KB
testcase_24 AC 358 ms
19,988 KB
testcase_25 AC 460 ms
33,016 KB
testcase_26 AC 571 ms
37,632 KB
testcase_27 AC 359 ms
26,152 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 416 ms
52,060 KB
testcase_32 AC 433 ms
53,792 KB
権限があれば一括ダウンロードができます

ソースコード

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, 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';
        }
    }
}
0