結果

問題 No.1641 Tree Xor Query
ユーザー 沙耶花沙耶花
提出日時 2021-08-06 22:47:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 379 ms / 5,000 ms
コード長 2,350 bytes
コンパイル時間 4,655 ms
コンパイル使用メモリ 271,704 KB
実行使用メモリ 29,564 KB
最終ジャッジ日時 2023-10-17 04:16:53
合計ジャッジ時間 6,213 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 245 ms
29,564 KB
testcase_14 AC 244 ms
29,564 KB
testcase_15 AC 7 ms
4,444 KB
testcase_16 AC 37 ms
5,012 KB
testcase_17 AC 26 ms
4,636 KB
testcase_18 AC 10 ms
4,756 KB
testcase_19 AC 21 ms
4,348 KB
testcase_20 AC 379 ms
21,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<int> C;
vector<int> D;
vector<vector<int>> E;

void dfs(int cur,int p){
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs(to,cur);
		D[cur] ^= D[to];
	}
	D[cur] ^= C[cur];
}

struct lca{
	
	vector<int> depth;
	vector<vector<int>> parents;
	
	int max_j=18;
	
	lca(int n,vector<vector<int>> &E){
		depth.assign(E.size(),-1);
		parents.assign(E.size(),vector<int>(max_j,-1));
		
		rep(i,100){
			if((1<<i)>E.size()){
				max_j = i;
				break;
			}
		}
		
		stack<int> s;
		s.push(n);
		depth[n] = 0;
		while(s.size()!=0){
			int k = s.top();
			s.pop();
			for(int i=0;i<E[k].size();i++){
				int x = E[k][i];
				if(depth[x]!=-1)continue;
				s.push(x);
				depth[x] = depth[k]+1;
				for(int j=0;j<max_j;j++){
					if(j==0){
						parents[x][j] = k;
					}
					else{
						parents[x][j] = parents[parents[x][j-1]][j-1];
					}
					if(parents[x][j] == -1)break;
				}
			}
		}
	}
	
	
	int kth_parent(int u,int k){
		for(int i=0;i<max_j;i++){
			if(k==0)break;
			if(u==-1)break;
			if(k%2){
				u = parents[u][i];
			}
			k/=2;
		}
		return u;
	}
	
	int query(int u,int v){
		if(depth[u]<depth[v])swap(u,v);
		u = kth_parent(u,depth[u]-depth[v]);
		
		if(u==v){
			return u;
		}
		
		for(int i=max_j-1;i>=0;i--){
			if(parents[u][i]!=parents[v][i]){
				u = parents[u][i];
				v = parents[v][i];
			}
		}
		
		return parents[u][0];
		
	}
	
	int get_distance(int u,int v){
		return depth[u]+depth[v]-2*depth[query(u,v)];
	}
	
	
};

int main(){
	
	int N,Q;
	cin>>N>>Q;
	
	C.resize(N);
	D.resize(N,0);
	rep(i,N)cin>>C[i];
	E.resize(N);
	rep(i,N-1){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	
	dfs(0,-1);
	
	lca L(0,E);
	
	vector<int> x,y;
	rep(_,Q){
		int T;
		cin>>T;
		int a,b;
		scanf("%d %d",&a,&b);
		a--;
		if(T==1){
			x.push_back(a);
			y.push_back(b);
		}
		else{
			int ans = D[a];
			rep(i,x.size()){
				if(L.query(a,x[i])==a)ans ^= y[i];
			}
			printf("%d\n",ans);
		}
		
		
		if(x.size()>200){
			while(x.size()>0){
				C[x.back()] ^= y.back();
				x.pop_back();
				y.pop_back();
			}
			D.assign(N,0);
			dfs(0,-1);
		}
	}
	
	return 0;
}
0