Submission #824306

# Submission time Handle Problem Language Result Execution time Memory
824306 2023-08-14T00:29:26 Z NK_ Magic Tree (CEOI19_magictree) C++17
20 / 100
118 ms 22532 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using ll = long long;
using vl = V<ll>;

struct Seg {
	const ll ID = 0, IDL = -1; ll comb(ll a, ll b) { return max(a, b); }
	int n; vl seg, lazy; void init(int _n) {
		n = 1; while(n < _n) n *= 2; 
		seg.assign(2 * n, ID); lazy.assign(2 * n, IDL);
	}
	void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); }
	void push(int x, int L, int R) {
		if (lazy[x] != IDL) {
			seg[x] = lazy[x];
			if (L != R) for(int i = 0; i < 2; i++) lazy[2*x+i] = lazy[x];
			lazy[x] = IDL;
		}
	}

	void upd(int l, int r, ll v, int x, int L, int R) {
		push(x, L, R); if (r < L || R < l) return;
		if (l <= L && R <= r) {
			lazy[x] = v; push(x, L, R); return;
		}

		int M = (L + R) / 2;
		upd(l, r, v, 2 * x, L, M);
		upd(l, r, v, 2 * x + 1, M + 1, R);
		pull(x);
	}

	ll query(int l, int r, int x, int L, int R) {
		push(x, L, R); if (r < L || R < l) return ID;
		if (l <= L && R <= r) return seg[x];

		int M = (L + R) / 2;
		return comb(
			query(l, r, 2 * x, L, M), 
			query(l, r, 2 * x + 1, M + 1, R)
		);
	}

	void upd(int l, int r, ll v) { upd(l, r, v, 1, 0, n - 1); }
	ll query(int l, int r) { return query(l, r, 1, 0, n - 1); }
};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M, K; cin >> N >> M >> K; 

	// (euler tour + segment tree + offline queries)? to compute dp
	// then basic tree dp with dp values to get answer

	V<vi> chd(N); for(int u = 1; u < N; u++) {
		int p; cin >> p; --p;
		chd[p].pb(u);
	}

	vi st(N), en(N); int t = 0;
	function<void(int)> gen = [&](int u) {
		st[u] = t++;
		for(auto& v : chd[u]) gen(v);
		en[u] = t-1;
	};

	gen(0);

	V<array<int, 3>> E;
	for(int i = 0; i < M; i++) {
		int v, d, w; cin >> v >> d >> w; --v, --d;
		E.push_back({d, v, w});
	}

	sort(begin(E), end(E), [&](array<int, 3> x, array<int, 3> y) {
		if (x[0] == y[0]) return st[x[1]] > st[y[1]];
		return x[0] < y[0];
	});

	Seg T; T.init(N);
	vl dp(N, 0);

	for(auto q : E) {
		auto [d, u, w] = q;
		// cout << d << " " << u << endl;

		dp[u] = w;
		for(auto& v : chd[u]) {
			// cout << v << endl;
			// cout << st[v] << " " << en[v] << endl;
			// cout << T.query(st[v], en[v]) << endl;
			// cout << endl;
			dp[u] += T.query(st[v], en[v]);
		}
		// T.upd(st[u] + 1, en[u], 0);
		T.upd(st[u], st[u], dp[u]);
		// cout << u << " - " << dp[u] << endl;
	}

	function<ll(int)> ans = [&](int u) {
		ll best = 0;
		for(auto& v : chd[u]) best += ans(v);
		return max(best, dp[u]);
	};

	cout << ans(0) << nl;

    return 0;
}			

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 10132 KB Output is correct
2 Correct 27 ms 14244 KB Output is correct
3 Correct 68 ms 10196 KB Output is correct
4 Correct 78 ms 10180 KB Output is correct
5 Correct 69 ms 11140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 74 ms 22508 KB Output is correct
5 Correct 71 ms 22532 KB Output is correct
6 Correct 118 ms 22508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 82 ms 11132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Incorrect 82 ms 12612 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 444 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 74 ms 22508 KB Output is correct
14 Correct 71 ms 22532 KB Output is correct
15 Correct 118 ms 22508 KB Output is correct
16 Incorrect 82 ms 12612 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 34 ms 10132 KB Output is correct
11 Correct 27 ms 14244 KB Output is correct
12 Correct 68 ms 10196 KB Output is correct
13 Correct 78 ms 10180 KB Output is correct
14 Correct 69 ms 11140 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 444 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 74 ms 22508 KB Output is correct
19 Correct 71 ms 22532 KB Output is correct
20 Correct 118 ms 22508 KB Output is correct
21 Incorrect 82 ms 11132 KB Output isn't correct
22 Halted 0 ms 0 KB -