QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#136060#674. Ascending TreeFISHER_WA 3ms34360kbC++141.6kb2023-08-06 22:06:252023-08-06 22:06:28

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-06 22:06:28]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:34360kb
  • [2023-08-06 22:06:25]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
#define PB push_back
#define EB emplace_back 
using namespace std;
const int maxn = 1000000;
int f[maxn + 5], c[maxn + 5];
int dep[maxn + 5];
vector<int> g[maxn + 5];
struct heap_node {
	int v, d;
	int ls, rs, fa;
	int sz, rsz, rt;
} p[maxn + 5];
int merge(int x, int y) {
	if (!x || !y) return x | y;
	if (p[x].v > p[y].v) swap(x, y);
	else p[x].rt = p[y].rt;
	p[x].sz += p[y].sz, p[x].rsz += p[y].rsz;
	p[x].rs = merge(p[x].rs, y);
	if (p[p[x].ls].d < p[p[x].rs].d) swap(p[x].ls, p[x].rs);
	p[x].d = p[p[x].rs].d + 1;
	p[p[x].ls].fa = p[p[x].rs].fa = x;
	return x;
}
inline void del(int& x) {
	p[p[x].ls].fa = p[x].ls, p[p[x].rs].fa = p[x].rs;
	p[x].fa = p[x].ls ? p[x].ls : p[x].rs;
	int rt = p[x].rt, rsz = p[x].rsz;
	x = merge(p[x].ls, p[x].rs);
	p[x].rt = rt, p[x].rsz = rsz;
}
int findAnc(int x) {
	if (x == p[x].fa) return x;
	return p[x].fa = findAnc(p[x].fa);
}
int main() {
	int n;
	scanf("%d%d", &n, &c[1]);
	for (int i = 2; i <= n; i++) {
		scanf("%d%d", &f[i], &c[i]);
		g[f[i]].PB(i);
		dep[i] = dep[f[i]] + 1;
		c[i] += dep[i];
	}
	priority_queue<pair<int, int>> q;
	q.emplace(c[1], 1);
	while (!q.empty()) {
		int u = q.top().se; q.pop();
		for (int v : g[u]) q.emplace(c[v], v);
		p[u] = {c[u], 0, 0, 0, u, 1, 1, u};
		int fa = findAnc(f[p[u].rt]);
		while (fa && p[u].v > p[fa].v) {
			u = merge(u, fa);
			while (2 * p[u].sz - 1 > p[u].rsz) del(u);
			fa = findAnc(f[p[u].rt]); 
		}
	}
	long long ans = 0;
	for (int i = 1; i <= n; i++) ans += abs(c[i] - p[findAnc(i)].v);
	printf("%lld", ans);
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 34360kb

input:

21 -28
1 -21
2 -7
3 -3
4 -44
4 21
4 -39
4 42
2 -49
9 16
8 -22
5 -49
11 -8
1 3
11 28
3 8
8 -38
16 34
4 -45
7 -43
2 7

output:

295

result:

wrong answer 1st lines differ - expected: '290', found: '295'