跳到正文

题解

题解:P7242 [COCI 2019/2020 #4] Klasika

linruiyu约 9 分钟
原文

这是 2026年8月19日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月18日
收录
2026年8月19日
更新
2026年11月7日
篇幅
约 9 分钟
获赞
1
题目
P7242

版本

  1. v2当前
  2. v1

首先看到路径的异或和,简单地想到把其转换为从根到某个 xx 的异或和 valval,则 xx 到 yy 的路径的异或和等于 valx⊕valyval_x\oplus val_y。所以由此,题目所要求的就转换为了

max⁡c∈subtree(b)(vala⊕valc)\max_{c\in subtree(b)}(val_a \oplus val_c)

的答案。因为要求一坨数与一个数异或的最大值,能很快想到用 Trie 树来维护。因为要求子树中所有数与其的异或最大值,所以可以把每一个节点的儿子的 Trie 上的信息合并到父亲上,查询就是把要查位置上的数在当前 Trie 树上得到的最大值就好了。

但是如果直接合并的话,复杂度可能会爆炸,所以可以用启发式合并,先把重儿子的答案交换到当前节点,合并时再把儿子的答案都合并到当前节点,因为已经把答案离线到每个查询的点上,所以在交换儿子和父亲时,儿子的答案以及经处理完了,所以可行。

C++
#include<bits/stdc++.h>
// #define int long long
#define ull unsigned long long
#define up(i,x,y) for(register int i=x;i<=y;i=-~i)
#define dn(i,y,x) for(register int i=y;i>=x;i--)
#define mst(x,y) memset(x,y,sizeof x)
#define D(x) cout<<#x<<": "<<x<<endl;
#define DE(x) cout<<#x<<": "<<x<<" ";
#define MAXSIZE 1<<21
// #define faster 1
#define pl p<<1
#define pr p<<1|1
using namespace std;
namespace lry{
	char buf[MAXSIZE],*p1=buf,*p2=buf;
	char pbuf[MAXSIZE],*pp=pbuf;
	inline int gc(){
		#if faster
			return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
		#else
			return getchar();
		#endif
	}
	inline void pc(const char &c){
		#if faster
			if(pp-pbuf==MAXSIZE) fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;*pp++=c;
		#else
			putchar(c);
		#endif
	}
	inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
	inline void read(double &rdx){double t=0;int x=0,s=0,f=1;char c;do c=gc();while(!isdigit(c)&&c!='-'&&c!='.');if (c=='-')f=-1,c=gc();while(isdigit(c)&&c!='.')x=(c^48)+(x<<1)+(x<<3),c=gc();if(c=='.')c=gc();else{rdx=x*f;return;}while(c>='0'&&c<='9')t=t*10+(c^48),s=-~s,c=gc();while(s--)t/=10;rdx=(x+t)*f;}
	inline void read(string &x){x="";char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
	inline void read(char &x){char a=gc();for(;blank(a)&&(a^-1);a=gc());if(!blank(a)&&(a^-1))x=gc();}
	inline void read(int &x){int ret=0,f=0;char ch=gc();while(!isdigit(ch)){if(ch=='-')f=1;ch=gc();}while(isdigit(ch)){ret=(ret<<1)+(ret<<3)+(ch^48);ch=gc();}x=(f?-ret:ret);}
	inline void read(ull &x){ull ret=0,f=0;char ch=gc();while(!isdigit(ch)){if(ch=='-')f=1;ch=gc();}while(isdigit(ch)){ret=(ret<<1)+(ret<<3)+(ch^48);ch=gc();}x=(f?-ret:ret);}
	template<typename t,typename ...T> inline void read(t &x,T&...y){read(x),read(y...);}
	inline void write(int x){if(x<0)pc('-'),x=-x;if(x>9)write(x/10);pc(x%10+48);}
	inline void write(ull x){if(x<0)pc('-'),x=-x;if(x>9)write(x/10);pc(x%10+48);}
	inline void write(const char *x){fputs(x,stdout);}
	inline void write(const string &x){fputs(x.c_str(),stdout);}
	inline void writeln(int x){write(x),pc('\n');}
	inline void writetr(int x){write(x),pc(' ');}
	inline void writeln(ull x){write(x),pc('\n');}
	inline void writetr(ull x){write(x),pc(' ');}
	inline void writeln(string x){write(x),pc('\n');}
	inline void writetr(string x){write(x),pc(' ');}
	template<typename t,typename ...T> inline void write(t x,T...y){write(x),write(y...);}
}
using namespace lry;
constexpr int N=200000+10,M=10000000+10,inf=0x3f3f3f3f;
int q,n;
int rt[N];
int tr[M],son[M][2],tot;
struct ques{
	int opt,x,y,tim;
}Q[N];
inline void insert(int &p,int x,int d,int tim){
	int t=(x>>d)&1;
	if(!p) p=++tot,tr[p]=inf;
	tr[p]=min(tr[p],tim);
	if(d<0) return;
	insert(son[p][t],x,d-1,tim);
}
inline void merge(int p1,int p2,int d){
	if(!p2) return;
	tr[p1]=min(tr[p1],tr[p2]);
	if(d<0) return;// 这个不要写错位置了,不然会导致答案差1
	if(son[p1][0]&&son[p2][0]) merge(son[p1][0],son[p2][0],d-1);
	else if(son[p2][0]) son[p1][0]=son[p2][0];
	if(son[p1][1]&&son[p2][1]) merge(son[p1][1],son[p2][1],d-1);
	else if(son[p2][1]) son[p1][1]=son[p2][1];
}
inline int query(int p,int x,int d,int tim){
	if(d<0) return 0;
	int t=(x>>d)&1;
	if(son[p][!t]&&tr[son[p][!t]]<tim) return query(son[p][!t],x,d-1,tim)+(1<<d);
	return query(son[p][t],x,d-1,tim);
}
vector<int> G[N],p[N];
int hson[N],sz[N],ans[N],a[N];
inline void dfs(int x,int f){
	sz[x]=1;
	for(int y:G[x]){
		if(y==f) continue;
		dfs(y,x);
		sz[x]+=sz[y];
		if(sz[y]>sz[hson[x]]) hson[x]=y;
	}
	if(hson[x]) swap(rt[x],rt[hson[x]]);
	for(int y:G[x]){
		if(y==f) continue;
		merge(rt[x],rt[y],30);
	}
	for(int i:p[x])  ans[Q[i].tim]=query(rt[x],Q[i].y,30,Q[i].tim);
}
signed main(){
	read(q);
	insert(rt[1],0,30,0);
	n=1;
	for(int i=1;i<=q;i++){
		string s; int x,y;
		read(s,x,y);
		if(s=="Add"){
			n++;
			a[n]=(a[x]^y);
			G[x].push_back(n);
			insert(rt[n],a[n],30,i);
		}else{
			Q[i]={1,0,a[x],i};
			p[y].push_back(i);
		}
	}
	dfs(1,0);
	for(int i=1;i<=q;i++) if(Q[i].opt) writeln(ans[i]);
	#if faster
		fwrite(pbuf,1,pp-pbuf,stdout);
		pp=pbuf;
	#endif
	return 0;
}