Submission #467882

# Submission time Handle Problem Language Result Execution time Memory
467882 2021-08-25T11:23:02 Z warner1129 Magic Tree (CEOI19_magictree) C++17
100 / 100
207 ms 45868 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

struct segment_tree {
	static const int maxn = 2e6 + 5;
	int ls[maxn] = {}, rs[maxn] = {};
	long long ma[maxn] = {}, tag[maxn] = {};
	int buf = 0;
	inline void updata(int p, long long v) { ma[p] += v, tag[p] += v; }
	inline void pull(int p) { ma[p] = max(ma[ls[p]], ma[rs[p]]); }
	inline void push(int p) {
		if (!tag[p] || !p) return;
		if (ls[p]) updata(ls[p], tag[p]);
		if (rs[p]) updata(rs[p], tag[p]);
		tag[p] = 0;
	}
	void add(int &p, int l, int r, int pos, long long val, long long m) {
		if (!p) p = ++buf;
		if (l == r) { ma[p] = val + max(m, ma[p]); return; }
		push(p);
		int mid = (l + r) >> 1;
		if (pos <= mid) add(ls[p], l, mid, pos, val, m);	
		else add(rs[p], mid+1, r, pos, val, max(m, ma[ls[p]]));
		pull(p);
	}
	void Merge(int &a, const int &b, int l, int r, long long am, long long bm) {
		if (!b) { if (a) updata(a, bm); return; } 
		if (!a) { updata(b, am); a = b; return; }
		if (l == r) {
			am = max(am, ma[a]), bm = max(bm, ma[b]);
			ma[a] = max(ma[a] + bm, am + ma[b]);
			return;
		}
		int mid = (l + r) >> 1;
		push(a), push(b);
		Merge(rs[a], rs[b], mid+1, r, max(am, ma[ls[a]]), max(bm, ma[ls[b]]));
		Merge(ls[a], ls[b], l, mid, am, bm);
		pull(a);
	}
} seg;

const int maxn = 1e5 + 5;

vector<int> G[maxn];
pair<int, long long> fru[maxn];
int rt[maxn], d;

void dfs(int u) {
	for (int v : G[u])
		dfs(v), seg.Merge(rt[u], rt[v], 1, d, 0, 0);
	if (fru[u].ff) seg.add(rt[u], 1, d, fru[u].ff, fru[u].ss, 0);
}

void solve() {
	int n, m;
	cin >> n >> m >> d;
	for (int i = 2, f; i <= n; i++)
		cin >> f, G[f].emplace_back(i);
	for (int i = 1; i <= m; i++) {
		int id, d; long long w;
		cin >> id >> d >> w;
		fru[id] = {d, w};
	}
	dfs(1);
	cout << seg.ma[rt[1]] << '\n';
}

signed main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2636 KB Output is correct
9 Correct 2 ms 2728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 17472 KB Output is correct
2 Correct 57 ms 18332 KB Output is correct
3 Correct 198 ms 45064 KB Output is correct
4 Correct 167 ms 44612 KB Output is correct
5 Correct 195 ms 45868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 3 ms 2816 KB Output is correct
3 Correct 2 ms 2820 KB Output is correct
4 Correct 74 ms 19900 KB Output is correct
5 Correct 84 ms 19944 KB Output is correct
6 Correct 83 ms 19880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 10820 KB Output is correct
2 Correct 58 ms 9048 KB Output is correct
3 Correct 76 ms 12928 KB Output is correct
4 Correct 44 ms 9664 KB Output is correct
5 Correct 52 ms 17728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2636 KB Output is correct
9 Correct 2 ms 2728 KB Output is correct
10 Correct 81 ms 15936 KB Output is correct
11 Correct 71 ms 14020 KB Output is correct
12 Correct 62 ms 13236 KB Output is correct
13 Correct 55 ms 13840 KB Output is correct
14 Correct 52 ms 16964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3840 KB Output is correct
2 Correct 29 ms 6892 KB Output is correct
3 Correct 35 ms 6872 KB Output is correct
4 Correct 41 ms 7092 KB Output is correct
5 Correct 16 ms 5580 KB Output is correct
6 Correct 29 ms 8364 KB Output is correct
7 Correct 30 ms 10976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2636 KB Output is correct
9 Correct 2 ms 2728 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 3 ms 2816 KB Output is correct
12 Correct 2 ms 2820 KB Output is correct
13 Correct 74 ms 19900 KB Output is correct
14 Correct 84 ms 19944 KB Output is correct
15 Correct 83 ms 19880 KB Output is correct
16 Correct 81 ms 15936 KB Output is correct
17 Correct 71 ms 14020 KB Output is correct
18 Correct 62 ms 13236 KB Output is correct
19 Correct 55 ms 13840 KB Output is correct
20 Correct 52 ms 16964 KB Output is correct
21 Correct 25 ms 8108 KB Output is correct
22 Correct 93 ms 19188 KB Output is correct
23 Correct 142 ms 25596 KB Output is correct
24 Correct 207 ms 41632 KB Output is correct
25 Correct 160 ms 43840 KB Output is correct
26 Correct 157 ms 34388 KB Output is correct
27 Correct 117 ms 24004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2636 KB Output is correct
9 Correct 2 ms 2728 KB Output is correct
10 Correct 79 ms 17472 KB Output is correct
11 Correct 57 ms 18332 KB Output is correct
12 Correct 198 ms 45064 KB Output is correct
13 Correct 167 ms 44612 KB Output is correct
14 Correct 195 ms 45868 KB Output is correct
15 Correct 2 ms 2764 KB Output is correct
16 Correct 3 ms 2816 KB Output is correct
17 Correct 2 ms 2820 KB Output is correct
18 Correct 74 ms 19900 KB Output is correct
19 Correct 84 ms 19944 KB Output is correct
20 Correct 83 ms 19880 KB Output is correct
21 Correct 71 ms 10820 KB Output is correct
22 Correct 58 ms 9048 KB Output is correct
23 Correct 76 ms 12928 KB Output is correct
24 Correct 44 ms 9664 KB Output is correct
25 Correct 52 ms 17728 KB Output is correct
26 Correct 81 ms 15936 KB Output is correct
27 Correct 71 ms 14020 KB Output is correct
28 Correct 62 ms 13236 KB Output is correct
29 Correct 55 ms 13840 KB Output is correct
30 Correct 52 ms 16964 KB Output is correct
31 Correct 8 ms 3840 KB Output is correct
32 Correct 29 ms 6892 KB Output is correct
33 Correct 35 ms 6872 KB Output is correct
34 Correct 41 ms 7092 KB Output is correct
35 Correct 16 ms 5580 KB Output is correct
36 Correct 29 ms 8364 KB Output is correct
37 Correct 30 ms 10976 KB Output is correct
38 Correct 25 ms 8108 KB Output is correct
39 Correct 93 ms 19188 KB Output is correct
40 Correct 142 ms 25596 KB Output is correct
41 Correct 207 ms 41632 KB Output is correct
42 Correct 160 ms 43840 KB Output is correct
43 Correct 157 ms 34388 KB Output is correct
44 Correct 117 ms 24004 KB Output is correct
45 Correct 26 ms 8244 KB Output is correct
46 Correct 93 ms 19600 KB Output is correct
47 Correct 127 ms 25812 KB Output is correct
48 Correct 198 ms 42328 KB Output is correct
49 Correct 176 ms 44720 KB Output is correct
50 Correct 165 ms 35052 KB Output is correct
51 Correct 127 ms 24772 KB Output is correct