QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#449846#8296. Constructing RanchesPetroTarnavskyi#WA 150ms5716kbC++202.8kb2024-06-21 18:15:582024-06-21 18:16:00

Judging History

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

  • [2024-06-21 18:16:00]
  • 评测
  • 测评结果:WA
  • 用时:150ms
  • 内存:5716kb
  • [2024-06-21 18:15:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;


struct Fenwick
{
	int n;
	vector<int> t;
	
	void init(int _n)
	{
		n = _n;
		t.clear();
		t.assign(n, 0);
	}
	void upd(int i, int x)
	{
		for(; i < n; i |= i + 1)
			t[i] += x;
	}
	int query(int i)
	{
		int ans = 0;
		for(; i >= 0; i = (i & (i + 1)) - 1)
			ans += t[i];
		return ans;
	}
}F;


const int N = 1 << 18;

VI g[N];
int w[N];

LL solve(vector<pair<int, LL>> vals, int cent)
{
	vector<LL> sums(SZ(vals));
	FOR(i, 0, SZ(vals))
		sums[i] = vals[i].S;
	
	sort(ALL(vals));
	sort(ALL(sums));
	
	F.init(SZ(sums));
	
	LL res = 0;
	for(auto [m, s] : vals)
	{
		//2 * max(m, w[cent]) + 1 <= s + s' + w[cent]
		//s' >= 2 * max + 1 - s - w[cent];
		LL low = 2 * max(m, w[cent]) + 1 - s - w[cent];
		int pos = lower_bound(ALL(sums), low) - sums.begin();
		
		res += F.query(SZ(sums) - 1) - F.query(pos - 1);
		
		pos = lower_bound(ALL(sums), s) - sums.begin();
		F.upd(pos, 1);
	}
	return res;
} 




int sz[N];
bool usedC[N];

int dfsSZ(int v, int par)
{
	sz[v] = 1;
	for(auto to : g[v])
	{
		if(to != par && !usedC[to])
			sz[v] += dfsSZ(to, v);
	}
	return sz[v];
}


vector<pair<int, LL>> verts;
void dfs(int v, int par, int d, LL sum)
{
	d = max(d, w[v]);
	sum += w[v];
	verts.PB({d, sum});
	
	for(int to : g[v])
	{
		if(to == par || usedC[to])
			continue;
		dfs(to, v, d, sum);	
	}
}

LL ans = 0;

void build(int u)
{
	dfsSZ(u, -1);
	int szAll = sz[u];
	int pr = u;
	while(true)
	{
		int v = -1;
		for(auto to : g[u])
		{
			if(to == pr || usedC[to])
				continue;
			if(sz[to] * 2 > szAll)
			{
				v = to;
				break;
			}
		}
		if(v == -1)
			break;
		pr = u;
		u = v;
	}
	int cent = u;
	usedC[cent] = true;
	
	vector<pair<int, LL>> vertsAll;
	
	for(int to : g[cent])
	{
		if(usedC[to])
			continue;
		dfs(to, cent, 0, 0);
		ans -= solve(verts, cent);
		
		vertsAll.insert(vertsAll.end(), ALL(verts));
		verts.clear();
	}
	ans += solve(vertsAll, cent);
	
	for(auto to : g[cent])
	{
		if(!usedC[to])
		{
			build(to);
		}
	}
}


void solve()
{
	int n;
	cin >> n;
	FOR(i, 0, n)
		cin >> w[i];
	FOR(i, 0, n - 1)
	{
		int a, b;
		cin >> a >> b;
		a--; b--;
		g[a].PB(b);
		g[b].PB(a);
	}
	ans = 0;
	build(0);
	cout << ans << "\n";
	
	FOR(i, 0, n)
	{
		g[i].clear();
		usedC[i] = 0;
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int t;
	cin >> t;
	while(t--)
		solve();
	
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3648kb

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: 150ms
memory: 5716kb

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:

117
18
112
117
128
93
8
3
110
4
1
95
19
4
12
49
46
12
20
128
10
50
105
1
25
62
2
104
1
140
0
4
108
60
113
11
59
98
28
80
125
127
10
73
7
55
26
7
72
8
9
31
15
0
122
0
15
0
19
19
53
140
61
1
4
112
26
96
18
47
16
19
44
75
15
40
12
18
10
1
53
139
10
15
60
72
12
125
71
3
37
140
17
90
28
100
9
24
134
1
4
...

result:

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