#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 101010
#define MAXS 20
#define ln '\n'
struct segtree {
ll N, s;
vector<ll> tree, l, r;
void init(ll x = 1) {
if (x >= s) {
l[x] = r[x] = x - s + 1;
return;
}
init(x * 2);
init(x * 2 + 1);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
}
segtree(ll n) {
N = n;
s = 1LL << (ll)ceil(log2(N));
tree.resize(2 * s + 1);
l.resize(2 * s + 1);
r.resize(2 * s + 1);
init();
}
void update(ll x, ll a) {
x += s - 1;
tree[x] = max(tree[x], a);
x /= 2;
while (x) {
tree[x] = max(tree[x * 2], tree[x * 2 + 1]);
x /= 2;
}
}
ll query(ll low, ll high, ll loc = 1) {
if (low > high) return 0;
if (l[loc] == low && r[loc] == high) return tree[loc];
if (r[loc * 2] >= high) return query(low, high, loc * 2);
if (l[loc * 2 + 1] <= low) return query(low, high, loc * 2 + 1);
return max(query(low, r[loc * 2], loc * 2), query(l[loc * 2 + 1], high, loc * 2 + 1));
}
segtree() {}
};
struct Fruit {
ll v;
ll w;
};
ll sp[MAX][MAXS];
ll cnt = 0;
ll ord[MAX];
ll dp[MAX];
pll range[MAX];
vector<ll> adj[MAX];
vector<Fruit> fruit[MAX];
ll dfs(ll x = 1, ll p = 0) {
ord[x] = ++cnt;
ll mx = ord[x];
for (auto v : adj[x]) {
if (v == p) continue;
mx = max(mx, dfs(v, x));
}
range[x] = { ord[x], mx };
return mx;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
ll N, M, K;
cin >> N >> M >> K;
ll i, j;
for (i = 2; i <= N; i++) {
cin >> sp[i][0];
adj[sp[i][0]].push_back(i);
adj[i].push_back(sp[i][0]);
}
for (i = 1; i <= N; i++) {
for (j = 1; j < MAXS; j++) sp[i][j] = sp[sp[i][j - 1]][j - 1];
}
ll a;
Fruit f;
for (i = 1; i <= M; i++) {
cin >> f.v >> a >> f.w;
fruit[a].push_back(f);
}
/*
dfs();
segtree rmq;
rmq = segtree(N);
for (i = 1; i <= K; i++) {
for (auto f : fruit[i]) {
ll v = f.v;
ll w = f.w;
ll sum = 0;
for (auto x : adj[v]) {
if (x == sp[v][0]) continue;
sum += rmq.query(range[x].first, range[x].second);
}
rmq.update(ord[v], sum + w);
}
}
cout << rmq.query(2, N) << ln;
*/
for (i = K; i >= 1; i--) {
for (auto f : fruit[i]) {
ll v = f.v;
ll w = f.w;
ll d = w - dp[v];
d = max(d, 0LL);
while (v) {
dp[v] += d;
v = sp[v][0];
}
}
}
cout << dp[1] << ln;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
27136 KB |
Output is correct |
2 |
Execution timed out |
2069 ms |
26416 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
110 ms |
28944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9164 KB |
Output is correct |
2 |
Correct |
69 ms |
25784 KB |
Output is correct |
3 |
Correct |
60 ms |
25784 KB |
Output is correct |
4 |
Correct |
69 ms |
25724 KB |
Output is correct |
5 |
Correct |
31 ms |
25656 KB |
Output is correct |
6 |
Incorrect |
516 ms |
25960 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |