結果

問題 No.1790 Subtree Deletion
ユーザー 沙耶花沙耶花
提出日時 2021-12-19 04:03:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 370 ms / 3,000 ms
コード長 2,740 bytes
コンパイル時間 7,484 ms
コンパイル使用メモリ 270,400 KB
実行使用メモリ 16,676 KB
最終ジャッジ日時 2023-10-13 18:36:35
合計ジャッジ時間 10,266 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,480 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 330 ms
16,528 KB
testcase_04 AC 322 ms
16,568 KB
testcase_05 AC 320 ms
16,592 KB
testcase_06 AC 327 ms
16,676 KB
testcase_07 AC 333 ms
16,564 KB
testcase_08 AC 97 ms
4,348 KB
testcase_09 AC 370 ms
16,624 KB
testcase_10 AC 328 ms
16,580 KB
testcase_11 AC 326 ms
16,584 KB
testcase_12 AC 208 ms
15,732 KB
testcase_13 AC 233 ms
14,612 KB
testcase_14 AC 93 ms
6,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define Inf 1000000001

struct HLD{
	vector<int> sz,parent,depth,root,pos;
	vector<int> arr;
	HLD(vector<vector<int>> &E){
		sz.resize(E.size(),1);
		parent.resize(E.size(),0);
		depth.resize(E.size(),0);
		root.resize(E.size(),0);
		pos.resize(E.size(),0);
		
		dfs(0,-1,E);
		dfs2(0,-1,E,0);
	}
	
	void dfs(int now,int p,vector<vector<int>> &E){
		parent[now] = p;
		if(p==-1){
			depth[now] = 0;
		}
		else{
			depth[now] = depth[p]+1;
		}
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			dfs(to,now,E);
			sz[now] += sz[to];
		}
	}
	
	void dfs2(int now,int p,vector<vector<int>> &E,int r){
		pos[now] = arr.size();
		arr.push_back(now);
		root[now] = r;
		int maxi = 0;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			if(maxi<sz[to]){
				maxi = sz[to];
				ind = to;
			}
		}
		if(ind==-1)return;
		dfs2(ind,now,E,r);
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p||to==ind)continue;
			dfs2(to,now,E,to);
		}
	}
	
	vector<pair<int,int>> query(int u,int v){
		vector<pair<int,int>> ret;
		int t = 0;
		while(root[u]!=root[v]){
			if(depth[root[u]] <= depth[root[v]]){
				ret.insert(ret.begin()+t,{pos[root[v]], pos[v]});
				v = parent[root[v]];
			}
			else{
				ret.insert(ret.begin()+t,{pos[u],pos[root[u]]});
				u = parent[root[u]];
				t++;
			}
		}
		ret.insert(ret.begin()+t,{pos[u],pos[v]});
		return ret;
	}
	
	int lca(int u,int v){
		for(;;v=parent[root[v]]){
			if(pos[u]>pos[v])swap(u,v);
			if(root[u]==root[v])return u;
		}
	}
	
	int get_distance(int u,int v){
		return depth[u] + depth[v] - 2 * depth[lca(u,v)];
	}
	
};

long long op(long long a,long long b){
	return a^b;
}
long long e(){
	return 0;
}

long long mapping(long long a,long long b){
	if(a==-1)return b;
	return a;
}

long long composition(long long a,long long b){
	if(a==-1)return b;
	if(b==-1)return a;
	return a;
}
long long id(){
	return -1;
}

int main(){
	
	int N;
	cin>>N;
	
	vector<vector<int>> E(N);
	vector<int> l(N-1),r(N-1);
	vector<long long> a(N-1);
	rep(i,N-1){
		cin>>l[i]>>r[i]>>a[i];
		l[i]--;r[i]--;
		E[l[i]].push_back(r[i]);
		E[r[i]].push_back(l[i]);
	}
	
	HLD H(E);
	
	vector<long long> v(N,0);
	rep(i,N-1){
		if(H.depth[l[i]]>H.depth[r[i]])swap(l[i],r[i]);
		v[H.pos[r[i]]] = a[i];
	}
	
	lazy_segtree<long long,op,e,long long,mapping,composition,id> seg(v);
			
	int Q;
	cin>>Q;
	rep(i,Q){
		int t,x;
		cin>>t>>x;
		x--;
		if(t==1){
			seg.apply(H.pos[x],H.pos[x] + H.sz[x],0);
		}
		else{
			cout<<seg.prod(H.pos[x]+1,H.pos[x]+H.sz[x])<<endl;
		}
	}
	
	return 0;
}
0