106682024-04-07 20:14:39111XORfa visszatércpp17Time limit exceeded 11/1001.588s522560 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;

	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&&r<=l){
			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;
		}
		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
2Accepted3ms2064 KiB
subtask211/11
3Accepted6ms7324 KiB
4Accepted4ms5744 KiB
5Accepted8ms10700 KiB
6Accepted6ms8648 KiB
7Accepted6ms7532 KiB
8Accepted8ms10188 KiB
9Accepted6ms8324 KiB
10Accepted4ms4200 KiB
11Accepted4ms4324 KiB
12Accepted4ms4556 KiB
13Accepted4ms4452 KiB
subtask30/13
14Accepted282ms185052 KiB
15Accepted337ms204740 KiB
16Time limit exceeded1.529s36940 KiB
17Time limit exceeded1.567s38420 KiB
18Time limit exceeded1.564s43764 KiB
19Time limit exceeded1.544s54072 KiB
20Time limit exceeded1.544s36260 KiB
21Time limit exceeded1.572s38452 KiB
22Accepted356ms205080 KiB
23Accepted340ms204972 KiB
subtask40/17
24Accepted529ms374600 KiB
25Accepted208ms186676 KiB
26Accepted889ms498924 KiB
27Runtime error970ms522560 KiB
28Runtime error973ms522512 KiB
29Accepted435ms318696 KiB
30Accepted586ms414632 KiB
31Accepted588ms414228 KiB
32Accepted275ms32492 KiB
33Accepted277ms32576 KiB
34Accepted277ms32612 KiB
35Accepted277ms32592 KiB
subtask50/59
36Runtime error736ms522464 KiB
37Runtime error702ms522436 KiB
38Runtime error680ms522408 KiB
39Runtime error698ms522444 KiB
40Runtime error646ms522416 KiB
41Runtime error728ms522380 KiB
42Runtime error720ms522356 KiB
43Runtime error714ms522348 KiB
44Runtime error722ms522352 KiB
45Runtime error495ms522368 KiB
46Time limit exceeded1.577s198284 KiB
47Time limit exceeded1.544s188380 KiB
48Time limit exceeded1.552s188496 KiB
49Time limit exceeded1.588s188524 KiB