QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718412#9605. 新生舞会thomaswmy0 858ms36328kbC++142.3kb2024-11-06 20:26:242024-11-06 20:26:24

Judging History

你现在查看的是最新测评结果

  • [2024-11-06 20:26:24]
  • 评测
  • 测评结果:0
  • 用时:858ms
  • 内存:36328kb
  • [2024-11-06 20:26:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=2e6+10;

struct edg{
	int to,nxt;
}edge[N];

int cnt,head[N];

void add(int u,int v) {
	edge[++cnt]={v,head[u]},head[u]=cnt;
}

int T;
int n;
int siz[N],son[N];
int ans;

struct Node{
	int ch[2];
	int siz;
	int tag;
	#define ch(x) t[x].ch
	#define siz(x) t[x].siz
	#define tag(x) t[x].tag
}t[N<<1];

int tot;
int stk[N<<1],top;
int rt[N];

int New() {
	int id;
	if(top>0) id=stk[top--];
	else id=++tot;
	t[id]={{0,0},0,0};
	return id;
}

void pushup(int id) {
	siz(id)=siz(ch(id)[0])+siz(ch(id)[1]);
}

void pushdown(int id,int k) {
	if(tag(id)) {
		if(tag(id)>>k&1) swap(ch(id)[0],ch(id)[1]);
		if(ch(id)[0]) tag(ch(id)[0])^=tag(id);
		if(ch(id)[1]) tag(ch(id)[1])^=tag(id);
		tag(id)=0;
	}
}

void ins(int p,int val) {
	int pp=p;
	bool flag=0;
	for(int i=20;i>=0;i--) {
		pushdown(p,i);
		int v=val>>i&1;
		if(!ch(p)[v]) {
			flag=1;
			break;
		}
		p=ch(p)[v];
	}
	if(!flag) return ;
	p=pp;
	siz(p)++;
	for(int i=20;i>=0;i--) {
		int v=val>>i&1;
		if(!ch(p)[v]) ch(p)[v]=New();
		p=ch(p)[v];
		siz(p)++;
	}
}

int mg(int p,int q,int k) {
	if(!p || !q) return p|q;
	pushdown(p,k);
	pushdown(q,k);
	ch(p)[0]=mg(ch(p)[0],ch(q)[0],k-1);
	ch(p)[1]=mg(ch(p)[1],ch(q)[1],k-1);
	stk[++top]=q;
	if(k) pushup(p);
	return p;
}

int query(int p,int k) {
	if(!p) return 0;
	if(siz(p)==1<<k+1) return 1<<k+1;
	pushdown(p,k);
	if(siz(ch(p)[0])==1<<k) return (1<<k)+query(ch(p)[1],k-1);
	else return query(ch(p)[0],k-1);
}

void dfs1(int u) {
	siz[u]=1;
	son[u]=0;
	for(int i=head[u];i;i=edge[i].nxt) {
		int v=edge[i].to;
		dfs1(v);
		if(siz[v]>siz[son[u]]) son[u]=v;
		siz[u]+=siz[v];
	}
}

int dfs2(int u) {
	int xr=0;
	if(son[u]) {
		xr^=dfs2(son[u]);
		rt[u]=rt[son[u]];
		for(int i=head[u];i;i=edge[i].nxt) {
			int v=edge[i].to;
			if(v==son[u]) continue;
			xr^=dfs2(v);
			rt[u]=mg(rt[u],rt[v],20);
		}
	}
	else rt[u]=New();
	ins(rt[u],0);
	tag(rt[u])^xr;
	int res=query(rt[u],20);
	tag(rt[u])^=res;
	return res;
}

int main() {
	scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		cnt=0;
		for(int i=1;i<=n;i++) head[i]=0;
		for(int i=2;i<=n;i++) {
			int p;
			scanf("%d",&p);
			add(p,i);
		}
		dfs1(1);
		tot=0,top=0;
		ans^=dfs2(1);
	}
	printf("%d",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 16216kb

input:

12
13
1 2 1 4 2 5 3 3 2 9 6 4
392
1 2 2 4 4 3 3 6 9 10 9 11 13 14 15 15 17 18 19 20 6 21 23 12 24 26 27 28 29 30 31 32 33 31 34 33 36 9 38 40 41 25 42 44 45 46 47 48 49 50 51 52 53 21 32 52 54 58 59 60 53 61 63 64 65 66 67 68 69 70 4 1 71 74 75 76 77 78 79 80 81 52 82 55 84 86 84 87 89 90 91 92 93 1...

output:

139

result:

wrong answer 1st numbers differ - expected: '1875', found: '139'

Subtask #2:

score: 0
Wrong Answer

Test #4:

score: 0
Wrong Answer
time: 858ms
memory: 36328kb

input:

15
1
5
1 1 3 3
10603
1 2 2 1 5 3 6 3 2 7 6 6 10 5 4 16 4 16 18 4 11 22 8 14 2 26 22 9 10 20 28 26 1 27 8 35 3 4 6 25 14 9 33 44 15 26 28 34 24 18 48 5 33 11 37 32 19 14 41 6 46 60 8 25 62 32 59 27 54 50 62 44 51 18 53 46 42 31 26 11 78 71 40 60 2 13 35 84 34 76 51 88 33 13 88 85 80 70 21 74 54 79 75...

output:

13835

result:

wrong answer 1st numbers differ - expected: '11527', found: '13835'

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%