QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#627228#9349. Exchanging Giftsguodong#TL 3ms16012kbC++141.2kb2024-10-10 15:15:172024-10-10 15:15:18

Judging History

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

  • [2024-10-10 15:15:18]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:16012kb
  • [2024-10-10 15:15:17]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int n,type[N],sz[N],s1[N],s2[N];
int num,q[N],ne[N],hd[N];
int add[N],b[N];
void addnum(int x,int y){
	q[++num]=x;
	ne[num]=hd[y];
	hd[y]=num;
}
int dfs(int t){
	if(b[t])return add[t];
	if(type[t]==1){
		b[t]=1;
		return add[t];
	}
	add[t]=dfs(s1[t])+dfs(s2[t]);
	b[t]=1;
	return add[t];
}
int check(int x,int y){
	for(int i=1; i<=n; i++){
		add[i]=0;
		b[i]=0;
		if(type[i]==2)continue;
		for(int j=hd[i]; j; j=ne[j]){
			if(q[j]>=x&&q[j]<=y)add[i]++;
		}
	}
	return dfs(n);
}
signed main(){
	ios::sync_with_stdio(false);
	// synchronize with standard io false
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1; i<=n; i++){
			cin>>type[i];
			if(type[i]==1){
				cin>>sz[i];
				for(int j=1; j<=sz[i]; j++){
					int x;
					cin>>x;
					addnum(x,i);
				}
			}else{
				cin>>s1[i]>>s2[i];
				sz[i]=sz[s1[i]]+sz[s2[i]];
			}
		}
		int l=1,r=1e9;
		while(l<r){
			int mid=(l+r)/2;
			if(check(1,mid)>sz[n]/2)r=mid;
			else l=mid+1;
		}
		int sum=check(l,l);
		if(sum<=sz[n]/2){
			cout<<sz[n]<<endl;
		}else{
			cout<<(sz[n]-sum)*2<<endl;
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 16012kb

input:

2
1
1 5 3 3 2 1 3
3
1 3 3 3 2
1 4 2 2 3 3
2 1 2

output:

4
6

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10000
100
1 30 371028678 371028678 371028678 716418076 398221499 591504380 398221499 398221499 591504380 777141390 398221499 591504380 591504380 777141390 287847807 716418076 777141390 716418076 777141390 287847807 287847807 287847807 371028678 371028678 398221499 777141390 371028678 6827702 6827702...

output:

700
0
332
284
130
1048
194
667
754
640
484
252
18
351
1228
238
1025
354
383
571
4272
340
1044
199
448
190
1459
69
841
546
247
883
138
1633
91
3308
2556
1280
488
618
407
381
383
2865
1407
496
1202
22
112
415
662
380
41
18
91
505
818
-68
241
764
1227
1802
176
187
817
1489
460
296
238
259
1028
190
606
...

result: