#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
constexpr int maxn = 2e5 + 5;
struct DS {
struct Node {
long v, lazy;
int lc, rc;
};
inline static int heap_ptr = 1;
inline static Node heap[1 << 25] {};
static int alloc() {
return heap_ptr++;
}
static void alloc(int& o) {
if (!o) {
o = alloc();
}
}
static void maintain(int o) {
auto& [v, lazy, lc, rc] = heap[o];
v = min(heap[lc].v, heap[rc].v) + lazy;
}
static void merge(int& a, int& b, int l, int r) {
if (!b) {
return;
} else if (!a) {
a = b;
return;
}
auto& [v, lazy, lc, rc] = heap[a];
int mid = (l + r) / 2;
lazy += heap[b].lazy;
merge(lc, heap[b].lc, l, mid);
merge(rc, heap[b].rc, mid + 1, r);
maintain(a);
}
friend void merge(DS& a, DS& b) {
merge(a.root, b.root, 0, maxn);
}
static long get(int o, int l, int r, int ind) {
if (!o) {
return 0;
}
auto& [v, lazy, lc, rc] = heap[o];
if (l == r) {
return v;
}
int mid = (l + r) / 2;
if (ind <= mid) {
return lazy + get(lc, l, mid, ind);
} else {
return lazy + get(rc, mid + 1, r, ind);
}
}
static void upd_add(int& o, long x) {
alloc(o);
heap[o].lazy += x;
maintain(o);
}
static long query_min(int o, int l, int r, int ql, int qr) {
if (!o) {
return 0;
}
auto& [v, lazy, lc, rc] = heap[o];
if (ql <= l && r <= qr) {
return v;
}
int mid = (l + r) / 2;
long ans = 1e18;
if (ql <= mid) {
ans = min(ans, query_min(lc, l, mid, ql, qr));
}
if (mid < qr) {
ans = min(ans, query_min(rc, mid + 1, r, ql, qr));
}
return ans + lazy;
}
static void upd_set(int& o, int l, int r, int ql, int qr, long x) {
alloc(o);
auto& [v, lazy, lc, rc] = heap[o];
if (ql <= l && r <= qr) {
lc = rc = 0;
v = lazy = x;
return;
}
int mid = (l + r) / 2;
x -= lazy;
if (ql <= mid) {
upd_set(lc, l, mid, ql, qr, x);
}
if (mid < qr) {
upd_set(rc, mid + 1, r, ql, qr, x);
}
maintain(o);
}
int root = 0;
long get(int ind) const {
return get(root, 0, maxn, ind);
}
void upd_add(long x) {
upd_add(root, x);
}
long query_min(int l, int r) const {
return query_min(root, 0, maxn, l, r);
}
void upd_set(int l, int r, long x) {
upd_set(root, 0, maxn, l, r, x);
}
void upd_min(int qr, long x) {
int l = 0, r = qr + 1;
while (l < r) {
int mid = (l + r) / 2;
if (query_min(mid, qr) >= x) {
r = mid;
} else {
l = mid + 1;
}
}
if (l <= qr) {
upd_set(l, qr, x);
}
}
};
int arr[maxn], cost[maxn];
vector<int> graph[maxn];
DS dp(int u, int p = -1) {
DS cur;
long cans = 0;
for (auto& v : graph[u]) {
if (v == p) {
continue;
}
auto&& ndp = dp(v, u);
cans += ndp.get(arr[u]);
merge(cur, ndp);
}
cur.upd_add(cost[u]);
cur.upd_min(arr[u], cans);
return cur;
}
void solve() {
int n;
cin >> n;
int par[n];
for (int i = 0; i < n; i++) {
cin >> par[i] >> arr[i] >> cost[i];
par[i]--;
}
for (int i = 1; i < n; i++) {
graph[par[i]].push_back(i);
}
vector<int> comp(arr, arr + n);
sort(begin(comp), end(comp));
comp.erase(unique(begin(comp), end(comp)), comp.end());
for (int i = 0; i < n; i++) {
arr[i] =
int(lower_bound(begin(comp), end(comp), arr[i]) - comp.begin());
}
cout << dp(0).get(0) << endl;
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
12 ms |
7472 KB |
Output is correct |
6 |
Correct |
11 ms |
6740 KB |
Output is correct |
7 |
Correct |
8 ms |
6452 KB |
Output is correct |
8 |
Correct |
12 ms |
7508 KB |
Output is correct |
9 |
Correct |
10 ms |
6740 KB |
Output is correct |
10 |
Correct |
8 ms |
6484 KB |
Output is correct |
11 |
Correct |
7 ms |
6444 KB |
Output is correct |
12 |
Correct |
15 ms |
6952 KB |
Output is correct |
13 |
Correct |
7 ms |
6100 KB |
Output is correct |
14 |
Correct |
15 ms |
6612 KB |
Output is correct |
15 |
Correct |
10 ms |
5808 KB |
Output is correct |
16 |
Correct |
12 ms |
7380 KB |
Output is correct |
17 |
Correct |
9 ms |
6700 KB |
Output is correct |
18 |
Correct |
7 ms |
6356 KB |
Output is correct |
19 |
Correct |
13 ms |
7092 KB |
Output is correct |
20 |
Correct |
10 ms |
6836 KB |
Output is correct |
21 |
Correct |
7 ms |
6700 KB |
Output is correct |
22 |
Correct |
9 ms |
7892 KB |
Output is correct |
23 |
Correct |
10 ms |
7472 KB |
Output is correct |
24 |
Correct |
13 ms |
7096 KB |
Output is correct |
25 |
Correct |
9 ms |
6868 KB |
Output is correct |
26 |
Correct |
11 ms |
6224 KB |
Output is correct |
27 |
Correct |
12 ms |
7380 KB |
Output is correct |
28 |
Correct |
15 ms |
7084 KB |
Output is correct |
29 |
Correct |
15 ms |
6624 KB |
Output is correct |
30 |
Correct |
14 ms |
5964 KB |
Output is correct |
31 |
Correct |
15 ms |
5984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
12 ms |
7472 KB |
Output is correct |
6 |
Correct |
11 ms |
6740 KB |
Output is correct |
7 |
Correct |
8 ms |
6452 KB |
Output is correct |
8 |
Correct |
12 ms |
7508 KB |
Output is correct |
9 |
Correct |
10 ms |
6740 KB |
Output is correct |
10 |
Correct |
8 ms |
6484 KB |
Output is correct |
11 |
Correct |
7 ms |
6444 KB |
Output is correct |
12 |
Correct |
15 ms |
6952 KB |
Output is correct |
13 |
Correct |
7 ms |
6100 KB |
Output is correct |
14 |
Correct |
15 ms |
6612 KB |
Output is correct |
15 |
Correct |
10 ms |
5808 KB |
Output is correct |
16 |
Correct |
12 ms |
7380 KB |
Output is correct |
17 |
Correct |
9 ms |
6700 KB |
Output is correct |
18 |
Correct |
7 ms |
6356 KB |
Output is correct |
19 |
Correct |
13 ms |
7092 KB |
Output is correct |
20 |
Correct |
10 ms |
6836 KB |
Output is correct |
21 |
Correct |
7 ms |
6700 KB |
Output is correct |
22 |
Correct |
9 ms |
7892 KB |
Output is correct |
23 |
Correct |
10 ms |
7472 KB |
Output is correct |
24 |
Correct |
13 ms |
7096 KB |
Output is correct |
25 |
Correct |
9 ms |
6868 KB |
Output is correct |
26 |
Correct |
11 ms |
6224 KB |
Output is correct |
27 |
Correct |
12 ms |
7380 KB |
Output is correct |
28 |
Correct |
15 ms |
7084 KB |
Output is correct |
29 |
Correct |
15 ms |
6624 KB |
Output is correct |
30 |
Correct |
14 ms |
5964 KB |
Output is correct |
31 |
Correct |
15 ms |
5984 KB |
Output is correct |
32 |
Correct |
12 ms |
7360 KB |
Output is correct |
33 |
Correct |
497 ms |
130008 KB |
Output is correct |
34 |
Correct |
361 ms |
90484 KB |
Output is correct |
35 |
Correct |
494 ms |
130124 KB |
Output is correct |
36 |
Correct |
352 ms |
90628 KB |
Output is correct |
37 |
Correct |
221 ms |
62840 KB |
Output is correct |
38 |
Correct |
197 ms |
60696 KB |
Output is correct |
39 |
Correct |
740 ms |
102748 KB |
Output is correct |
40 |
Correct |
409 ms |
55768 KB |
Output is correct |
41 |
Correct |
158 ms |
50892 KB |
Output is correct |
42 |
Correct |
724 ms |
91096 KB |
Output is correct |
43 |
Correct |
405 ms |
41548 KB |
Output is correct |
44 |
Correct |
458 ms |
129528 KB |
Output is correct |
45 |
Correct |
320 ms |
89372 KB |
Output is correct |
46 |
Correct |
156 ms |
60828 KB |
Output is correct |
47 |
Correct |
805 ms |
98020 KB |
Output is correct |
48 |
Correct |
317 ms |
82124 KB |
Output is correct |
49 |
Correct |
180 ms |
76124 KB |
Output is correct |
50 |
Correct |
305 ms |
132264 KB |
Output is correct |
51 |
Correct |
221 ms |
113568 KB |
Output is correct |
52 |
Correct |
731 ms |
95532 KB |
Output is correct |
53 |
Correct |
313 ms |
82744 KB |
Output is correct |
54 |
Correct |
435 ms |
57352 KB |
Output is correct |
55 |
Correct |
550 ms |
103276 KB |
Output is correct |
56 |
Correct |
642 ms |
82504 KB |
Output is correct |
57 |
Correct |
689 ms |
71896 KB |
Output is correct |
58 |
Correct |
724 ms |
43980 KB |
Output is correct |
59 |
Correct |
729 ms |
43852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
12 ms |
7472 KB |
Output is correct |
6 |
Correct |
11 ms |
6740 KB |
Output is correct |
7 |
Correct |
8 ms |
6452 KB |
Output is correct |
8 |
Correct |
12 ms |
7508 KB |
Output is correct |
9 |
Correct |
10 ms |
6740 KB |
Output is correct |
10 |
Correct |
8 ms |
6484 KB |
Output is correct |
11 |
Correct |
7 ms |
6444 KB |
Output is correct |
12 |
Correct |
15 ms |
6952 KB |
Output is correct |
13 |
Correct |
7 ms |
6100 KB |
Output is correct |
14 |
Correct |
15 ms |
6612 KB |
Output is correct |
15 |
Correct |
10 ms |
5808 KB |
Output is correct |
16 |
Correct |
12 ms |
7380 KB |
Output is correct |
17 |
Correct |
9 ms |
6700 KB |
Output is correct |
18 |
Correct |
7 ms |
6356 KB |
Output is correct |
19 |
Correct |
13 ms |
7092 KB |
Output is correct |
20 |
Correct |
10 ms |
6836 KB |
Output is correct |
21 |
Correct |
7 ms |
6700 KB |
Output is correct |
22 |
Correct |
9 ms |
7892 KB |
Output is correct |
23 |
Correct |
10 ms |
7472 KB |
Output is correct |
24 |
Correct |
13 ms |
7096 KB |
Output is correct |
25 |
Correct |
9 ms |
6868 KB |
Output is correct |
26 |
Correct |
11 ms |
6224 KB |
Output is correct |
27 |
Correct |
12 ms |
7380 KB |
Output is correct |
28 |
Correct |
15 ms |
7084 KB |
Output is correct |
29 |
Correct |
15 ms |
6624 KB |
Output is correct |
30 |
Correct |
14 ms |
5964 KB |
Output is correct |
31 |
Correct |
15 ms |
5984 KB |
Output is correct |
32 |
Correct |
12 ms |
7360 KB |
Output is correct |
33 |
Correct |
497 ms |
130008 KB |
Output is correct |
34 |
Correct |
361 ms |
90484 KB |
Output is correct |
35 |
Correct |
494 ms |
130124 KB |
Output is correct |
36 |
Correct |
352 ms |
90628 KB |
Output is correct |
37 |
Correct |
221 ms |
62840 KB |
Output is correct |
38 |
Correct |
197 ms |
60696 KB |
Output is correct |
39 |
Correct |
740 ms |
102748 KB |
Output is correct |
40 |
Correct |
409 ms |
55768 KB |
Output is correct |
41 |
Correct |
158 ms |
50892 KB |
Output is correct |
42 |
Correct |
724 ms |
91096 KB |
Output is correct |
43 |
Correct |
405 ms |
41548 KB |
Output is correct |
44 |
Correct |
458 ms |
129528 KB |
Output is correct |
45 |
Correct |
320 ms |
89372 KB |
Output is correct |
46 |
Correct |
156 ms |
60828 KB |
Output is correct |
47 |
Correct |
805 ms |
98020 KB |
Output is correct |
48 |
Correct |
317 ms |
82124 KB |
Output is correct |
49 |
Correct |
180 ms |
76124 KB |
Output is correct |
50 |
Correct |
305 ms |
132264 KB |
Output is correct |
51 |
Correct |
221 ms |
113568 KB |
Output is correct |
52 |
Correct |
731 ms |
95532 KB |
Output is correct |
53 |
Correct |
313 ms |
82744 KB |
Output is correct |
54 |
Correct |
435 ms |
57352 KB |
Output is correct |
55 |
Correct |
550 ms |
103276 KB |
Output is correct |
56 |
Correct |
642 ms |
82504 KB |
Output is correct |
57 |
Correct |
689 ms |
71896 KB |
Output is correct |
58 |
Correct |
724 ms |
43980 KB |
Output is correct |
59 |
Correct |
729 ms |
43852 KB |
Output is correct |
60 |
Correct |
2 ms |
4948 KB |
Output is correct |
61 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |