/* 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 inc = w[v], exc = 0;
for(int u: g[v]){
dfs(u, v);
inc += get(1, n, seg_pos[u], 1);
exc += dp[u];
}
dp[v] = max(inc, exc);
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:155:16: warning: unused variable 'aa' [-Wunused-variable]
155 | int T = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70740 KB |
Output is correct |
2 |
Correct |
34 ms |
70820 KB |
Output is correct |
3 |
Correct |
34 ms |
70720 KB |
Output is correct |
4 |
Correct |
36 ms |
70760 KB |
Output is correct |
5 |
Incorrect |
34 ms |
70916 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
86000 KB |
Output is correct |
2 |
Correct |
122 ms |
89224 KB |
Output is correct |
3 |
Correct |
172 ms |
85884 KB |
Output is correct |
4 |
Correct |
115 ms |
85356 KB |
Output is correct |
5 |
Correct |
169 ms |
85024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
70996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
236 ms |
86120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70740 KB |
Output is correct |
2 |
Correct |
34 ms |
70820 KB |
Output is correct |
3 |
Correct |
34 ms |
70720 KB |
Output is correct |
4 |
Correct |
36 ms |
70760 KB |
Output is correct |
5 |
Incorrect |
34 ms |
70916 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
74124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70740 KB |
Output is correct |
2 |
Correct |
34 ms |
70820 KB |
Output is correct |
3 |
Correct |
34 ms |
70720 KB |
Output is correct |
4 |
Correct |
36 ms |
70760 KB |
Output is correct |
5 |
Incorrect |
34 ms |
70916 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70740 KB |
Output is correct |
2 |
Correct |
34 ms |
70820 KB |
Output is correct |
3 |
Correct |
34 ms |
70720 KB |
Output is correct |
4 |
Correct |
36 ms |
70760 KB |
Output is correct |
5 |
Incorrect |
34 ms |
70916 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |