This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Om Namah Shivaya
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
edi
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
template<typename T>
struct fenwick {
int siz;
map<int,T> tree;
fenwick(){
}
int lsb(int x) {
return x & -x;
}
void pupd(int i, T v) {
while (i < N) {
tree[i] += v;
i += lsb(i);
}
}
T sum(int i) {
T res = 0;
while (i) {
if(tree.find(i) != tree.end()){
res += tree[i];
}
i -= lsb(i);
}
return res;
}
T query(int l, int r) {
if (l > r) return 0;
T res = sum(r) - sum(l - 1);
return res;
}
};
vector<ll> adj[N];
vector<pll> here(N,{-1,-1});
fenwick<ll> fenw[N];
set<ll> days[N];
vector<ll> subsiz(N);
void dfs1(ll u){
subsiz[u] = (here[u].ff != -1);
trav(v,adj[u]){
dfs1(v);
subsiz[u] += subsiz[v];
}
}
void dfs2(ll u){
ll mx = -1, largest = -1;
trav(v,adj[u]){
if(subsiz[v] > mx){
mx = subsiz[v];
largest = v;
}
}
if(largest != -1){
dfs2(largest);
swap(days[u],days[largest]);
swap(fenw[u],fenw[largest]);
}
auto [day,cost] = here[u];
if(day != -1){
days[u].insert(day);
if(largest != -1){
cost += fenw[u].query(1,day);
}
}
trav(v,adj[u]){
if(v == largest) conts;
dfs2(v);
if(day != -1){
cost += fenw[v].query(1,day);
}
trav(d,days[v]){
ll val = fenw[v].query(d,d);
fenw[u].pupd(d,val);
days[u].insert(d);
}
days[v].clear();
}
if(day != -1){
ll val = fenw[u].query(1,day);
if(cost > val){
fenw[u].pupd(day,cost-val);
auto it = days[u].find(day);
it++;
vector<ll> toerase;
for(; it != days[u].end(); ++it){
ll p = *it;
ll add = fenw[u].query(p,p);
val += add;
if(val > cost){
fenw[u].pupd(p,-add+val-cost);
break;
}
else{
fenw[u].pupd(p,-add);
toerase.pb(p);
}
}
trav(p,toerase){
days[u].erase(p);
}
}
}
}
void solve(int test_case)
{
ll n,m,k; cin >> n >> m >> k;
for(int i = 2; i <= n; ++i){
ll p; cin >> p;
adj[p].pb(i);
}
vector<ll> days;
rep1(i,m){
ll u,d,w; cin >> u >> d >> w;
days.pb(d);
here[u] = {d,w};
}
sort(all(days));
days.resize(unique(all(days))-days.begin());
rep1(i,n){
if(here[i].ff != -1){
here[i].ff = lower_bound(all(days),here[i].ff)-days.begin()+1;
}
}
dfs1(1);
dfs2(1);
ll ans = fenw[1].query(1,N-1);
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |