/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e5+100, M = 1e5, K = 1e3+10;
int ttt = 1;
struct Node{
Node *L, *R;
ll val, lazy;
Node(){
L = R = nullptr;
val = 0;
lazy = 0;
}
void extend(){
if(L == nullptr) L = new Node();
if(R == nullptr) R = new Node();
}
void push(){
L->val += lazy;
R->val += lazy;
L->lazy += lazy;
R->lazy += lazy;
lazy = 0;
}
void range_update(int l, int r, int ql, int qr, ll x){
if(ql > r || l > qr) return;
if(ql <= l && r <= qr){
val += x;
lazy += x;
return;
}
extend();
push();
int m = l+r>>1;
L->range_update(l, m, ql, qr, x);
R->range_update(m+1, r, ql, qr, x);
val = max(L->val, R->val);
}
ll get(int l, int r, int p){
if(r <= p) return val;
if(p==0) return 0;
extend();
push();
int m = l+r>>1;
if(p <= m)
return L->get(l, m, p);
return max(L->val, R->get(m+1, r, p));
}
ll gett(int l, int r, int p){
if(l == r) return val;
extend();
push();
int m = l+r>>1;
if(p <= m)
return L->gett(l, m, p);
return R->gett(m+1, r, p);
}
};
int n, m, k, sz[N], d[N];
vector<int> g[N];
set<int> T_time[N];
ll w[N], D[N];
vector<Node*> T;
void pre(int v){
sz[v] = 1;
for(int &u: g[v]){
pre(u);
sz[v] += sz[u];
if(sz[u] > sz[g[v][0]]) swap(u, g[v][0]);
}
}
void dfs(int v){
int big = g[v].empty() ? -1 : g[v][0];
for(int u: g[v]){
dfs(u);
}
ll bigger_dv_sum = 0;
if(big != -1){
swap(T[v], T[big]);
T_time[v].swap(T_time[big]);
if(d[v] > 0)
bigger_dv_sum += T[v]->get(1, M, M) - T[v]->get(1, M, d[v]);
}else{
T[v] = new Node();
}
for(int u: g[v]){
if(u == big) continue;
int last = M;
if(d[v] > 0)
bigger_dv_sum += T[u]->get(1, M, M) - T[u]->get(1, M, d[v]);
if(!T_time[u].empty()){
auto it = prev(T_time[u].end());
while(1){
int tm = *it;
ll val = T[u]->get(1, M, tm);
T[v]->range_update(1, M, tm, last, val);
T_time[v].insert(tm);
last = tm - 1;
if(it == T_time[u].begin()) break;
--it;
}
}
}
if(d[v] > 0){
if(bigger_dv_sum <= w[v]){
if(T_time[v].size() > 0){
ll dval = T[v]->gett(1, M, d[v]);
auto it = prev(T_time[v].end());
while(!T_time[v].empty() && *it > d[v]){
ll tm = *it;
ll val = T[v]->gett(1, M, tm);
T[v]->range_update(1, M, tm, M, val - dval);
T_time[v].erase(it);
if(!T_time[v].empty()){
it = prev(T_time[v].end());
}
}
}
T[v]->range_update(1, M, d[v], M, w[v]);
T_time[v].insert(d[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 = 0; i <= n; ++i) d[i] = w[i] = 0;
vector<int> val;
for(int i = 0; i < m; ++i){
int v, dd, e; cin >> v >> dd >> e;
d[v] = dd;
w[v] = e;
}
T.resize(n+1);
pre(1);
dfs(1);
cout << T[1]->val;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
//freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
magictree.cpp: In member function 'void Node::range_update(int, int, int, int, long long int)':
magictree.cpp:43:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int m = l+r>>1;
| ~^~
magictree.cpp: In member function 'long long int Node::get(int, int, int)':
magictree.cpp:53:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int m = l+r>>1;
| ~^~
magictree.cpp: In member function 'long long int Node::gett(int, int, int)':
magictree.cpp:62:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int m = l+r>>1;
| ~^~
magictree.cpp: In function 'int main()':
magictree.cpp:168:15: warning: unused variable 'aa' [-Wunused-variable]
168 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
138932 KB |
Output is correct |
2 |
Correct |
84 ms |
65108 KB |
Output is correct |
3 |
Correct |
506 ms |
351796 KB |
Output is correct |
4 |
Correct |
240 ms |
172652 KB |
Output is correct |
5 |
Correct |
263 ms |
175184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9052 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
131 ms |
96544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
14684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |