結果

問題 No.1790 Subtree Deletion
ユーザー 👑 NachiaNachia
提出日時 2021-12-20 01:32:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,312 bytes
コンパイル時間 1,011 ms
コンパイル使用メモリ 88,908 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2023-10-13 19:20:29
合計ジャッジ時間 6,079 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/segtree>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


namespace rq{
    u64 op(u64 a, u64 b){ return a^b; }
    u64 e(){ return 0; }
    using rq = atcoder::segtree<u64,op,e>;
}


struct Edge{ int u,v; u64 a; };


int N;
vector<Edge> edges;
vector<vector<int>> E;
vector<u64> A;
vector<int> I;
vector<int> depth;
vector<int> in_time;
vector<int> out_time;
vector<int> out_time_from_in;


void dfs(int p = 0, int pre = -1){
    in_time[p] = I.size();
    I.push_back(p);
    for(int e : E[p]) if(e != pre){
        depth[e] = depth[p] + 1;
        dfs(e,p);
    }
    out_time[p] = I.size();
}

rq::rq sum_A;
vector<int> erase_visited;


int Q;


int main() {
    cin >> N;
    edges.resize(N-1);
    E.resize(N);
    rep(i,N-1){
        int u,v; cin >> u >> v; u--; v--;
        u64 a; cin >> a;
        edges[i] = {u,v,a};
        E[u].push_back(v);
        E[v].push_back(u);
    }

    depth.assign(N, 0);
    in_time.assign(N, 0);
    out_time.assign(N, 0);
    out_time_from_in.resize(N);
    rep(i,N) out_time_from_in[in_time[i]] = out_time[i];
    dfs();

    E.clear();
    E.resize(N);
    A.assign(N, 0);

    for(auto e : edges){
        if(depth[e.u] > depth[e.v]) swap(e.u, e.v);
        A[e.v] = e.a;
        E[e.u].push_back(e.v);
    }

    erase_visited.assign(N, 0);

    vector<u64> sum_A_tmp(N, 0);
    rep(i,N) sum_A_tmp[in_time[i]] = A[i];
    sum_A = rq::rq(sum_A_tmp);
    
    cin >> Q;
    rep(queryid, Q){
        int t; cin >> t;
        if(t == 1){
            int x; cin >> x; x--;
            for(int p=in_time[x]; p<out_time[x]; ){
                if(erase_visited[p]){ p = out_time_from_in[p]; continue; }
                sum_A.set(p,0);
                erase_visited[p] = 1;
                p++;
            }
        }
        if(t == 2){
            int x; cin >> x; x--;
            int l = in_time[x] + 1;
            int r = out_time[x];
            u64 ans = sum_A.prod(l,r);
            cout << ans << "\n";
        }
    }

    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

0