代码全WA 求调
查看原帖
代码全WA 求调
741775
封禁用户楼主2024/4/15 19:49

不知道哪里写错了

#include<bits/stdc++.h>
using namespace std;
int read()
{
	int f=1,x=0; char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f*x;
}
void write(int x)
{
	if(x<0){putchar('-');x=-x;}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
const int N=200010;
int n,q,a[N];
int head[N],cn;
struct edge
{
	int nxt,to;
}e[N<<1];
void add(int u,int v)
{
	cn++;
	e[cn].nxt=head[u];
	e[cn].to=v;
	head[u]=cn;
}
int fa[N],dep[N],siz[N],son[N];
int top[N],id[N],b[N],tot;
void dfs1(int u,int d,int f)
{
	dep[u]=d; fa[u]=f; siz[d]=1;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v==f) continue;
		dfs1(v,d+1,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;	
	}
}
void dfs2(int u,int t)
{
	top[u]=t; id[u]=++tot; b[tot]=a[u];
	if(!son[u]) return;
	dfs2(son[u],t);
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v==son[u]||v==fa[u]) continue;
		dfs2(v,v);
	}
}
int cnode,rt[N],ch[N*30][2],cnt[N*30];
void update(int a,int b,int t,int x)
{
	if(t<0) return;
	int i=(x>>t)&1;
	ch[a][!i]=ch[b][!i];
	ch[a][i]=cnode++;
	cnt[ch[a][i]]=cnt[ch[b][i]]+1;
	update(ch[a][i],ch[b][i],t-1,x);
}
int query(int a,int b,int t,int x)
{
	if(t<0) return 0;
	int i=(x>>t)&1;
	if(cnt[ch[b][!i]]>cnt[ch[a][!i]])
		return (1<<t)+query(ch[a][!i],ch[b][!i],t-1,x);
	else return query(ch[a][i],ch[b][i],t-1,x);
}
int getans(int x,int y,int z)
{
	int ans=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		ans=max(ans,query(rt[id[top[x]]-1],rt[id[x]],30,z));
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	ans=max(ans,query(rt[id[x]-1],rt[id[y]],30,z));
	return ans;
}
int main()
{
	n=read(),q=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<n;i++)
	{
		int u=read(),v=read();
		add(u,v); add(v,u);
	}
	dfs1(1,1,0); dfs2(1,1);
	for(int i=1;i<=n;i++)
	{
		rt[i]=cnode++;
		update(rt[i],rt[i-1],30,b[i]);
	}
	while(q--)
	{
		int opt=read();
		if(opt==1)
		{
			int x=read(),z=read();
			write(query(rt[id[x]-1],rt[id[x]+siz[x]-1],30,z));
			puts("");
		}
		else
		{
			int x=read(),y=read(),z=read();
			write(getans(x,y,z));
			puts("");
		}
	}
	return 0;
}
2024/4/15 19:49
加载中...