#include<bits/stdc++.h>
using namespace std;
#define NAME ""
#define ll long long
#define pii pair < int , int >
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i ++)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; i --)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i ++)
#define bit(x, i) (((x) >> (i)) & 1ll)
#define mask(x) (1ll << (x))
#define mem(f, x) memset(f, x, sizeof(f))
#define sz(x) (int32_t) (x.size())
const int nmax = 1e5;
int in[nmax + 5], out[nmax + 5], timer = 0;
vector < int > adj[nmax + 5];
vector < pii > fruits[nmax + 5];
struct SegTree {
int n;
bool del[nmax * 4 + 5];
ll b[nmax * 4 + 5];
void init(int _n) {
n = _n;
}
void down(int id) {
if (!del[id]) {
return;
}
del[id << 1] = 1;
del[id << 1 | 1] = 1;
b[id << 1] = 0;
b[id << 1 | 1] = 0;
del[id] = 0;
}
void update(int id, int l, int r, int u, int v, int val) {
if (l > v || u > r) {
return;
}
if (u <= l && r <= v) {
if (val != -1) {
b[id] = val;
}
else {
del[id] = 1;
b[id] = 0;
}
return;
}
down(id);
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
b[id] = b[id << 1] + b[id << 1 | 1];
}
ll get(int id, int l, int r, int u, int v) {
if (u > r || l > v) {
return 0;
}
if (u <= l && r <= v) {
return b[id];
}
down(id);
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
void update(int l, int r, int val) {
update(1, 1, n, l, r, val);
// cout << l << " " << r << " " << val << "\n";
}
ll get(int l, int r) {
return get(1, 1, n, l, r);
}
}seg;
void dfs(int i) {
in[i] = ++ timer;
for (auto x: adj[i]) {
dfs(x);
}
out[i] = timer;
}
signed main() {
if (fopen(NAME".inp", "r")) {
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int n, m, k;
cin >> n >> m >> k;
seg.init(n);
FOR(i, 2, n) {
int p;
cin >> p;
adj[p].push_back(i);
}
dfs(1);
for (; m --;) {
int v, d, w;
cin >> v >> d >> w;
fruits[d].push_back({v, w});
}
FORD(i, k, 1) {
sort(fruits[i].begin(), fruits[i].end(), [&] (const pii &x, const pii &y) {
return (in[x.fi] < in[y.fi]);
});
for (auto x: fruits[i]) {
int u = x.fi;
if (seg.get(in[u], out[u]) <= x.se) {
seg.update(in[u], out[u], -1);
seg.update(in[u], in[u], x.se);
}
}
}
cout << seg.b[1];
return 0;
}
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:106:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:107:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11356 KB |
Output is correct |
2 |
Correct |
27 ms |
11580 KB |
Output is correct |
3 |
Correct |
72 ms |
13292 KB |
Output is correct |
4 |
Correct |
63 ms |
12496 KB |
Output is correct |
5 |
Correct |
67 ms |
13136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5180 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
12740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6372 KB |
Output is correct |
2 |
Correct |
17 ms |
10180 KB |
Output is correct |
3 |
Correct |
16 ms |
10332 KB |
Output is correct |
4 |
Correct |
18 ms |
10372 KB |
Output is correct |
5 |
Correct |
8 ms |
8892 KB |
Output is correct |
6 |
Incorrect |
15 ms |
9956 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |