結果

問題 No.1790 Subtree Deletion
ユーザー chocoruskchocorusk
提出日時 2021-12-19 00:11:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 347 ms / 3,000 ms
コード長 3,429 bytes
コンパイル時間 3,562 ms
コンパイル使用メモリ 198,468 KB
実行使用メモリ 17,640 KB
最終ジャッジ日時 2023-10-13 18:29:37
合計ジャッジ時間 8,214 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,752 KB
testcase_01 AC 3 ms
5,836 KB
testcase_02 AC 3 ms
5,772 KB
testcase_03 AC 310 ms
17,312 KB
testcase_04 AC 321 ms
17,316 KB
testcase_05 AC 316 ms
17,308 KB
testcase_06 AC 316 ms
17,412 KB
testcase_07 AC 336 ms
17,364 KB
testcase_08 AC 98 ms
5,964 KB
testcase_09 AC 347 ms
17,640 KB
testcase_10 AC 307 ms
17,360 KB
testcase_11 AC 298 ms
17,380 KB
testcase_12 AC 172 ms
16,836 KB
testcase_13 AC 210 ms
15,896 KB
testcase_14 AC 94 ms
8,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, ll> P;
using mint=modint998244353;
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	using G=function<Monoid(Monoid, OperatorMonoid, int)>;
	using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
	int sz;
	vector<Monoid> data;
	vector<OperatorMonoid> lazy;
	const F f;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], r-l);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};
vector<P> g[100010];
int ord[100010], rev[100010], r[100010];
vector<ll> v;
void dfs(int x, int p, int &i){
    ord[x]=i;
    rev[i]=x;
    i++;
    for(auto q:g[x]){
        int y=q.first;
        if(y==p)continue;
        v[i]=q.second;
        dfs(y, x, i);
    }
    r[x]=i;
}
int main()
{
    int n;
    cin>>n;
    v.resize(n);
    for(int i=0; i<n-1; i++){
        int l, r;ll a;
        cin>>l>>r>>a;
        l--;r--;
        g[l].push_back({r,a});
        g[r].push_back({l,a});
    }
    int i0=0;
    dfs(0, -1, i0);
    auto f=[&](ll a, ll b){
        return (a^b);
    };
    auto g=[&](ll a, ll x, int len){
        if(x==-1) return a;
        else return 0ll;
    };
    auto h=[&](ll x, ll y){
        return max(x, y);
    };
    LazySegmentTree<ll> seg(n, f, g, h, 0, -1);
    seg.build(v);
    int q;cin>>q;
    while(q--){
        int t, x;cin>>t>>x;
        x--;
        if(t==1){
            seg.update(ord[x], r[x], 0);
        }else{
            ll ans=(seg.find(ord[x], r[x])^seg[ord[x]]);
            cout<<ans<<endl;
        }
    }
    return 0;
}
0