Submission #966453

# Submission time Handle Problem Language Result Execution time Memory
966453 2024-04-19T22:52:22 Z shadow_sami Magic Tree (CEOI19_magictree) C++17
100 / 100
102 ms 64092 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll,ll> pi;
typedef map<ll,ll> mi;
typedef long double ld;
typedef vector<ld> vd;
typedef vector<vector<ld>> vvd;
typedef pair<ld,ld> pd;
#define ff first
#define ss second
#define srt(a) sort(a.begin(),a.end());
#define fip(k, n) for (ll i = k; i < n; i++)
#define fjp(k, n) for (ll j = k; j < n; j++)
#define fin(k, n) for (ll i = k; i >= n; i--)
#define fjn(k, n) for (ll j = k; j >= n; j--)
#define fp(k, n, m) for (ll k = n; k < m; k++)
#define fn(k, n, m) for (ll k = n; k >= m; k--)
#define ordered_set tree<pi, null_type,less< pi >, rb_tree_tag,tree_order_statistics_node_update>
#define totalOne(n) __builtin_popcount(n)
#define backZero(n) __builtin_ctzll(n)
#define frontZero(n) __builtin_clzll(n)
#define fx(k) for ( auto x : k )
#define test ll t;cin >> t;while (t--)
#define nli "\n"
 
// ==========================(debug)============================================================================================== //
 
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _printn(x); cerr << nli;
#else
#define debug(x)
#endif
 
void _printn(ll x){ cerr<<x<<" "; }
void _printn(int x){ cerr<<x<<" "; }
void _printn(ld x){ cerr<<x<<" "; }
void _printn(double x){ cerr<<x<<" "; }
void _printn(string x){ cerr<<x<<" "; }
void _printn(char x){ cerr<<x<<" "; }
void _printn(bool x){ cerr<<x<<" "; }
template<class T,class V>void _printn(pair<T,V> vv);
template<class T> void _printn(vector<T> vv);
template<class T> void _printn(set<T> vv);
template<class T,class V> void _printn(map<T,V> vv);
template<class T> void _printn(multiset<T> vv);
template<class T,class V>void _printn(pair<T,V> vv){ cerr<<"( ";_printn(vv.ff);cerr<<",";_printn(vv.ss);cerr<<")";}
template<class T> void _printn(vector<T> vv){ cerr<<"[ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"]"; };
template<class T> void _printn(set<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T> void _printn(multiset<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T,class V> void _printn(map<T,V> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
 
// ==========================(debug)============================================================================================== //
 
ll n,m,tp,tp2,res,cnt,sum,tptp,ans;
const ll mx = 5e5+5;
const ll mod = 1e9+7;
 
// ==========================(MOD)=============================================================================================== //
 
ll mod_add(ll aa,ll bb){ return ((aa%mod)+(bb%mod))%mod; }
ll mod_minus(ll aa,ll bb){ return (((aa%mod)-(bb%mod))+10*mod)%mod; }
ll mod_mul(ll aa,ll bb){ return ((aa%mod)*(bb%mod))%mod; }
ll mod_power(ll aa,ll bb){ aa%=mod; ll empowered = 1; bb%=mod-1; while(bb > 0){ if(bb & 1) empowered = mod_mul(empowered,aa); bb = bb >> 1; aa = mod_mul(aa,aa); } return empowered; }
ll mod_divi(ll aa,ll bb){ aa=mod_mul(aa,mod_power(bb,mod-2)); return aa; }
 
// ==========================(MOD)=============================================================================================== //
 
bool f = false;
vi adj[mx];
ll d[mx];
ll w[mx];
ll sr;
multiset<pi> st[mx];
pi p,pt;
 
void dfs(ll u){
    fx(adj[u]){
        dfs(x);            
    }    
    fx(adj[u]){
        if(st[u].size() >= st[x].size()){
            for (auto y : st[x])
                st[u].insert(y);
        }      
        else{
            for (auto y : st[u])
                st[x].insert(y);
            st[u].swap(st[x]);
        }
        st[x].clear();
    }    
    
    if(d[u]<=0)
        return;
    // debug(u);
    // debug(st[u]);
    ll rs = 0;
    st[u].insert({d[u],w[u]});
    p = {d[u]+1,0};
    while(1){
        if(st[u].lower_bound(p)==st[u].end())
            break;
        pt = *st[u].lower_bound(p);
        if(rs+pt.ss>=w[u]){
            st[u].erase(st[u].find(pt));
            st[u].insert({pt.ff,rs+pt.ss-w[u]});
            // st[u].erase(st[u].find({d[u],w[u]}));
            break;
        }
        st[u].erase(st[u].find(pt));
        rs += pt.ss;
    }

    // debug(st[u]);
    return;
}
 
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    // #ifndef ONLINE_JUDGE
    //     freopen("input1.txt", "r", stdin);
    //     freopen("output1.txt", "w", stdout);
    //     freopen("error1.txt", "w", stderr);
    // #endif // ONLINE_JUDGE
 
        cin>>n>>m>>tp;
        fip(0,n+2){
        	adj[i].clear();
        	d[i] = -1;
        }
        fip(2,n+1){
        	cin>>sr;
        	adj[sr].push_back(i);
        }
        fip(0,m){
        	cin>>res>>tp2>>tptp;
        	d[res] = tp2;
        	w[res] = tptp;
        }        
        dfs(1);
        ans = 0;        
        fx(st[1])
            ans += x.ss;
        cout<<ans<<nli;
        
    cerr << "Time elapsed: " << setprecision(6) << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39260 KB Output is correct
2 Correct 10 ms 39260 KB Output is correct
3 Correct 9 ms 39260 KB Output is correct
4 Correct 9 ms 39260 KB Output is correct
5 Correct 8 ms 39348 KB Output is correct
6 Correct 8 ms 39260 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 9 ms 39260 KB Output is correct
9 Correct 9 ms 39260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 43344 KB Output is correct
2 Correct 37 ms 49488 KB Output is correct
3 Correct 95 ms 47956 KB Output is correct
4 Correct 60 ms 45784 KB Output is correct
5 Correct 100 ms 47532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39512 KB Output is correct
2 Correct 10 ms 39572 KB Output is correct
3 Correct 9 ms 39612 KB Output is correct
4 Correct 60 ms 56608 KB Output is correct
5 Correct 68 ms 64092 KB Output is correct
6 Correct 94 ms 57796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 46672 KB Output is correct
2 Correct 101 ms 48524 KB Output is correct
3 Correct 76 ms 52048 KB Output is correct
4 Correct 64 ms 45776 KB Output is correct
5 Correct 68 ms 59368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39260 KB Output is correct
2 Correct 10 ms 39260 KB Output is correct
3 Correct 9 ms 39260 KB Output is correct
4 Correct 9 ms 39260 KB Output is correct
5 Correct 8 ms 39348 KB Output is correct
6 Correct 8 ms 39260 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 9 ms 39260 KB Output is correct
9 Correct 9 ms 39260 KB Output is correct
10 Correct 91 ms 46676 KB Output is correct
11 Correct 96 ms 47184 KB Output is correct
12 Correct 70 ms 49492 KB Output is correct
13 Correct 48 ms 45692 KB Output is correct
14 Correct 74 ms 56780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 39768 KB Output is correct
2 Correct 26 ms 41304 KB Output is correct
3 Correct 27 ms 41308 KB Output is correct
4 Correct 26 ms 41308 KB Output is correct
5 Correct 15 ms 40412 KB Output is correct
6 Correct 26 ms 43960 KB Output is correct
7 Correct 27 ms 47524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39260 KB Output is correct
2 Correct 10 ms 39260 KB Output is correct
3 Correct 9 ms 39260 KB Output is correct
4 Correct 9 ms 39260 KB Output is correct
5 Correct 8 ms 39348 KB Output is correct
6 Correct 8 ms 39260 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 9 ms 39260 KB Output is correct
9 Correct 9 ms 39260 KB Output is correct
10 Correct 9 ms 39512 KB Output is correct
11 Correct 10 ms 39572 KB Output is correct
12 Correct 9 ms 39612 KB Output is correct
13 Correct 60 ms 56608 KB Output is correct
14 Correct 68 ms 64092 KB Output is correct
15 Correct 94 ms 57796 KB Output is correct
16 Correct 91 ms 46676 KB Output is correct
17 Correct 96 ms 47184 KB Output is correct
18 Correct 70 ms 49492 KB Output is correct
19 Correct 48 ms 45692 KB Output is correct
20 Correct 74 ms 56780 KB Output is correct
21 Correct 26 ms 41552 KB Output is correct
22 Correct 66 ms 45908 KB Output is correct
23 Correct 65 ms 45664 KB Output is correct
24 Correct 102 ms 49500 KB Output is correct
25 Correct 61 ms 47064 KB Output is correct
26 Correct 95 ms 51884 KB Output is correct
27 Correct 94 ms 52044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39260 KB Output is correct
2 Correct 10 ms 39260 KB Output is correct
3 Correct 9 ms 39260 KB Output is correct
4 Correct 9 ms 39260 KB Output is correct
5 Correct 8 ms 39348 KB Output is correct
6 Correct 8 ms 39260 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 9 ms 39260 KB Output is correct
9 Correct 9 ms 39260 KB Output is correct
10 Correct 47 ms 43344 KB Output is correct
11 Correct 37 ms 49488 KB Output is correct
12 Correct 95 ms 47956 KB Output is correct
13 Correct 60 ms 45784 KB Output is correct
14 Correct 100 ms 47532 KB Output is correct
15 Correct 9 ms 39512 KB Output is correct
16 Correct 10 ms 39572 KB Output is correct
17 Correct 9 ms 39612 KB Output is correct
18 Correct 60 ms 56608 KB Output is correct
19 Correct 68 ms 64092 KB Output is correct
20 Correct 94 ms 57796 KB Output is correct
21 Correct 97 ms 46672 KB Output is correct
22 Correct 101 ms 48524 KB Output is correct
23 Correct 76 ms 52048 KB Output is correct
24 Correct 64 ms 45776 KB Output is correct
25 Correct 68 ms 59368 KB Output is correct
26 Correct 91 ms 46676 KB Output is correct
27 Correct 96 ms 47184 KB Output is correct
28 Correct 70 ms 49492 KB Output is correct
29 Correct 48 ms 45692 KB Output is correct
30 Correct 74 ms 56780 KB Output is correct
31 Correct 12 ms 39768 KB Output is correct
32 Correct 26 ms 41304 KB Output is correct
33 Correct 27 ms 41308 KB Output is correct
34 Correct 26 ms 41308 KB Output is correct
35 Correct 15 ms 40412 KB Output is correct
36 Correct 26 ms 43960 KB Output is correct
37 Correct 27 ms 47524 KB Output is correct
38 Correct 26 ms 41552 KB Output is correct
39 Correct 66 ms 45908 KB Output is correct
40 Correct 65 ms 45664 KB Output is correct
41 Correct 102 ms 49500 KB Output is correct
42 Correct 61 ms 47064 KB Output is correct
43 Correct 95 ms 51884 KB Output is correct
44 Correct 94 ms 52044 KB Output is correct
45 Correct 24 ms 41052 KB Output is correct
46 Correct 68 ms 45904 KB Output is correct
47 Correct 74 ms 45344 KB Output is correct
48 Correct 94 ms 48904 KB Output is correct
49 Correct 60 ms 47820 KB Output is correct
50 Correct 92 ms 51536 KB Output is correct
51 Correct 101 ms 51916 KB Output is correct