/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD1 (1000000000+7)
#define MOD (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
int n, m, k, ti[N], sz[N], timer = 1, tin[N], seg_pos[N], par[N], heavy[N], o_t[N];
vector<int> g[N], path[N];
vector<pair<int, int>> times[N];
ll w[N], dp[N], t[N], lazy[N];
void push(int k){
if(lazy[k] > 0){
lazy[k<<1] = max(lazy[k<<1], lazy[k]);
lazy[k<<1|1] = max(lazy[k<<1|1], lazy[k]);
t[k<<1] = max(t[k<<1], lazy[k]);
t[k<<1|1] = max(t[k<<1|1], lazy[k]);
lazy[k] = 0;
}
}
void build(int l, int r, int k){
if(l==r){
t[k] = 0;
return;
}
int m = (l+r)>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
t[k] = 0;
}
ll get(int l, int r, int p, int k){
if(l == r) return t[k];
push(k);
int m = (l+r)>>1;
if(m>=p)
return max(t[k], get(l, m, p, k<<1));
return max(t[k], get(m+1, r, p, k<<1|1));
}
void update(int l, int r, int ql, int qr, int k, ll val){
if(l > qr || r < ql) return;
if(ql <= l && r <= qr){
t[k] = max(val, t[k]);
lazy[k] = max(val, lazy[k]);
return;
}
push(k);
int m = (l+r)>>1;
update(l, m, ql, qr, k<<1, val);
update(m+1, r, ql, qr, k<<1|1, val);
}
void pre(int v, int p){
sz[v] = 1;
par[v] = p;
for(int &u: g[v]){
pre(u, v);
sz[v] += sz[u];
if(sz[u] > sz[g[v][0]]) swap(u, g[v][0]);
}
}
void pre_dfs(int v, int p){
tin[v] = timer++;
path[heavy[v]].pb(v);
for(int &u: g[v]){
heavy[u] = (g[v][0] == u ? heavy[v] : u);
pre_dfs(u, v);
ti[u] = ti[v];
}
}
void dfs(int v, int p){
ll dp2 = 0;
for(int u: g[v]){
dfs(u, v);
dp[v] += get(1, n, seg_pos[u], 1);
dp2 += dp[u];
}
dp[v] += w[v];
dp[v] = max(dp[v], dp2);
int T = o_t[v];
int V = v;
while(v > 0){
int pos = upper_bound(all(times[heavy[v]]), make_pair(T, -1)) - times[heavy[v]].begin();
int S = times[heavy[v]].size() - 1;
if(pos <= S){
update(1, n, tin[heavy[v]], tin[heavy[v]] + S - pos, 1, dp[V]);
}
v = par[heavy[v]];
}
}
void solve(){
cin >> n >> m >> k;
for(int i = 2; i <= n; ++i){
int p; cin >> p;
g[p].pb(i);
}
for(int i = 1; i <= n; ++i) o_t[i] = t[i] = w[i] = dp[i] = 0;
for(int i = 0; i < m; ++i){
int v, d, e; cin >> v >> d >> e;
o_t[v] = ti[v] = d;
w[v] = e;
}
ti[1] = MOD;
w[1] = 0;
pre(1, 0);
heavy[1] = 1;
pre_dfs(1, 0);
for(int i = 1; i <= n; ++i){
times[i].pb({-1, 0});
for(int v: path[i]){
times[i].pb({ti[v], v});
}
sort(all(times[i]));
int S = times[i].size() - 1;
for(int j = 1; j <= S; ++j){
int v = times[i][j].second;
seg_pos[v] = tin[i] + S - j;
}
}
build(1, n, 1);
dfs(1, 0);
cout << dp[1];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:157:16: warning: unused variable 'aa' [-Wunused-variable]
157 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
70756 KB |
Output is correct |
2 |
Correct |
34 ms |
70808 KB |
Output is correct |
3 |
Correct |
35 ms |
70828 KB |
Output is correct |
4 |
Correct |
34 ms |
70744 KB |
Output is correct |
5 |
Incorrect |
36 ms |
70828 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
234 ms |
87116 KB |
Output is correct |
2 |
Correct |
121 ms |
90248 KB |
Output is correct |
3 |
Correct |
168 ms |
88352 KB |
Output is correct |
4 |
Correct |
112 ms |
87392 KB |
Output is correct |
5 |
Correct |
165 ms |
87524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
71048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
249 ms |
88220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
70756 KB |
Output is correct |
2 |
Correct |
34 ms |
70808 KB |
Output is correct |
3 |
Correct |
35 ms |
70828 KB |
Output is correct |
4 |
Correct |
34 ms |
70744 KB |
Output is correct |
5 |
Incorrect |
36 ms |
70828 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
74188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
70756 KB |
Output is correct |
2 |
Correct |
34 ms |
70808 KB |
Output is correct |
3 |
Correct |
35 ms |
70828 KB |
Output is correct |
4 |
Correct |
34 ms |
70744 KB |
Output is correct |
5 |
Incorrect |
36 ms |
70828 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
70756 KB |
Output is correct |
2 |
Correct |
34 ms |
70808 KB |
Output is correct |
3 |
Correct |
35 ms |
70828 KB |
Output is correct |
4 |
Correct |
34 ms |
70744 KB |
Output is correct |
5 |
Incorrect |
36 ms |
70828 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |