結果

問題 No.1790 Subtree Deletion
ユーザー milanis48663220milanis48663220
提出日時 2021-12-30 17:39:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,394 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 134,736 KB
実行使用メモリ 32,676 KB
最終ジャッジ日時 2024-04-16 02:28:21
合計ジャッジ時間 5,349 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/segtree>
#include <atcoder/lazysegtree>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

struct edge{
    int to;
    ll a;
};

/**
 * 変数の意味は概ね[maspyさんの記事](https://maspypy.com/euler-tour-%E3%81%AE%E3%81%8A%E5%8B%89%E5%BC%B7#toc1)に準じています(ありがとうございます)
 */ 
class EulerTour{
    public:
    vector<vector<edge>> g;
    int root;
    int n;
    vector<ll> as;
    vector<int> ord;
    vector<int> in, out;
    EulerTour(vector<vector<edge>> g, int root): g(g), root(root){
        n = g.size();
        in.resize(n);
        out.resize(n);
        build();
    }
    private:
    void build(){
        vector<bool> seen(n, false);
        dfs(0, seen);
    }
    void dfs(int v, vector<bool> &seen){
        seen[v] = true;
        in[v] = ord.size();
        ord.push_back(v);
        int cnt = 0;
        for(auto [to, a]: g[v]){
            if(seen[to]) continue;
            as.push_back(a);
            dfs(to, seen);
            as.push_back(0);
            cnt++;
        }
        if(cnt > 0) {
            out[v] = ord.size();
            ord.push_back(v);
        }else{
            out[v] = ord.size()-1;
        }
    }
};

ll op(ll a, ll b){ return a^b; }
ll e(){ return 0; }
ll id(){ return -1; }

ll mapping(ll l, ll r) {
    if (l == id()) return r;
    return 0;
}

ll composition(ll l, ll r) { 
    if(l == id()) return r;
    if(r == id()) return l;
    return 0;
 }

using Seg = atcoder::lazy_segtree<ll, op, e, ll, mapping, composition, id>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<vector<edge>> g(n);
    for(int i = 0; i < n-1; i++){
        int l, r; ll a; cin >> l >> r >> a; l--; r--;
        g[l].push_back(edge{r, a});
        g[r].push_back(edge{l, a});
    }
    auto et = EulerTour(g, 0);
    Seg seg(et.as);
    int q; cin >> q;
    while(q--){
        int t, x; cin >> t >> x; x--;
        int l = et.in[x], r = et.out[x];
        if(t == 1){
            seg.apply(l-1, r, 0);
        }else{
            if(seg.prod(l, r) < 0) {
                debug_value(q)
                assert(false);
            }
            cout << seg.prod(l, r) << endl;
        }
    }
}
0