Submission #824298

# Submission time Handle Problem Language Result Execution time Memory
824298 2023-08-14T00:02:11 Z NK_ Magic Tree (CEOI19_magictree) C++17
0 / 100
38 ms 12460 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; ll comb(ll a, ll b) { return max(a, b); }
	int n; vl seg; void init(int _n) {
		n = 1; while(n < _n) n *= 2; seg.assign(2 * n, ID);
	}
	void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); }
	void upd(int p, ll x) { 
		seg[p += n] = x; for(p /= 2; p; p /= 2) pull(x);
	}
	int query(int l, int r) {
		ll ra = ID, rb = ID;
		for(l += n, r += n+1; l < r; l /= 2, r /= 2) {
			if (l&1) ra = comb(ra, seg[l++]);
			if (r&1) rb = comb(seg[--r], rb);
		}
		return comb(ra, rb);
	}
};
 
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});
	}
 

	assert(false);

	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);
 
	for(auto q : E) {
		auto [d, u, w] = q;
		// cout << d << " " << u << endl;
 
		int best = 0;
		for(auto& v : chd[u]) best += T.query(st[v], en[v]);
		best += w; T.upd(st[u], best);
		dp[u] = best;
		// cout << u << " " << w << " " << best << 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 Runtime error 1 ms 512 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 23 ms 10604 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 724 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 38 ms 12460 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 512 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 2388 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 512 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 512 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -