답안 #952147

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952147 2024-03-23T06:42:29 Z GrindMachine Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
490 ms 122372 KB
#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) (int)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:
https://codeforces.com/blog/entry/88748?#comment-774017 (thread)
got the key idea, but didnt know how to implement in a clean way

implementation idea for tree case:
represent the subtree cost @u as a staircase function using a map
merging 2 staircase functions => add the deltas of the 2 maps
for a given u, merge staircase functions of children
insertion of u can be done by updating some deltas
note that function can become non-staircase after an update because at most 1 delta can become negative
in order to make it staircase again, keep merging that negative delta position with the previous position till the delta becomes >= 0

functional graph:
trees hanging down from some cycles
all values on a cycle must have the same value
fix this common value and find the cost
need to do it in a clever way
can be done with maps again (check implementation for more details)

*/

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<ll> adj[N];
vector<pll> a(N);
vector<ll> subsiz(N);
vector<bool> on_cyc(N);

void dfs1(ll u){
    subsiz[u] = 1;
    trav(v,adj[u]){
        if(on_cyc[v]) conts;
        dfs1(v);
        subsiz[u] += subsiz[v];
    }
}

map<ll,ll> mp[N];

void dfs2(ll u){
    pll best = {-1,-1};
    trav(v,adj[u]){
        if(on_cyc[v]) conts;
        pll px = {subsiz[v],v};
        amax(best,px);
    }

    ll heavy = best.ss;
    if(heavy == -1){
        auto [h,c] = a[u];
        mp[u][1] = 0;
        mp[u][h+1] = c;
        return;
    }

    dfs2(heavy);
    if(on_cyc[u]){
        mp[u] = mp[heavy];
    }
    else{
        swap(mp[u],mp[heavy]);
    }

    trav(v,adj[u]){
        if(on_cyc[v]) conts;
        if(v == heavy) conts;
        dfs2(v);
        for(auto [i,x] : mp[v]){
            mp[u][i] += x;
        }

        if(!on_cyc[u]){
            mp[v].clear();
        }
    }

    auto [h,c] = a[u];
    mp[u][1] += c;
    mp[u][h] -= c;
    mp[u][h+1] += c;

    auto it = mp[u].find(h);
    vector<ll> rem;

    ll val = mp[u][h], recent_h = h; 
    while(val < 0){
        it--;
        rem.pb(recent_h);
        recent_h = it->first;
        val += it->second;
    }

    mp[u][recent_h] = val;

    trav(x,rem){
        mp[u].erase(x);
    }
}

void solve(int test_case)
{
    ll n; cin >> n;
    vector<ll> par(n+5);

    rep1(i,n){
        ll p,h,c; cin >> p >> h >> c;
        adj[p].pb(i);
        par[i] = p;
        a[i] = {h,c};
    }

    vector<ll> outdeg(n+5);
    rep1(i,n) outdeg[i] = sz(adj[i]);

    queue<ll> q;
    rep1(i,n){
        if(outdeg[i] == 0){
            q.push(i);
        }
    }

    while(!q.empty()){
        ll u = q.front();
        q.pop();

        ll p = par[u];
        outdeg[p]--;
        if(outdeg[p] == 0){
            q.push(p);
        }
    }

    rep1(i,n){
        if(outdeg[i]){
            on_cyc[i] = 1;
        }
    }

    vector<bool> vis(n+5);
    ll ans = 0;

    rep1(i,n){
        if(vis[i] or !on_cyc[i]) conts;
        ll j = i;
        map<ll,ll> sum_mp;
        vector<ll> nodes;
        ll tot_sum = 0;

        while(!vis[j]){
            vis[j] = 1;
            nodes.pb(j);
            dfs1(j);
            dfs2(j);
            sum_mp[a[j].ff] += a[j].ss;
            tot_sum += a[j].ss;
            j = par[j];
        }

        map<ll,ll> cost_mp;
        cost_mp[1] += 0;
        ll cost_sum = 0;

        trav(u,nodes){
            cost_mp[a[u].ff] += 0;
            trav(v,adj[u]){
                if(on_cyc[v]) conts;
                for(auto [i,x] : mp[v]){
                    cost_mp[i] += x;
                }
            }
        }

        ll cost = inf2;

        for(auto [h,delta] : cost_mp){
            cost_sum += delta;
            ll curr_cost = cost_sum+tot_sum-sum_mp[h];
            amin(cost,curr_cost);
        }

        ans += cost;
    }

    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19032 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 10 ms 20264 KB Output is correct
6 Correct 8 ms 19288 KB Output is correct
7 Correct 8 ms 19292 KB Output is correct
8 Correct 14 ms 20388 KB Output is correct
9 Correct 11 ms 19292 KB Output is correct
10 Correct 7 ms 19292 KB Output is correct
11 Correct 9 ms 19288 KB Output is correct
12 Correct 10 ms 20572 KB Output is correct
13 Correct 10 ms 20572 KB Output is correct
14 Correct 11 ms 20096 KB Output is correct
15 Correct 9 ms 19868 KB Output is correct
16 Correct 11 ms 20316 KB Output is correct
17 Correct 8 ms 19384 KB Output is correct
18 Correct 8 ms 19292 KB Output is correct
19 Correct 11 ms 20524 KB Output is correct
20 Correct 8 ms 20008 KB Output is correct
21 Correct 7 ms 19800 KB Output is correct
22 Correct 10 ms 20824 KB Output is correct
23 Correct 7 ms 20060 KB Output is correct
24 Correct 11 ms 20568 KB Output is correct
25 Correct 7 ms 19804 KB Output is correct
26 Correct 12 ms 21852 KB Output is correct
27 Correct 11 ms 20668 KB Output is correct
28 Correct 9 ms 20828 KB Output is correct
29 Correct 13 ms 21244 KB Output is correct
30 Correct 10 ms 20824 KB Output is correct
31 Correct 10 ms 20828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19032 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 10 ms 20264 KB Output is correct
6 Correct 8 ms 19288 KB Output is correct
7 Correct 8 ms 19292 KB Output is correct
8 Correct 14 ms 20388 KB Output is correct
9 Correct 11 ms 19292 KB Output is correct
10 Correct 7 ms 19292 KB Output is correct
11 Correct 9 ms 19288 KB Output is correct
12 Correct 10 ms 20572 KB Output is correct
13 Correct 10 ms 20572 KB Output is correct
14 Correct 11 ms 20096 KB Output is correct
15 Correct 9 ms 19868 KB Output is correct
16 Correct 11 ms 20316 KB Output is correct
17 Correct 8 ms 19384 KB Output is correct
18 Correct 8 ms 19292 KB Output is correct
19 Correct 11 ms 20524 KB Output is correct
20 Correct 8 ms 20008 KB Output is correct
21 Correct 7 ms 19800 KB Output is correct
22 Correct 10 ms 20824 KB Output is correct
23 Correct 7 ms 20060 KB Output is correct
24 Correct 11 ms 20568 KB Output is correct
25 Correct 7 ms 19804 KB Output is correct
26 Correct 12 ms 21852 KB Output is correct
27 Correct 11 ms 20668 KB Output is correct
28 Correct 9 ms 20828 KB Output is correct
29 Correct 13 ms 21244 KB Output is correct
30 Correct 10 ms 20824 KB Output is correct
31 Correct 10 ms 20828 KB Output is correct
32 Correct 14 ms 20316 KB Output is correct
33 Correct 335 ms 64976 KB Output is correct
34 Correct 206 ms 26952 KB Output is correct
35 Correct 345 ms 64940 KB Output is correct
36 Correct 190 ms 26960 KB Output is correct
37 Correct 180 ms 26880 KB Output is correct
38 Correct 137 ms 27216 KB Output is correct
39 Correct 207 ms 72584 KB Output is correct
40 Correct 184 ms 72532 KB Output is correct
41 Correct 110 ms 72320 KB Output is correct
42 Correct 185 ms 51024 KB Output is correct
43 Correct 168 ms 50768 KB Output is correct
44 Correct 301 ms 62548 KB Output is correct
45 Correct 159 ms 26312 KB Output is correct
46 Correct 80 ms 26192 KB Output is correct
47 Correct 262 ms 75600 KB Output is correct
48 Correct 122 ms 48260 KB Output is correct
49 Correct 87 ms 48032 KB Output is correct
50 Correct 312 ms 86540 KB Output is correct
51 Correct 102 ms 49216 KB Output is correct
52 Correct 266 ms 76920 KB Output is correct
53 Correct 119 ms 48848 KB Output is correct
54 Correct 318 ms 122372 KB Output is correct
55 Correct 234 ms 84792 KB Output is correct
56 Correct 248 ms 99452 KB Output is correct
57 Correct 238 ms 107740 KB Output is correct
58 Correct 296 ms 88136 KB Output is correct
59 Correct 297 ms 88044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19032 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 10 ms 20264 KB Output is correct
6 Correct 8 ms 19288 KB Output is correct
7 Correct 8 ms 19292 KB Output is correct
8 Correct 14 ms 20388 KB Output is correct
9 Correct 11 ms 19292 KB Output is correct
10 Correct 7 ms 19292 KB Output is correct
11 Correct 9 ms 19288 KB Output is correct
12 Correct 10 ms 20572 KB Output is correct
13 Correct 10 ms 20572 KB Output is correct
14 Correct 11 ms 20096 KB Output is correct
15 Correct 9 ms 19868 KB Output is correct
16 Correct 11 ms 20316 KB Output is correct
17 Correct 8 ms 19384 KB Output is correct
18 Correct 8 ms 19292 KB Output is correct
19 Correct 11 ms 20524 KB Output is correct
20 Correct 8 ms 20008 KB Output is correct
21 Correct 7 ms 19800 KB Output is correct
22 Correct 10 ms 20824 KB Output is correct
23 Correct 7 ms 20060 KB Output is correct
24 Correct 11 ms 20568 KB Output is correct
25 Correct 7 ms 19804 KB Output is correct
26 Correct 12 ms 21852 KB Output is correct
27 Correct 11 ms 20668 KB Output is correct
28 Correct 9 ms 20828 KB Output is correct
29 Correct 13 ms 21244 KB Output is correct
30 Correct 10 ms 20824 KB Output is correct
31 Correct 10 ms 20828 KB Output is correct
32 Correct 14 ms 20316 KB Output is correct
33 Correct 335 ms 64976 KB Output is correct
34 Correct 206 ms 26952 KB Output is correct
35 Correct 345 ms 64940 KB Output is correct
36 Correct 190 ms 26960 KB Output is correct
37 Correct 180 ms 26880 KB Output is correct
38 Correct 137 ms 27216 KB Output is correct
39 Correct 207 ms 72584 KB Output is correct
40 Correct 184 ms 72532 KB Output is correct
41 Correct 110 ms 72320 KB Output is correct
42 Correct 185 ms 51024 KB Output is correct
43 Correct 168 ms 50768 KB Output is correct
44 Correct 301 ms 62548 KB Output is correct
45 Correct 159 ms 26312 KB Output is correct
46 Correct 80 ms 26192 KB Output is correct
47 Correct 262 ms 75600 KB Output is correct
48 Correct 122 ms 48260 KB Output is correct
49 Correct 87 ms 48032 KB Output is correct
50 Correct 312 ms 86540 KB Output is correct
51 Correct 102 ms 49216 KB Output is correct
52 Correct 266 ms 76920 KB Output is correct
53 Correct 119 ms 48848 KB Output is correct
54 Correct 318 ms 122372 KB Output is correct
55 Correct 234 ms 84792 KB Output is correct
56 Correct 248 ms 99452 KB Output is correct
57 Correct 238 ms 107740 KB Output is correct
58 Correct 296 ms 88136 KB Output is correct
59 Correct 297 ms 88044 KB Output is correct
60 Correct 5 ms 19160 KB Output is correct
61 Correct 5 ms 19036 KB Output is correct
62 Correct 5 ms 19036 KB Output is correct
63 Correct 5 ms 19032 KB Output is correct
64 Correct 309 ms 59220 KB Output is correct
65 Correct 194 ms 28240 KB Output is correct
66 Correct 282 ms 54352 KB Output is correct
67 Correct 190 ms 27456 KB Output is correct
68 Correct 168 ms 27476 KB Output is correct
69 Correct 131 ms 27220 KB Output is correct
70 Correct 471 ms 69260 KB Output is correct
71 Correct 208 ms 53968 KB Output is correct
72 Correct 487 ms 80180 KB Output is correct
73 Correct 198 ms 55236 KB Output is correct
74 Correct 490 ms 81212 KB Output is correct
75 Correct 266 ms 56340 KB Output is correct
76 Correct 222 ms 55056 KB Output is correct
77 Correct 384 ms 83392 KB Output is correct
78 Correct 166 ms 52372 KB Output is correct
79 Correct 403 ms 72672 KB Output is correct
80 Correct 218 ms 41040 KB Output is correct
81 Correct 165 ms 40720 KB Output is correct
82 Correct 298 ms 80260 KB Output is correct
83 Correct 111 ms 53600 KB Output is correct
84 Correct 328 ms 82484 KB Output is correct
85 Correct 334 ms 82736 KB Output is correct
86 Correct 311 ms 77092 KB Output is correct
87 Correct 334 ms 82648 KB Output is correct
88 Correct 335 ms 82488 KB Output is correct