結果

問題 No.1790 Subtree Deletion
ユーザー shiroha_F14shiroha_F14
提出日時 2021-12-10 19:45:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,937 bytes
コンパイル時間 2,075 ms
コンパイル使用メモリ 179,016 KB
実行使用メモリ 8,084 KB
最終ジャッジ日時 2023-10-13 18:16:46
合計ジャッジ時間 4,200 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,696 KB
testcase_01 AC 2 ms
4,696 KB
testcase_02 AC 3 ms
4,796 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 85 ms
8,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/lazysegtree>
using namespace atcoder;

using ll = long long;


// Euler Tour
vector<int> vis;
vector<ll> edges(1, 0);
vector<vector<pair<int, ll>>> g(50000);
vector<int> in(50000);
vector<int> out(50000);
vector<int> seen(50000, false);
int t = 0;

void dfs(int p){
    in[p] = t;
    t++;
    vis.push_back(p);
    seen[p] = true;
    
    for(auto v : g[p]){
        if(seen[v.first]) continue;
        edges.push_back(v.second);
        dfs(v.first);
        vis.push_back(p);
        edges.push_back(0);
    }

    t++;
    out[p] = t;
}

// lazy segtree
ll ID = 8000000000000000000;
ll op(ll a, ll b){
    return a ^ b;
}
ll e(){
    return 0;
}
ll mapping(ll f, ll x){
    if(f == ID){
        return x;
    }else{
        return f;
    }
}
ll composition(ll f, ll g){
    if(f == ID){
        return g;
    }else{
        return f;
    }
}
ll id(){
    return ID;
}

// main
int main(){
    int n; cin >> n;
    assert(2 <= n && n <= 50000);
    for(int i = 0; i < n - 1; i++){
        int l, r; cin >> l >> r;
        assert(1 <= l && l <= n && 1 <= r && r <= n);
        ll a; cin >> a;
        assert(0 <= a && a <= 1000000000000000000);
        g[l - 1].push_back({r - 1, a});
        g[r - 1].push_back({l - 1, a});
    }

    vis.reserve(n * 2);
    edges.reserve(n * 2);

    dfs(0);
    edges.push_back(0);

    int m = vis.size();
    lazy_segtree<ll, op, e, ll, mapping, composition, id> seg(edges);

    int q; cin >> q;
    assert(1 <= q && q <= 50000);
    for(int i = 0; i < q; i++){
        int t; cin >> t;
        assert(t == 1 || t == 2);
        int x; cin >> x;
        if(t == 1){
            // delete
            assert(2 <= x && x <= n);
            seg.apply(in[x - 1], out[x - 1], 0);
        }else{
            // f(x)
            assert(1 <= x && x <= n);
            cout << seg.prod(in[x - 1] + 1, out[x - 1]) << endl;
        }
    }
}
0