106692024-04-07 20:25:15111XORfa visszatércpp17Time limit exceeded 11/1001.585s522060 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]){
				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;
	vector<vector<int>>d;

	ST(int n):n(n),t(n*4),d(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>=ll&&r<=rr){
			d[i].push_back(xx);
			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;
		}
		int ans=0;
		if(l==r){
			return t[i].b;
		}
		for(int x:d[i]){
			t[i*2+1].add(x);
			d[i*2+1].push_back(x);
			t[i*2+2].add(x);
			d[i*2+2].push_back(x);
		}
		d[i].clear();
		ans=max(ans,query(i*2+1,l,(l+r)/2,pp));
		ans=max(ans,query(i*2+2,(l+r)/2+1,r,pp));
		return ans;
	}
};

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
1Accepted3ms1832 KiB
2Accepted3ms2060 KiB
subtask211/11
3Accepted6ms6844 KiB
4Accepted4ms5740 KiB
5Accepted8ms10248 KiB
6Accepted7ms7900 KiB
7Accepted6ms7576 KiB
8Accepted8ms9736 KiB
9Accepted6ms7892 KiB
10Accepted4ms4552 KiB
11Accepted6ms4768 KiB
12Accepted4ms4964 KiB
13Accepted4ms5172 KiB
subtask30/13
14Accepted319ms201152 KiB
15Accepted428ms225784 KiB
16Time limit exceeded1.572s162964 KiB
17Time limit exceeded1.574s156016 KiB
18Time limit exceeded1.557s152056 KiB
19Time limit exceeded1.582s152600 KiB
20Time limit exceeded1.585s159964 KiB
21Time limit exceeded1.582s156332 KiB
22Accepted391ms226252 KiB
23Accepted391ms226132 KiB
subtask40/17
24Accepted284ms305564 KiB
25Accepted135ms157816 KiB
26Accepted444ms456248 KiB
27Runtime error518ms522060 KiB
28Runtime error527ms522036 KiB
29Accepted247ms273380 KiB
30Accepted317ms334872 KiB
31Accepted314ms334464 KiB
32Accepted228ms54940 KiB
33Accepted228ms54936 KiB
34Accepted231ms54792 KiB
35Accepted231ms54916 KiB
subtask50/59
36Runtime error657ms521908 KiB
37Runtime error782ms521888 KiB
38Runtime error725ms521856 KiB
39Runtime error823ms521848 KiB
40Runtime error845ms521824 KiB
41Runtime error790ms521816 KiB
42Runtime error647ms521808 KiB
43Runtime error656ms521804 KiB
44Runtime error787ms521592 KiB
45Runtime error550ms521588 KiB
46Runtime error1.371s521600 KiB
47Runtime error1.296s521588 KiB
48Runtime error1.368s521588 KiB
49Runtime error1.297s521588 KiB