#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 1000010
#define INF (ll)1e18
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
#define DEBUG 0
int TC,N,M,K;
int D[maxn],W[maxn];
vi adj[maxn];
int p;
struct node{
int s,e,m;
node *l=nullptr,*r=nullptr;
bool linit=0,rinit=0;
int val = 0;
node(int ss,int ee){
s = ss; e = ee; m = (s + e) / 2;
}
int qry(int a,int b){
if (a <= s && e <= b) return val;
else if (a > e || s > b) return 0;
else{
int ans = 0;
if (linit) ans += l->qry(a,b);
if (rinit) ans += r->qry(a,b);
return ans;
}
}
void upd(int p,int v){
val += v;
assert(val >= 0);
if (s == e) return;
else if (p > m){
if (!rinit) r = new node(m+1,e);
rinit = 1;
r->upd(p,v);
}else{
if (!linit) l = new node(s,m);
linit = 1;
l->upd(p,v);
}
}
}*root[maxn];
set <pi> st[maxn];
void dfs(int x){
int mx = -1;
aFOR(i, adj[x]){
dfs(i);
//if (mx == -1 || st[i].size() > st[mx].size()) mx = i;
}
if (mx != -1){
swap(root[x], root[mx]); st[x].swap(st[mx]);
}else root[x] = new node(1,K);
aFOR(i,adj[x]) if (i != mx){
aFOR(j,st[i]){
root[x]->upd(j.f, j.s);
st[x].insert(j);
}
}
if (D[x] == 0) return; // no fruit
//int v = root[x]->qry(1,D[x]) + W[x];
// update everything after D[x] by y -> max(y,v)
//int pt = root[x]->bs(v); // earliest point where f(pt) >= v
//int vpt = root[x]->qry(1, pt);
root[x]->upd(D[x], W[x]); // shift up first, then flatten
if (DEBUG){
cout << x << ' ' << '\n';
aFOR(i, st[x]) cout << i.f << ' ' << i.s << '\n';
cout << "\n\n";
}
st[x].insert(pi(D[x], W[x]));
int cur = W[x];
while (cur > 0){
auto it = st[x].upper_bound(pi(D[x], INF));
if (it == st[x].end()) break;
if (cur >= it->s){
cur -= it->s;
root[x]->upd(it->f, -it->s);
st[x].erase(it);
}else{
root[x]->upd(it->f, -cur);
pi tmp = *it;
st[x].erase(it);
st[x].insert(pi(tmp.f, tmp.s-cur));
cur = 0;
}
}
}
void solve(){
cin >> N >> M >> K;
FOR(i,2,N){
cin >> p; adj[p].pb(i);
}
int v,d,w;
FOR(i,1,M){
cin >> v >> d >> w;
D[v] = d; W[v] = w;
}
dfs(1);
cout << root[1]->val;
}
int32_t main(){
fast;
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
316 ms |
288788 KB |
Output is correct |
2 |
Runtime error |
977 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
71932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
281 ms |
145252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
79528 KB |
Output is correct |
2 |
Correct |
105 ms |
89856 KB |
Output is correct |
3 |
Correct |
88 ms |
89980 KB |
Output is correct |
4 |
Correct |
83 ms |
92620 KB |
Output is correct |
5 |
Correct |
50 ms |
82492 KB |
Output is correct |
6 |
Runtime error |
935 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |