QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#344862#7305. Lowest Common AncestorIBoryTL 871ms13904kbC++172.3kb2024-03-05 16:30:172024-03-05 16:30:18

Judging History

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

  • [2024-03-05 16:30:18]
  • 评测
  • 测评结果:TL
  • 用时:871ms
  • 内存:13904kb
  • [2024-03-05 16:30:17]
  • 提交

answer

#include <bits/stdc++.h>
#define pii pair<int, int>
typedef long long ll;
using namespace std;

const int MAX = 200007, LIM = (1 << 9) - 1;
vector<int> G[MAX];
int W[MAX], H[MAX], P[MAX], Z[MAX], in[MAX], out[MAX], top[MAX], dn;
int S[MAX];

int DFS(int cur, int prev) {
	H[cur] = H[prev] + 1;
	P[cur] = prev;
	int& ret = Z[cur] = 1;
	for (int& next : G[cur]) {
		ret += DFS(next, cur);
		if (Z[next] > Z[G[cur][0]]) swap(next, G[cur][0]);
	}
	return ret;
}

void DFS2(int cur) {
	in[cur] = ++dn;
	for (int next : G[cur]) {
		top[next] = (next == G[cur][0] ? top[cur] : next);
		DFS2(next);
	}
	out[cur] = dn;
}

int LCA(int a, int b) {
	while (top[a] != top[b]) {
		if (H[top[a]] > H[top[b]]) swap(a, b);
		b = P[top[b]];
	}
	return (in[a] < in[b] ? a : b);
}

struct BIT {
	ll T[MAX];
	void Clear(int i) {
		for (; i < MAX; i += i & -i) T[i] = 0;
	}
	void Update(int i, ll v) {
		for (; i < MAX; i += i & -i) T[i] += v;
	}
	ll Query(int L, int R) {
		ll ret = 0; L--;
		for (; R; R -= R & -R) ret += T[R];
		for (; L; L -= L & -L) ret -= T[L];
		return ret;
	}
	ll PathQuery(int p) {
		ll ret = 0;
		while (p) {
			ret += Query(in[top[p]], in[p]);
			if (p == 1) return ret;
			p = P[top[p]];
		}
	}
} T, T2;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N;
	while (cin >> N, !cin.eof()) {
		for (int i = 1; i <= N; ++i) cin >> W[i];
		for (int i = 2; i <= N; ++i) {
			int p;
			cin >> p;
			G[p].push_back(i);
		}

		DFS(1, 1);
		top[1] = 1;
		DFS2(1);

		vector<pii> ord;
		for (int i = 1; i <= N; ++i) ord.emplace_back(H[i], i);
		sort(ord.begin(), ord.end(), greater<pii>());

		int lastUpdate = 0;
		for (int i = 1; i <= N; ++i) {
			ll ans = T.PathQuery(i) + T2.Query(in[i], out[i]) * W[i];
			for (int j = lastUpdate + 1; j < i; ++j) ans += W[LCA(i, j)];
			if (1 < i) cout << ans << '\n';

			if ((i & LIM) == LIM) {
				for (auto [_, j] : ord) {
					if (lastUpdate < j && j <= i) {
						S[j]++;
						T2.Update(in[j], 1);
					}
					for (int next : G[j]) S[j] += S[next];
					for (int next : G[j]) T.Update(in[next], W[j] * (S[j] - S[next]));
				}
				memset(S, 0, (N + 1) * 4);
				lastUpdate = i;
			}
		}

		for (int i = 1; i <= N; ++i) {
			G[i].clear();
			T.Clear(i);
			T2.Clear(i);
		}
		dn = 0;
	}
	return 0;
}

详细

Test #1:

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

input:

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

output:

1
2
1
3
5
4

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 41ms
memory: 13824kb

input:

13
887 4555 8570 5485 8611 9500 295 2499 83 4959 9772 8620 3825
4 4 1 3 7 12 2 1 9 5 8 12
16
3405 9213 9243 8188 2733 1333 1614 4907 4256 7506 4228 1286 6121 6155 4082 8084
1 12 9 4 2 13 3 8 9 7 11 15 8 1 10
5
7183 1481 9468 8242 1044
1 5 5 1
3
5758 3693 1694
1 2
20
6683 4426 5616 8166 810 4265 3000...

output:

887
6372
11857
20427
21897
22192
26895
7096
7179
47267
48895
57515
3405
6810
16053
24241
22833
15057
30886
34491
38747
37197
23773
65304
57242
55117
66877
7183
14366
15410
16454
5758
9451
6683
11109
23015
24891
29156
35244
41332
41537
37300
51071
58569
59109
70092
93562
98559
108242
88010
120021
991...

result:

ok 181926 numbers

Test #3:

score: 0
Accepted
time: 871ms
memory: 13904kb

input:

1477
3418 2604 3534 2752 131 645 5196 4830 3302 3842 1625 3740 9292 9939 1582 4155 3540 3753 7421 3629 7596 2043 1399 8955 2124 7955 6100 1346 1339 3935 767 4593 1720 3200 2248 6972 7122 9566 624 9948 6048 8338 208 8669 827 3770 4306 9503 1935 6488 454 3510 4974 2964 5407 138 5468 1142 1812 5864 803...

output:

3418
5803
9152
13820
18874
26680
33465
41014
25683
51131
42058
39772
46791
50431
71966
64478
59926
77557
63112
88734
65714
71174
94621
92142
101334
117673
92286
90454
122881
126056
126405
137417
149983
109256
150899
154398
173188
119649
166368
169452
175799
143976
216296
149549
223478
222792
195677
...

result:

ok 199907 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

200000
8748 791 4490 1890 3323 1988 6142 9186 1748 6247 5686 247 5513 8788 3823 7152 9488 7315 6394 4878 2869 5935 395 8470 9990 7448 3034 9602 5477 8257 9605 8576 2122 418 28 2865 6223 6295 8852 6891 1554 7663 869 728 486 5102 716 1349 82 3819 4154 9380 4368 9283 7842 4298 3903 5172 6847 2578 8050 ...

output:

8748
10980
21720
23004
28494
43086
40992
43082
51656
55659
43921
65566
70999
87217
105493
115456
108075
96154
99671
108252
123219
115979
129363
128272
161386
204287
213656
166933
194923
216010
171656
177783
187699
195840
210767
207893
227214
227883
277178
282916
225885
246528
237889
342600
263596
28...

result: