跳到正文

个人记录

HDU4825 Xor Sum

Isonan约 2 分钟
原文
文章信息与版本

文章信息

发布
2018年8月8日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟
获赞
2

版本

  1. v1当前

原题传送门>here<

这道题比上一道chip factory简单,只要求静态查询,每次只要在trie树中找最大异或和就好了。

代码也十分简单,一遍就过了:

C++
#include <cstdio>
#include <cstring>

int T,n,tem,son[3200001][2],cnt[3200001],end[3200001],now,points,a[40],x,m;
int main(){
	scanf("%d",&T);
	for(int i=1;i<=T;i++){
		scanf("%d%d",&n,&m);
		printf("Case #%d:\n",i);
		memset(son,0,sizeof son);
		memset(cnt,0,sizeof cnt);
		memset(end,0,sizeof end);
		points=0;
		for(int j=1;j<=n;j++,now=0){
			scanf("%d",&tem),x=tem;
			for(int k=1;k<=32;k++)a[k]=tem&1,tem>>=1;
			for(int k=32;k;k--){
				if(!son[now][a[k]])son[now][a[k]]=++points;
				now=son[now][a[k]];
				cnt[now]++;
			}
			end[now]=x;
		}
		for(int j=1;j<=m;j++,now=0){
			scanf("%d",&tem);
			for(int k=1;k<=32;k++)a[k]=tem&1,tem>>=1;
			for(int k=32;k;k--){
				if(!cnt[son[now][a[k]^1]])now=son[now][a[k]];
				else now=son[now][a[k]^1];
			}
			printf("%d\n",end[now]);
		}
	} 
}
原文