답안 #810673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
810673 2023-08-06T14:00:20 Z GrindMachine Magic Tree (CEOI19_magictree) C++17
100 / 100
847 ms 136068 KB
// 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15164 KB Output is correct
2 Correct 7 ms 15224 KB Output is correct
3 Correct 7 ms 15188 KB Output is correct
4 Correct 7 ms 15148 KB Output is correct
5 Correct 8 ms 15188 KB Output is correct
6 Correct 6 ms 15188 KB Output is correct
7 Correct 7 ms 15216 KB Output is correct
8 Correct 5 ms 15188 KB Output is correct
9 Correct 6 ms 15216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 55532 KB Output is correct
2 Correct 96 ms 47304 KB Output is correct
3 Correct 765 ms 136068 KB Output is correct
4 Correct 285 ms 77000 KB Output is correct
5 Correct 393 ms 81000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15572 KB Output is correct
2 Correct 8 ms 15472 KB Output is correct
3 Correct 8 ms 15608 KB Output is correct
4 Correct 549 ms 56264 KB Output is correct
5 Correct 237 ms 59244 KB Output is correct
6 Correct 847 ms 56244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 68508 KB Output is correct
2 Correct 168 ms 69208 KB Output is correct
3 Correct 109 ms 51636 KB Output is correct
4 Correct 180 ms 111436 KB Output is correct
5 Correct 86 ms 52544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15164 KB Output is correct
2 Correct 7 ms 15224 KB Output is correct
3 Correct 7 ms 15188 KB Output is correct
4 Correct 7 ms 15148 KB Output is correct
5 Correct 8 ms 15188 KB Output is correct
6 Correct 6 ms 15188 KB Output is correct
7 Correct 7 ms 15216 KB Output is correct
8 Correct 5 ms 15188 KB Output is correct
9 Correct 6 ms 15216 KB Output is correct
10 Correct 243 ms 69808 KB Output is correct
11 Correct 214 ms 68804 KB Output is correct
12 Correct 121 ms 49344 KB Output is correct
13 Correct 147 ms 102248 KB Output is correct
14 Correct 96 ms 51948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 16628 KB Output is correct
2 Correct 56 ms 18472 KB Output is correct
3 Correct 44 ms 18552 KB Output is correct
4 Correct 41 ms 18812 KB Output is correct
5 Correct 15 ms 16976 KB Output is correct
6 Correct 42 ms 24216 KB Output is correct
7 Correct 38 ms 31104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15164 KB Output is correct
2 Correct 7 ms 15224 KB Output is correct
3 Correct 7 ms 15188 KB Output is correct
4 Correct 7 ms 15148 KB Output is correct
5 Correct 8 ms 15188 KB Output is correct
6 Correct 6 ms 15188 KB Output is correct
7 Correct 7 ms 15216 KB Output is correct
8 Correct 5 ms 15188 KB Output is correct
9 Correct 6 ms 15216 KB Output is correct
10 Correct 7 ms 15572 KB Output is correct
11 Correct 8 ms 15472 KB Output is correct
12 Correct 8 ms 15608 KB Output is correct
13 Correct 549 ms 56264 KB Output is correct
14 Correct 237 ms 59244 KB Output is correct
15 Correct 847 ms 56244 KB Output is correct
16 Correct 243 ms 69808 KB Output is correct
17 Correct 214 ms 68804 KB Output is correct
18 Correct 121 ms 49344 KB Output is correct
19 Correct 147 ms 102248 KB Output is correct
20 Correct 96 ms 51948 KB Output is correct
21 Correct 86 ms 30040 KB Output is correct
22 Correct 314 ms 59876 KB Output is correct
23 Correct 396 ms 75768 KB Output is correct
24 Correct 724 ms 121576 KB Output is correct
25 Correct 289 ms 76388 KB Output is correct
26 Correct 514 ms 65872 KB Output is correct
27 Correct 700 ms 50136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15164 KB Output is correct
2 Correct 7 ms 15224 KB Output is correct
3 Correct 7 ms 15188 KB Output is correct
4 Correct 7 ms 15148 KB Output is correct
5 Correct 8 ms 15188 KB Output is correct
6 Correct 6 ms 15188 KB Output is correct
7 Correct 7 ms 15216 KB Output is correct
8 Correct 5 ms 15188 KB Output is correct
9 Correct 6 ms 15216 KB Output is correct
10 Correct 271 ms 55532 KB Output is correct
11 Correct 96 ms 47304 KB Output is correct
12 Correct 765 ms 136068 KB Output is correct
13 Correct 285 ms 77000 KB Output is correct
14 Correct 393 ms 81000 KB Output is correct
15 Correct 7 ms 15572 KB Output is correct
16 Correct 8 ms 15472 KB Output is correct
17 Correct 8 ms 15608 KB Output is correct
18 Correct 549 ms 56264 KB Output is correct
19 Correct 237 ms 59244 KB Output is correct
20 Correct 847 ms 56244 KB Output is correct
21 Correct 190 ms 68508 KB Output is correct
22 Correct 168 ms 69208 KB Output is correct
23 Correct 109 ms 51636 KB Output is correct
24 Correct 180 ms 111436 KB Output is correct
25 Correct 86 ms 52544 KB Output is correct
26 Correct 243 ms 69808 KB Output is correct
27 Correct 214 ms 68804 KB Output is correct
28 Correct 121 ms 49344 KB Output is correct
29 Correct 147 ms 102248 KB Output is correct
30 Correct 96 ms 51948 KB Output is correct
31 Correct 16 ms 16628 KB Output is correct
32 Correct 56 ms 18472 KB Output is correct
33 Correct 44 ms 18552 KB Output is correct
34 Correct 41 ms 18812 KB Output is correct
35 Correct 15 ms 16976 KB Output is correct
36 Correct 42 ms 24216 KB Output is correct
37 Correct 38 ms 31104 KB Output is correct
38 Correct 86 ms 30040 KB Output is correct
39 Correct 314 ms 59876 KB Output is correct
40 Correct 396 ms 75768 KB Output is correct
41 Correct 724 ms 121576 KB Output is correct
42 Correct 289 ms 76388 KB Output is correct
43 Correct 514 ms 65872 KB Output is correct
44 Correct 700 ms 50136 KB Output is correct
45 Correct 92 ms 30536 KB Output is correct
46 Correct 326 ms 61136 KB Output is correct
47 Correct 421 ms 78100 KB Output is correct
48 Correct 739 ms 127916 KB Output is correct
49 Correct 272 ms 77068 KB Output is correct
50 Correct 536 ms 66888 KB Output is correct
51 Correct 663 ms 51128 KB Output is correct