답안 #523069

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523069 2022-02-06T22:04:48 Z Blondie Magic Tree (CEOI19_magictree) C++17
100 / 100
837 ms 596568 KB
#include <bits/stdc++.h>
using namespace std;

#define sz(x) ((int)(x.size()))
#define all(x) x.begin(), x.end()

template<typename T> void pr(T a){std::cerr<<a<<std::endl;}
template<typename T, typename... Args> void pr(T a, Args... args){std::cerr<<a<<' ',pr(args...);}

using ll = long long;

const char nl = '\n';
const int MX = 1e5 + 10;

struct Node {
	Node *ls, *rs;
	ll mx, mn, lz;
	Node(ll hi = 0, ll lo = 0) : ls(NULL), rs(NULL), mx(hi), mn(lo), lz(0) {}
};

void addChild(Node* v) {
	if(v->ls) return;
// cerr << v->mx << " " << v->mn << nl;
	assert(v->mx == v->mn);
	v->ls = new Node(v->mx - v->lz, v->mn - v->lz);
	v->rs = new Node(v->mx - v->lz, v->mn - v->lz);
}

void prop(Node* v) {
	if(!v->ls) return;
	ll& L = v->lz;
	v->ls->lz += L;
	v->ls->mx += L;
	v->ls->mn += L;

	v->rs->lz += L;
	v->rs->mx += L;
	v->rs->mn += L;
	L = 0;
}

void pull(Node* v) {
	v->mx = max(v->ls->mx, v->rs->mx);
	v->mn = min(v->ls->mn, v->rs->mn);
}

void upd(Node* v, int b, int e, int l, int r, ll x) {
	if(l >= r) return;
	addChild(v);
	if(b == l && e == r) {
		v->mx += x;
		v->mn += x;
		v->lz += x;
		return;
	}
	prop(v);
	int m = (b + e) / 2;
	upd(v->ls, b, m, l, min(r, m), x);
	upd(v->rs, m, e, max(l, m), r, x);
	pull(v);
}

void upd2(Node* v, int b, int e, int l, int r, ll x) {
	if(l >= r || v->mn >= x) return;
	addChild(v);
	if(b == l && e == r && v->mx <= x) {
		v->ls = v->rs = NULL;
		v->mx = x;
		v->mn = x;
		v->lz = 0;
//		pr("upd:", b+1, e, x);
		return;
	}
	prop(v);
	int m = (b + e) / 2;
	upd2(v->ls, b, m, l, min(r, m), x);
	upd2(v->rs, m, e, max(l, m), r, x);
	pull(v);
}

ll qry(Node* v, int b, int e, int l, int r) {
	if(l >= r) return 0;
	addChild(v);
	if(b == l && e == r) {
		return v->mx;
	}
	prop(v);
	int m = (b + e) / 2;
	return max(qry(v->ls, b, m, l, min(r, m)), qry(v->rs, m, e, max(l, m), r));
}

vector<pair<int,ll>> leaves;
void getLeaves(Node* v, int b, int e) {
	if(!v) return;
	if(e - b == 1 || !v->ls) {
		assert(v->mx == v->mn);
		if(!sz(leaves) || v->mx > leaves.back().second) {
			leaves.push_back({b, v->mx});
		}
		return;
	}
	prop(v);
	int m = (b + e) / 2;
	getLeaves(v->ls, b, m);
	getLeaves(v->rs, m, e);
}

vector<int> adj[MX];
Node* tree[MX];
int val[MX], day[MX];
int n, m, k, sz[MX];

void dfsSz(int v) {
	sz[v] = 1;
	for(int u : adj[v]) {
		dfsSz(u);
		sz[v] += sz[u];
	}
}

void dfs(int v) {
	int big = -1;
	for(int u : adj[v]) {
		dfs(u);
		if(big == -1 || sz[big] < sz[u]) {
			big = u;
		}
	}
	if(big == -1) {
		tree[v] = new Node();
	} else {
		tree[v] = tree[big];
	}
	//cerr << "doing: " << v+1 << nl;
	for(int u : adj[v]) {
		if(u != big) {
			leaves.clear();
			getLeaves(tree[u], 0, k);
			ll prv = 0;
			for(int i = 0; i < sz(leaves); i++) {
				int nxt = i + 1 == sz(leaves) ? k : leaves[i + 1].first;
				auto [pos, x] = leaves[i];
//				cerr << prv << " " << x << " -> " << pos << " " << nxt << nl;
				assert(prv <= x);
				upd(tree[v], 0, k, pos, nxt, x);
				prv = x;
			}
		}
	}
	if(day[v] != -1) {
		ll me = val[v] + qry(tree[v], 0, k, 0, day[v]+1);
		upd2(tree[v], 0, k, day[v], k, me);
//		cerr << v+1 << ": " << me << nl;
	}
}

void solve() {
	cin >> n >> m >> k;
	fill_n(day, n, -1);
	for(int i = 1; i < n; i++) {
		int p;
		cin >> p; --p;
		adj[p].push_back(i);
	}
	ll should = 0;
	for(int i = 0; i < m; i++) {
		int v;
		cin >> v; --v;
		cin >> day[v] >> val[v];
		--day[v];
		should += val[v];
	}
	dfsSz(0);
	dfs(0);
	cout << tree[0]->mx << nl;
//	assert(tree[0]->mx <= should);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int te = 1;
//	cin >> te;
	while(te--) {
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2712 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2664 KB Output is correct
9 Correct 1 ms 2672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 316 ms 228672 KB Output is correct
2 Correct 131 ms 86492 KB Output is correct
3 Correct 837 ms 596568 KB Output is correct
4 Correct 432 ms 300544 KB Output is correct
5 Correct 461 ms 301524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2892 KB Output is correct
2 Correct 2 ms 3196 KB Output is correct
3 Correct 2 ms 3068 KB Output is correct
4 Correct 112 ms 41196 KB Output is correct
5 Correct 169 ms 113228 KB Output is correct
6 Correct 196 ms 143172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 25780 KB Output is correct
2 Correct 62 ms 19132 KB Output is correct
3 Correct 63 ms 23476 KB Output is correct
4 Correct 58 ms 28272 KB Output is correct
5 Correct 56 ms 26180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2712 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2664 KB Output is correct
9 Correct 1 ms 2672 KB Output is correct
10 Correct 155 ms 71096 KB Output is correct
11 Correct 113 ms 53804 KB Output is correct
12 Correct 79 ms 33024 KB Output is correct
13 Correct 113 ms 77624 KB Output is correct
14 Correct 64 ms 26188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 11264 KB Output is correct
2 Correct 44 ms 18296 KB Output is correct
3 Correct 47 ms 18624 KB Output is correct
4 Correct 55 ms 20852 KB Output is correct
5 Correct 20 ms 13944 KB Output is correct
6 Correct 31 ms 14708 KB Output is correct
7 Correct 32 ms 14532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2712 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2664 KB Output is correct
9 Correct 1 ms 2672 KB Output is correct
10 Correct 2 ms 2892 KB Output is correct
11 Correct 2 ms 3196 KB Output is correct
12 Correct 2 ms 3068 KB Output is correct
13 Correct 112 ms 41196 KB Output is correct
14 Correct 169 ms 113228 KB Output is correct
15 Correct 196 ms 143172 KB Output is correct
16 Correct 155 ms 71096 KB Output is correct
17 Correct 113 ms 53804 KB Output is correct
18 Correct 79 ms 33024 KB Output is correct
19 Correct 113 ms 77624 KB Output is correct
20 Correct 64 ms 26188 KB Output is correct
21 Correct 89 ms 55308 KB Output is correct
22 Correct 255 ms 163064 KB Output is correct
23 Correct 421 ms 322832 KB Output is correct
24 Correct 707 ms 536636 KB Output is correct
25 Correct 407 ms 299904 KB Output is correct
26 Correct 364 ms 227184 KB Output is correct
27 Correct 242 ms 148824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2712 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2664 KB Output is correct
9 Correct 1 ms 2672 KB Output is correct
10 Correct 316 ms 228672 KB Output is correct
11 Correct 131 ms 86492 KB Output is correct
12 Correct 837 ms 596568 KB Output is correct
13 Correct 432 ms 300544 KB Output is correct
14 Correct 461 ms 301524 KB Output is correct
15 Correct 2 ms 2892 KB Output is correct
16 Correct 2 ms 3196 KB Output is correct
17 Correct 2 ms 3068 KB Output is correct
18 Correct 112 ms 41196 KB Output is correct
19 Correct 169 ms 113228 KB Output is correct
20 Correct 196 ms 143172 KB Output is correct
21 Correct 97 ms 25780 KB Output is correct
22 Correct 62 ms 19132 KB Output is correct
23 Correct 63 ms 23476 KB Output is correct
24 Correct 58 ms 28272 KB Output is correct
25 Correct 56 ms 26180 KB Output is correct
26 Correct 155 ms 71096 KB Output is correct
27 Correct 113 ms 53804 KB Output is correct
28 Correct 79 ms 33024 KB Output is correct
29 Correct 113 ms 77624 KB Output is correct
30 Correct 64 ms 26188 KB Output is correct
31 Correct 17 ms 11264 KB Output is correct
32 Correct 44 ms 18296 KB Output is correct
33 Correct 47 ms 18624 KB Output is correct
34 Correct 55 ms 20852 KB Output is correct
35 Correct 20 ms 13944 KB Output is correct
36 Correct 31 ms 14708 KB Output is correct
37 Correct 32 ms 14532 KB Output is correct
38 Correct 89 ms 55308 KB Output is correct
39 Correct 255 ms 163064 KB Output is correct
40 Correct 421 ms 322832 KB Output is correct
41 Correct 707 ms 536636 KB Output is correct
42 Correct 407 ms 299904 KB Output is correct
43 Correct 364 ms 227184 KB Output is correct
44 Correct 242 ms 148824 KB Output is correct
45 Correct 80 ms 56952 KB Output is correct
46 Correct 260 ms 167756 KB Output is correct
47 Correct 466 ms 334708 KB Output is correct
48 Correct 763 ms 567072 KB Output is correct
49 Correct 428 ms 300768 KB Output is correct
50 Correct 389 ms 231924 KB Output is correct
51 Correct 282 ms 159752 KB Output is correct