Submission #414999

# Submission time Handle Problem Language Result Execution time Memory
414999 2021-05-31T11:59:07 Z 반딧불(#7587) Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
403 ms 69948 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n;
int par[200002], arr[200002]; ll cost[200002];
vector<int> link[200002];
ll ans;

bool hasChecked[200002];

void input();
void findRoot(int);
void treeDP();

int main(){
    input();
    for(int i=1; i<=n; i++){
        if(hasChecked[i]) continue;
        findRoot(i);
        treeDP();
    }
    printf("%lld", ans);
}

void input(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%d %d %lld", &par[i], &arr[i], &cost[i]);
        link[par[i]].push_back(i);
    }
}

vector<int> roots;
vector<int> rootLink;

void findRoot(int x){
    roots.clear();
    rootLink.clear();

    vector<int> stk;
    while(1){
        if(hasChecked[x]){
            auto it = find(stk.begin(), stk.end(), x);
            while(it != stk.end()){
                roots.push_back(*it);
                ++it;
            }
            break;
        }
        hasChecked[x] = 1;
        stk.push_back(x);
        x = par[x];
    }

    while(!stk.empty()){
        hasChecked[stk.back()] = 0;
        stk.pop_back();
    }
    for(auto x: roots){
        stk.push_back(x);
        hasChecked[x] = 1;
    }

    for(auto x: roots){
        for(auto y: link[x]){
            if(!hasChecked[y]) rootLink.push_back(y);
        }
    }

    while(!stk.empty()){
        int tmp = stk.back(); stk.pop_back();

        for(auto y: link[tmp]){
            if(hasChecked[y]) continue;
            hasChecked[y] = 1;
            stk.push_back(y);
        }
    }
}

map<int, ll> DP[200002];
ll costSum;

void treeDP(int);
void treeDP(){
    costSum = 0;
    for(auto y: rootLink) treeDP(y);

    vector<pair<int, ll> > vec;
    map<int, ll> tmpMap;
    map<int, ll> costMap;
    for(auto y: rootLink){
        for(auto p: DP[y]){
            tmpMap[p.first-1] -= p.second;
        }
    }
    for(auto p: tmpMap) vec.push_back(p);

    vec.push_back(make_pair(1e9 + 1, costSum));
    sort(vec.begin(), vec.end());

    for(int i=(int)vec.size()-2; i>=0; i--){
        vec[i].second = vec[i+1].second + vec[i].second;
    }

    ll changeCost = 0;
    for(auto y: roots){
        changeCost += cost[y];
        costMap[arr[y]] += cost[y];
    }

    ll hereAns = changeCost + vec[0].second;
    for(auto p: costMap){
        auto it = lower_bound(vec.begin(), vec.end(), make_pair(p.first, LLONG_MIN));
        hereAns = min(hereAns, changeCost - p.second + it->second);
    }
    ans += hereAns;
}

void mergeDP(map<int, ll> &dpA, map<int, ll> &dpB){
    for(auto p: dpB){
        dpA[p.first] += p.second;
    }

    dpB.clear();
}

void treeDP(int x){
    costSum += cost[x];
    for(auto y: link[x]){
        treeDP(y);

        if((int)DP[x].size() < (int)DP[y].size()) DP[x].swap(DP[y]);
        mergeDP(DP[x], DP[y]);
    }

    if(DP[x].empty()){
        DP[x].insert(make_pair(arr[x]+1, cost[x]));
        return;
    }
    DP[x][arr[x]+1] += cost[x];

    auto it = DP[x].upper_bound(arr[x]);
    ll downSum = cost[x];

    while(it != DP[x].begin()){
        if(downSum <= prev(it)->second){
            if(downSum == prev(it)->second){
                DP[x].erase(prev(it));
                break;
            }
            ll w = prev(it)->second - downSum;
            prev(it)->second = w;
            break;
        }
        else{
            downSum -= prev(it)->second;
            DP[x].erase(prev(it));
        }
    }
}

Compilation message

worst_reporter2.cpp: In function 'void input()':
worst_reporter2.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
worst_reporter2.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         scanf("%d %d %lld", &par[i], &arr[i], &cost[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14344 KB Output is correct
3 Correct 8 ms 14284 KB Output is correct
4 Correct 9 ms 14388 KB Output is correct
5 Correct 15 ms 15104 KB Output is correct
6 Correct 13 ms 14540 KB Output is correct
7 Correct 12 ms 14540 KB Output is correct
8 Correct 14 ms 15148 KB Output is correct
9 Correct 12 ms 14540 KB Output is correct
10 Correct 12 ms 14580 KB Output is correct
11 Correct 11 ms 14548 KB Output is correct
12 Correct 12 ms 15308 KB Output is correct
13 Correct 11 ms 15180 KB Output is correct
14 Correct 13 ms 14956 KB Output is correct
15 Correct 15 ms 14932 KB Output is correct
16 Correct 15 ms 15036 KB Output is correct
17 Correct 15 ms 14592 KB Output is correct
18 Correct 11 ms 14540 KB Output is correct
19 Correct 17 ms 15340 KB Output is correct
20 Correct 12 ms 14796 KB Output is correct
21 Correct 11 ms 14760 KB Output is correct
22 Correct 15 ms 15308 KB Output is correct
23 Correct 12 ms 14840 KB Output is correct
24 Correct 14 ms 15308 KB Output is correct
25 Correct 12 ms 14828 KB Output is correct
26 Correct 12 ms 15180 KB Output is correct
27 Correct 13 ms 15392 KB Output is correct
28 Correct 13 ms 15424 KB Output is correct
29 Correct 13 ms 15692 KB Output is correct
30 Correct 14 ms 15820 KB Output is correct
31 Correct 14 ms 15692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14344 KB Output is correct
3 Correct 8 ms 14284 KB Output is correct
4 Correct 9 ms 14388 KB Output is correct
5 Correct 15 ms 15104 KB Output is correct
6 Correct 13 ms 14540 KB Output is correct
7 Correct 12 ms 14540 KB Output is correct
8 Correct 14 ms 15148 KB Output is correct
9 Correct 12 ms 14540 KB Output is correct
10 Correct 12 ms 14580 KB Output is correct
11 Correct 11 ms 14548 KB Output is correct
12 Correct 12 ms 15308 KB Output is correct
13 Correct 11 ms 15180 KB Output is correct
14 Correct 13 ms 14956 KB Output is correct
15 Correct 15 ms 14932 KB Output is correct
16 Correct 15 ms 15036 KB Output is correct
17 Correct 15 ms 14592 KB Output is correct
18 Correct 11 ms 14540 KB Output is correct
19 Correct 17 ms 15340 KB Output is correct
20 Correct 12 ms 14796 KB Output is correct
21 Correct 11 ms 14760 KB Output is correct
22 Correct 15 ms 15308 KB Output is correct
23 Correct 12 ms 14840 KB Output is correct
24 Correct 14 ms 15308 KB Output is correct
25 Correct 12 ms 14828 KB Output is correct
26 Correct 12 ms 15180 KB Output is correct
27 Correct 13 ms 15392 KB Output is correct
28 Correct 13 ms 15424 KB Output is correct
29 Correct 13 ms 15692 KB Output is correct
30 Correct 14 ms 15820 KB Output is correct
31 Correct 14 ms 15692 KB Output is correct
32 Correct 16 ms 15076 KB Output is correct
33 Correct 384 ms 44596 KB Output is correct
34 Correct 272 ms 21452 KB Output is correct
35 Correct 363 ms 44668 KB Output is correct
36 Correct 297 ms 21316 KB Output is correct
37 Correct 202 ms 20876 KB Output is correct
38 Correct 176 ms 20920 KB Output is correct
39 Correct 184 ms 49080 KB Output is correct
40 Correct 172 ms 49048 KB Output is correct
41 Correct 139 ms 48900 KB Output is correct
42 Correct 185 ms 36760 KB Output is correct
43 Correct 176 ms 36548 KB Output is correct
44 Correct 343 ms 43320 KB Output is correct
45 Correct 210 ms 21264 KB Output is correct
46 Correct 108 ms 20804 KB Output is correct
47 Correct 325 ms 49584 KB Output is correct
48 Correct 141 ms 33484 KB Output is correct
49 Correct 124 ms 33236 KB Output is correct
50 Correct 304 ms 48548 KB Output is correct
51 Correct 121 ms 31788 KB Output is correct
52 Correct 289 ms 50188 KB Output is correct
53 Correct 150 ms 33980 KB Output is correct
54 Correct 133 ms 48988 KB Output is correct
55 Correct 270 ms 56108 KB Output is correct
56 Correct 256 ms 65080 KB Output is correct
57 Correct 249 ms 69948 KB Output is correct
58 Correct 256 ms 65644 KB Output is correct
59 Correct 254 ms 65688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14344 KB Output is correct
3 Correct 8 ms 14284 KB Output is correct
4 Correct 9 ms 14388 KB Output is correct
5 Correct 15 ms 15104 KB Output is correct
6 Correct 13 ms 14540 KB Output is correct
7 Correct 12 ms 14540 KB Output is correct
8 Correct 14 ms 15148 KB Output is correct
9 Correct 12 ms 14540 KB Output is correct
10 Correct 12 ms 14580 KB Output is correct
11 Correct 11 ms 14548 KB Output is correct
12 Correct 12 ms 15308 KB Output is correct
13 Correct 11 ms 15180 KB Output is correct
14 Correct 13 ms 14956 KB Output is correct
15 Correct 15 ms 14932 KB Output is correct
16 Correct 15 ms 15036 KB Output is correct
17 Correct 15 ms 14592 KB Output is correct
18 Correct 11 ms 14540 KB Output is correct
19 Correct 17 ms 15340 KB Output is correct
20 Correct 12 ms 14796 KB Output is correct
21 Correct 11 ms 14760 KB Output is correct
22 Correct 15 ms 15308 KB Output is correct
23 Correct 12 ms 14840 KB Output is correct
24 Correct 14 ms 15308 KB Output is correct
25 Correct 12 ms 14828 KB Output is correct
26 Correct 12 ms 15180 KB Output is correct
27 Correct 13 ms 15392 KB Output is correct
28 Correct 13 ms 15424 KB Output is correct
29 Correct 13 ms 15692 KB Output is correct
30 Correct 14 ms 15820 KB Output is correct
31 Correct 14 ms 15692 KB Output is correct
32 Correct 16 ms 15076 KB Output is correct
33 Correct 384 ms 44596 KB Output is correct
34 Correct 272 ms 21452 KB Output is correct
35 Correct 363 ms 44668 KB Output is correct
36 Correct 297 ms 21316 KB Output is correct
37 Correct 202 ms 20876 KB Output is correct
38 Correct 176 ms 20920 KB Output is correct
39 Correct 184 ms 49080 KB Output is correct
40 Correct 172 ms 49048 KB Output is correct
41 Correct 139 ms 48900 KB Output is correct
42 Correct 185 ms 36760 KB Output is correct
43 Correct 176 ms 36548 KB Output is correct
44 Correct 343 ms 43320 KB Output is correct
45 Correct 210 ms 21264 KB Output is correct
46 Correct 108 ms 20804 KB Output is correct
47 Correct 325 ms 49584 KB Output is correct
48 Correct 141 ms 33484 KB Output is correct
49 Correct 124 ms 33236 KB Output is correct
50 Correct 304 ms 48548 KB Output is correct
51 Correct 121 ms 31788 KB Output is correct
52 Correct 289 ms 50188 KB Output is correct
53 Correct 150 ms 33980 KB Output is correct
54 Correct 133 ms 48988 KB Output is correct
55 Correct 270 ms 56108 KB Output is correct
56 Correct 256 ms 65080 KB Output is correct
57 Correct 249 ms 69948 KB Output is correct
58 Correct 256 ms 65644 KB Output is correct
59 Correct 254 ms 65688 KB Output is correct
60 Correct 8 ms 14284 KB Output is correct
61 Correct 9 ms 14412 KB Output is correct
62 Correct 9 ms 14412 KB Output is correct
63 Correct 8 ms 14320 KB Output is correct
64 Correct 403 ms 45088 KB Output is correct
65 Correct 296 ms 27732 KB Output is correct
66 Correct 398 ms 42768 KB Output is correct
67 Correct 282 ms 28276 KB Output is correct
68 Correct 242 ms 26988 KB Output is correct
69 Correct 206 ms 26948 KB Output is correct
70 Correct 291 ms 38272 KB Output is correct
71 Correct 165 ms 30256 KB Output is correct
72 Correct 341 ms 42364 KB Output is correct
73 Correct 188 ms 31504 KB Output is correct
74 Correct 310 ms 47020 KB Output is correct
75 Correct 217 ms 33228 KB Output is correct
76 Correct 191 ms 33036 KB Output is correct
77 Correct 264 ms 47456 KB Output is correct
78 Correct 157 ms 33512 KB Output is correct
79 Correct 362 ms 45060 KB Output is correct
80 Correct 239 ms 28736 KB Output is correct
81 Correct 177 ms 28820 KB Output is correct
82 Correct 223 ms 42664 KB Output is correct
83 Correct 138 ms 29032 KB Output is correct
84 Correct 367 ms 54100 KB Output is correct
85 Correct 329 ms 53948 KB Output is correct
86 Correct 343 ms 50796 KB Output is correct
87 Correct 346 ms 53996 KB Output is correct
88 Correct 352 ms 54004 KB Output is correct