結果

問題 No.1790 Subtree Deletion
ユーザー milanis48663220milanis48663220
提出日時 2021-12-30 17:29:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,400 bytes
コンパイル時間 1,509 ms
コンパイル使用メモリ 130,860 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-04-16 02:15:12
合計ジャッジ時間 5,356 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
権限があれば一括ダウンロードができます

ソースコード

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, 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<int> par;
    vector<int> as;
    vector<int> ord;
    vector<int> in, out;
    EulerTour(vector<vector<edge>> g, int root): g(g), root(root){
        n = g.size();
        par.resize(n);
        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);
            par[to] = v;
            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;
        }
    }
};

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

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

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

using Seg = atcoder::lazy_segtree<int, op, e, int, 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, 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){
            // cout << l << ' ' << r << endl;
            seg.apply(l-1, r, 0);
        }else{
            cout << seg.prod(l, r) << endl;
        }
    }
}
0