Submission #1094432

# Submission time Handle Problem Language Result Execution time Memory
1094432 2024-09-29T15:30:03 Z vladilius Worst Reporter 4 (JOI21_worst_reporter4) C++17
79 / 100
438 ms 162412 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n; cin>>n;
    vector<int> a(n + 1), h(n + 1), c(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>a[i]>>h[i]>>c[i];
    }
    
    vector<pii> all;
    for (int i = 1; i <= n; i++) all.pb({h[i], i});
    sort(all.begin(), all.end());
    int i = 0, cc = 0;
    while (i < all.size()){
        int j = i;
        while (j < all.size() && all[i] == all[j]){
            j++;
        }
        cc++;
        while (i < j){
            h[all[i].ss] = cc;
            i++;
        }
    }
    
    vector<int> g1[n + 1];
    for (int i = 1; i <= n; i++){
        if (a[i] != i) g1[a[i]].pb(i);
    }
    vector<bool> used(n + 1), seen(n + 1);
    vector<int> dd(n + 1), d[n + 1], st;
    function<void(int)> dfs = [&](int v){
        seen[v] = used[v] = 1;
        st.pb(v);
        for (int i: g1[v]){
            if (used[i]){
                if (seen[i]){
                    for (int j = (int) st.size() - 1; j >= 0; j--){
                        dd[st[j]] = i;
                        if (st[j] == i) break;
                    }
                }
                continue;
            }
            dfs(i);
        }
        seen[v] = 0; st.pop_back();
    };
    
    for (int i = 1; i <= n; i++){
        if (!used[i]){
            dfs(i);
        }
    }
    
    vector<int> g[n + 1];
    for (int i = 1; i <= n; i++){
        if (!dd[i]) dd[i] = i;
        d[dd[i]].pb(i);
    }
    fill(used.begin(), used.end(), 0);
    for (int i = 1; i <= n; i++){
        int x = dd[a[i]], y = dd[i];
        if (x != y){
            g[x].pb(y);
            used[y] = 1;
        }
    }
    
    vector<ll> sum(cc + 1);
    map<int, ll> mp[n + 1];
    
    auto add = [&](int v, int l, int r, ll x){
        mp[v][l] += x;
        if (r < cc) mp[v][r + 1] -= x;
    };
    
    function<void(int)> solve = [&](int v){
        for (int i: g[v]) solve(i);
        
        for (int i: g[v]){
            if (mp[v].size() < mp[i].size()) swap(mp[v], mp[i]);
            for (auto [p, x]: mp[i]){
                mp[v][p] += x;
            }
        }
        
        ll S = 0;
        for (int i: d[v]) S += c[i];
        
        map<int, ll> t;
        for (int i: d[v]){
            t[h[i]] += c[i];
        }
    
        add(v, 1, cc, S);
        
        for (auto [i, S]: t){
            auto it = mp[v].upper_bound(i);
            if (it == mp[v].begin()) continue;
            it--;
            if (i < cc) mp[v][i + 1] += S;
            ll x = S; int r = -1;
            while ((*it).ss < x) {
                r = (*it).ff;
                x -= (*it).ss;
                if (it == mp[v].begin()) break;
                it--;
            }
            it -> ss -= x;
            if (r != -1){
                while (true){
                    it = mp[v].lower_bound(r);
                    if (it == mp[v].end() || (*it).ff > i) break;
                    mp[v].erase(it);
                }
            }
        }
    };
    ll out = 0;
    for (int i = 1; i <= n; i++){
        if (!used[i]){
            solve(i);
            out += (*mp[i].begin()).ss;
        }
    }
    cout<<out<<"\n";
}

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:25:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     while (i < all.size()){
      |            ~~^~~~~~~~~~~~
worst_reporter2.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         while (j < all.size() && all[i] == all[j]){
      |                ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 2632 KB Output is correct
6 Correct 6 ms 2772 KB Output is correct
7 Correct 6 ms 2904 KB Output is correct
8 Correct 7 ms 2652 KB Output is correct
9 Correct 6 ms 2908 KB Output is correct
10 Correct 7 ms 2648 KB Output is correct
11 Correct 6 ms 3160 KB Output is correct
12 Correct 5 ms 2904 KB Output is correct
13 Correct 4 ms 2940 KB Output is correct
14 Correct 5 ms 2140 KB Output is correct
15 Correct 4 ms 2140 KB Output is correct
16 Correct 6 ms 2908 KB Output is correct
17 Correct 6 ms 3164 KB Output is correct
18 Correct 6 ms 3420 KB Output is correct
19 Correct 5 ms 2652 KB Output is correct
20 Correct 5 ms 2652 KB Output is correct
21 Correct 4 ms 2672 KB Output is correct
22 Correct 4 ms 2396 KB Output is correct
23 Correct 4 ms 2396 KB Output is correct
24 Correct 5 ms 2680 KB Output is correct
25 Correct 5 ms 2648 KB Output is correct
26 Correct 4 ms 3160 KB Output is correct
27 Correct 5 ms 2652 KB Output is correct
28 Correct 5 ms 2652 KB Output is correct
29 Correct 5 ms 2904 KB Output is correct
30 Correct 5 ms 2652 KB Output is correct
31 Correct 4 ms 2788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 2632 KB Output is correct
6 Correct 6 ms 2772 KB Output is correct
7 Correct 6 ms 2904 KB Output is correct
8 Correct 7 ms 2652 KB Output is correct
9 Correct 6 ms 2908 KB Output is correct
10 Correct 7 ms 2648 KB Output is correct
11 Correct 6 ms 3160 KB Output is correct
12 Correct 5 ms 2904 KB Output is correct
13 Correct 4 ms 2940 KB Output is correct
14 Correct 5 ms 2140 KB Output is correct
15 Correct 4 ms 2140 KB Output is correct
16 Correct 6 ms 2908 KB Output is correct
17 Correct 6 ms 3164 KB Output is correct
18 Correct 6 ms 3420 KB Output is correct
19 Correct 5 ms 2652 KB Output is correct
20 Correct 5 ms 2652 KB Output is correct
21 Correct 4 ms 2672 KB Output is correct
22 Correct 4 ms 2396 KB Output is correct
23 Correct 4 ms 2396 KB Output is correct
24 Correct 5 ms 2680 KB Output is correct
25 Correct 5 ms 2648 KB Output is correct
26 Correct 4 ms 3160 KB Output is correct
27 Correct 5 ms 2652 KB Output is correct
28 Correct 5 ms 2652 KB Output is correct
29 Correct 5 ms 2904 KB Output is correct
30 Correct 5 ms 2652 KB Output is correct
31 Correct 4 ms 2788 KB Output is correct
32 Correct 6 ms 2648 KB Output is correct
33 Correct 357 ms 108488 KB Output is correct
34 Correct 374 ms 108256 KB Output is correct
35 Correct 356 ms 105684 KB Output is correct
36 Correct 378 ms 108336 KB Output is correct
37 Correct 362 ms 109628 KB Output is correct
38 Correct 438 ms 120024 KB Output is correct
39 Correct 177 ms 98796 KB Output is correct
40 Correct 189 ms 98936 KB Output is correct
41 Correct 193 ms 101320 KB Output is correct
42 Correct 170 ms 76488 KB Output is correct
43 Correct 189 ms 76564 KB Output is correct
44 Correct 346 ms 133576 KB Output is correct
45 Correct 363 ms 133856 KB Output is correct
46 Correct 424 ms 162412 KB Output is correct
47 Correct 260 ms 89652 KB Output is correct
48 Correct 286 ms 88776 KB Output is correct
49 Correct 209 ms 90056 KB Output is correct
50 Correct 200 ms 79820 KB Output is correct
51 Correct 214 ms 79832 KB Output is correct
52 Correct 218 ms 90572 KB Output is correct
53 Correct 237 ms 89832 KB Output is correct
54 Correct 191 ms 111456 KB Output is correct
55 Correct 266 ms 91336 KB Output is correct
56 Correct 209 ms 99532 KB Output is correct
57 Correct 198 ms 103612 KB Output is correct
58 Correct 201 ms 94444 KB Output is correct
59 Correct 197 ms 94436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 2632 KB Output is correct
6 Correct 6 ms 2772 KB Output is correct
7 Correct 6 ms 2904 KB Output is correct
8 Correct 7 ms 2652 KB Output is correct
9 Correct 6 ms 2908 KB Output is correct
10 Correct 7 ms 2648 KB Output is correct
11 Correct 6 ms 3160 KB Output is correct
12 Correct 5 ms 2904 KB Output is correct
13 Correct 4 ms 2940 KB Output is correct
14 Correct 5 ms 2140 KB Output is correct
15 Correct 4 ms 2140 KB Output is correct
16 Correct 6 ms 2908 KB Output is correct
17 Correct 6 ms 3164 KB Output is correct
18 Correct 6 ms 3420 KB Output is correct
19 Correct 5 ms 2652 KB Output is correct
20 Correct 5 ms 2652 KB Output is correct
21 Correct 4 ms 2672 KB Output is correct
22 Correct 4 ms 2396 KB Output is correct
23 Correct 4 ms 2396 KB Output is correct
24 Correct 5 ms 2680 KB Output is correct
25 Correct 5 ms 2648 KB Output is correct
26 Correct 4 ms 3160 KB Output is correct
27 Correct 5 ms 2652 KB Output is correct
28 Correct 5 ms 2652 KB Output is correct
29 Correct 5 ms 2904 KB Output is correct
30 Correct 5 ms 2652 KB Output is correct
31 Correct 4 ms 2788 KB Output is correct
32 Correct 6 ms 2648 KB Output is correct
33 Correct 357 ms 108488 KB Output is correct
34 Correct 374 ms 108256 KB Output is correct
35 Correct 356 ms 105684 KB Output is correct
36 Correct 378 ms 108336 KB Output is correct
37 Correct 362 ms 109628 KB Output is correct
38 Correct 438 ms 120024 KB Output is correct
39 Correct 177 ms 98796 KB Output is correct
40 Correct 189 ms 98936 KB Output is correct
41 Correct 193 ms 101320 KB Output is correct
42 Correct 170 ms 76488 KB Output is correct
43 Correct 189 ms 76564 KB Output is correct
44 Correct 346 ms 133576 KB Output is correct
45 Correct 363 ms 133856 KB Output is correct
46 Correct 424 ms 162412 KB Output is correct
47 Correct 260 ms 89652 KB Output is correct
48 Correct 286 ms 88776 KB Output is correct
49 Correct 209 ms 90056 KB Output is correct
50 Correct 200 ms 79820 KB Output is correct
51 Correct 214 ms 79832 KB Output is correct
52 Correct 218 ms 90572 KB Output is correct
53 Correct 237 ms 89832 KB Output is correct
54 Correct 191 ms 111456 KB Output is correct
55 Correct 266 ms 91336 KB Output is correct
56 Correct 209 ms 99532 KB Output is correct
57 Correct 198 ms 103612 KB Output is correct
58 Correct 201 ms 94444 KB Output is correct
59 Correct 197 ms 94436 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 456 KB Output is correct
64 Correct 368 ms 92856 KB Output is correct
65 Incorrect 351 ms 91740 KB Output isn't correct
66 Halted 0 ms 0 KB -