106672024-04-07 19:58:58111XORfa visszatércpp17Time limit exceeded 11/1001.583s522372 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

struct Trie{
	vector<array<int,2>>t;
	int b;
	
	Trie():t(1),b(0){
	}
	
	void add(int x){
		if(t.size()>1){
			b=max(b,get(x));
		}
		int y=0;
		for(int i=30;i>=0;i--){
			int b=x>>i&1;
			if(t[y][b]==0){
				t[y][b]=t.size();
				t.emplace_back();
			}
			y=t[y][b];
		}
	}
	
	int get(int x){
		int y=0;
		int z=0;
		for(int i=30;i>=0;i--){
			int b=x>>i&1;
			if(t[y][b^1]){
				y=t[y][b^1];
				z^=1<<i;
			}
			else{
				y=t[y][b];
			}
		}
		return z;
	}
};

struct ST{
	int n;
	vector<Trie>t;
	
	ST(int n):n(n),t(n*4){
	}
	
	void update(int i,int l,int r,int ll,int rr,int xx){
		if(r<ll||l>rr){
			return;
		}
		t[i].add(xx);
		if(l==r){
			return;
		}
		update(i*2+1,l,(l+r)/2,ll,rr,xx);
		update(i*2+2,(l+r)/2+1,r,ll,rr,xx);
	}
	
	int query(int i,int l,int r,int pp){
		if(r<pp||l>pp){
			return 0;
		}
		if(l==r){
			return t[i].b;
		}
		return max(query(i*2+1,l,(l+r)/2,pp),query(i*2+2,(l+r)/2+1,r,pp));
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,Q;
	cin>>N>>Q;
	vector<vector<pair<int,int>>>g(N+1);
	for(int i=0;i<N-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		g[a].emplace_back(b,w);
		g[b].emplace_back(a,w);
	}
	vector<int>v(N+1);
	auto dfs=[&](auto self,int i,int p,int x)->void{
		v[i]=x;
		for(auto[j,w]:g[i]){
			if(j==p){
				continue;
			}
			self(self,j,i,x^w);
		}
	};
	dfs(dfs,1,0,0);
	vector<int>s(N+1,-1);
	ST st(Q);
	for(int i=0;i<Q;i++){
		int x;
		cin>>x;
		if(s[x]==-1){
			s[x]=i;
		}
		else{
			st.update(0,0,Q-1,s[x],i-1,v[x]);
			s[x]=-1;
		}
	}
	for(int x=1;x<=N;x++){
		if(s[x]!=-1){
			st.update(0,0,Q-1,s[x],Q-1,v[x]);
		}
	}
	for(int i=0;i<Q;i++){
		cout<<st.query(0,0,Q-1,i)<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1964 KiB
2Accepted3ms2112 KiB
subtask211/11
3Accepted6ms7372 KiB
4Accepted4ms5808 KiB
5Accepted8ms10748 KiB
6Accepted6ms8440 KiB
7Accepted6ms7600 KiB
8Accepted7ms10080 KiB
9Accepted6ms7868 KiB
10Accepted4ms3900 KiB
11Accepted4ms3912 KiB
12Accepted4ms3908 KiB
13Accepted4ms4032 KiB
subtask30/13
14Accepted282ms184420 KiB
15Accepted337ms204232 KiB
16Time limit exceeded1.569s36852 KiB
17Time limit exceeded1.583s37924 KiB
18Time limit exceeded1.564s43316 KiB
19Time limit exceeded1.577s54004 KiB
20Time limit exceeded1.569s36096 KiB
21Time limit exceeded1.572s38316 KiB
22Accepted370ms204828 KiB
23Accepted342ms205072 KiB
subtask40/17
24Accepted523ms374572 KiB
25Accepted209ms186456 KiB
26Accepted896ms498860 KiB
27Runtime error1.319s522372 KiB
28Runtime error980ms522328 KiB
29Accepted437ms318892 KiB
30Accepted791ms415152 KiB
31Accepted596ms414656 KiB
32Accepted279ms32852 KiB
33Accepted275ms32916 KiB
34Accepted284ms33252 KiB
35Accepted284ms33336 KiB
subtask50/59
36Runtime error730ms521820 KiB
37Runtime error927ms521784 KiB
38Runtime error689ms521744 KiB
39Runtime error679ms521780 KiB
40Runtime error865ms521756 KiB
41Runtime error728ms521716 KiB
42Runtime error955ms521712 KiB
43Runtime error722ms521704 KiB
44Runtime error948ms521620 KiB
45Runtime error505ms521640 KiB
46Time limit exceeded1.552s188932 KiB
47Time limit exceeded1.583s189040 KiB
48Time limit exceeded1.574s189072 KiB
49Time limit exceeded1.583s188984 KiB