QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356673#8296. Constructing Rancheskevinyang#WA 207ms10872kbC++172.6kb2024-03-18 08:07:262024-03-18 08:07:27

Judging History

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

  • [2024-03-18 08:07:27]
  • 评测
  • 测评结果:WA
  • 用时:207ms
  • 内存:10872kb
  • [2024-03-18 08:07:26]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<long long, long long>, null_type, less<pair<long long, long long>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// find_by_order(x) returns the xth element(0 - indexed btw)
// order_of_key(x) returns index of lower_bound of x(0 - indexed btw)
#define int long long
const int mxn = 200005;
vector<vector<int>>adj(mxn);
vector<int>sz(mxn);
vector<bool>vis(mxn);
vector<int>a(mxn);
vector<pair<int,int>>vals;
int ans = 0;
void dfs(int u, int p){
	sz[u] = 1;
	for(int nxt: adj[u]){
		if(nxt==p||vis[nxt])continue;
		dfs(nxt,u);
		sz[u]+=sz[nxt];
	}
}
int cent = 0;
int n;
void getcentroid(int u, int p){
	bool f = true;
	for(int nxt: adj[u]){
		if(nxt==p||vis[nxt])continue;
		getcentroid(nxt,u);
		if(sz[nxt]>n/2)f = false;
	}
	if(n-sz[u]>n/2)f = false;
	if(f)cent = u;
}
void dfs1(int u, int p, int sum, int d){
	for(int nxt: adj[u]){
		if(vis[nxt] || nxt == p)continue;
		dfs1(nxt,u,sum+a[nxt],max(d,a[nxt]));
	}
	if(sum-d > d)ans++;
	vals.push_back({d,sum});
}
void centroid(int x){
	dfs(x,0);
	n = sz[x];
	getcentroid(x,0);
	vis[cent] = true;
	
	{
		dfs1(cent,0,a[cent],a[cent]);
		sort(vals.begin(),vals.end());
		ordered_set t;
		for(int i = 0; i<vals.size(); i++){
			int sum = vals[i].second;
			int d = vals[i].first;
			int lookup = sum-d;

			// want some x > d - lookup
			int cnt = (int)t.size() - t.order_of_key(make_pair(d-lookup+1,0LL));
			ans+=cnt;
			t.insert({sum-a[cent],i});
		}
		vals.clear();
	}
	for(int nxt: adj[cent]){
		if(vis[nxt])continue;
		dfs1(nxt,cent,a[cent],a[cent]);
		sort(vals.begin(),vals.end());
		ordered_set t;
		for(int i = 0; i<vals.size(); i++){
			int sum = vals[i].second;
			int d = vals[i].first;
			int lookup = sum-d;

			// want some x > d - lookup
			int cnt = (int)t.size() - t.order_of_key(make_pair(d-lookup+1,0LL));
			ans-=cnt;
			t.insert({sum-a[cent],i});
		}
		vals.clear();
	}
	for(int nxt: adj[cent]){
		if(vis[nxt])continue;
		centroid(nxt);
	}
}
void reset(int n){
	for(int i = 1; i<=n; i++){
		adj[i].clear();
		vis[i] = 0;
		a[i] = 0;
		sz[i] = 0;
	}
	ans = 0;
}
signed main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--){
		cin >> n;
		int nodes = n;
		for(int i = 1; i<=n; i++){
			cin >> a[i];
		}
		for(int i = 1; i<n; i++){
			int x,y;
			cin >> x >> y;
			adj[x].push_back(y);
			adj[y].push_back(x);
		}
		centroid(1);
		cout << ans << "\n";
		reset(nodes);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 10856kb

input:

2
3
1 10 100
1 2
3 2
5
1 1 1 1 1
1 2
1 3
1 4
1 5

output:

0
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 207ms
memory: 10872kb

input:

34763
19
98 27 61 17 77 9 97 23 24 5 94 61 100 88 98 43 8 75 96
4 17
12 17
7 3
19 4
12 5
1 18
10 5
7 2
1 4
2 11
19 9
18 16
1 11
17 15
6 3
16 8
15 14
15 13
9
49 4 97 14 1 11 52 84 84
1 3
6 4
9 7
2 6
7 8
1 2
3 8
5 9
19
92 74 62 54 60 78 74 6 76 80 13 94 4 86 85 89 23 46 2
6 17
1 8
8 2
8 14
13 7
12 9
1...

output:

180
37
171
180
197
142
12
9
150
6
1
152
32
7
22
86
76
25
43
180
22
90
156
4
48
100
3
170
4
210
0
8
172
105
166
23
99
152
45
126
190
203
23
118
12
84
44
13
104
17
22
48
27
0
191
1
25
0
38
29
78
203
97
1
7
168
48
142
26
73
26
34
67
124
23
60
25
37
19
1
88
202
16
27
103
124
21
184
114
9
68
210
32
136
5...

result:

wrong answer 1st lines differ - expected: '135', found: '180'