Submission #542546

# Submission time Handle Problem Language Result Execution time Memory
542546 2022-03-26T22:18:16 Z chonka Worst Reporter 4 (JOI21_worst_reporter4) C++
100 / 100
268 ms 76192 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;

// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define MAXN 200007

struct stairs {
    map < int , ll > w ;
    stairs ( ) { w.clear ( ) ; }
    int get_sz ( ) { return w.size ( ) ; }

    void absorb ( stairs &other ) {
        for ( auto [ x , y ] : other.w ) {
            w[ x ] += y ;
        }
    }
    void add ( int pos , long long cost ) {
        w[ 0 ] += cost ;
        w[ pos ] -= cost ;
        w[ pos + 1 ] += cost ;
        auto it = w.find ( pos ) ;
        while ( it->second < 0 ) {
            auto hh = it ;
            -- hh ;
            if ( abs ( hh->second ) >= abs ( it->second ) ) {
                hh->second += it->second ;
                w.erase ( it ) ;
                break ;
            }
            else {
                it->second += hh->second ;
                w.erase ( hh ) ;
            }
        }
    }
};

int n ;
int nxt[ MAXN ] , a[ MAXN ] , cost[ MAXN ] ;
vector < int > v[ MAXN ] ;

bool used[ MAXN ] , in_cycle[ MAXN ] ;
int root[ MAXN ] ;


stairs dfs ( int vertex ) {
    stairs ans ;
    for ( auto x : v[ vertex ] ) {
        auto ret = dfs ( x ) ;
        if ( ans.get_sz ( ) < ret.get_sz ( ) ) {
            swap ( ans , ret ) ;
        }
        ans.absorb ( ret ) ;
    }
    ans.add ( a[ vertex ] , cost[ vertex ] ) ;
    return ans ;
}

void input ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> nxt[ i ] >> a[ i ] >> cost[ i ] ;
        v[ nxt[ i ] ].push_back ( i ) ;
    }
}

map < int , ll > cycle_vals ;

void solve ( ) {
    vector < int > aux ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( used[ i ] == false ) {
            int x = i ;
            aux.clear ( ) ;
            while ( used[ x ] == false ) {
                aux.push_back ( x ) ;
                used[ x ] = true ;
                x = nxt[ x ] ;
            }
            if ( root[ x ] == 0 ) {
                for ( auto y : aux ) {
                    root[ y ] = x ;
                }
            }
            else {
                for ( auto y : aux ) {
                    root[ y ] = root[ x ] ;
                }
            }
        }
    }
    ll ans = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( root[ i ] == i ) {
            aux.clear ( ) ;
            int x = i ;
            while ( in_cycle[ x ] == false ) {
                in_cycle[ x ] = true ;
                aux.push_back ( x ) ;
                x = nxt[ x ] ;
            }
            stairs hh ;
            for ( auto ori : aux ) {
                for ( auto vertex : v[ ori ] ) {
                    if ( in_cycle[ vertex ] == false ) {
                        stairs ret = dfs ( vertex ) ;
                        if ( hh.get_sz ( ) < ret.get_sz ( ) ) {
                            swap ( hh , ret ) ;
                        }
                        hh.absorb ( ret ) ;
                    }
                }
            }
            ll current = 1e17 ;
            ll tot = 0 ;
            cycle_vals.clear ( ) ;
            for ( auto vertex : aux ) {
                tot += cost[ vertex ] ;
                cycle_vals[ a[ vertex ] ] += cost[ vertex ] ;
            }
            cycle_vals[ 0 ] = 0 ;
            auto it = hh.w.begin ( ) ;
            ll outside = 0 ;
            for ( auto [ key , sm ] : cycle_vals ) {
                while ( it != hh.w.end ( ) && it->first <= key ) {
                    outside += it->second ;
                    ++ it ;
                }
                current = min ( current , outside + tot - sm ) ;
            }
            ans += current ;
        }
    }
    cout << ans << "\n" ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ;
    // cin >> t ;
    while ( t -- ) {
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}

Compilation message

worst_reporter2.cpp: In member function 'void stairs::absorb(stairs&)':
worst_reporter2.cpp:14:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   14 |         for ( auto [ x , y ] : other.w ) {
      |                    ^
worst_reporter2.cpp: In function 'void solve()':
worst_reporter2.cpp:125:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  125 |             for ( auto [ key , sm ] : cycle_vals ) {
      |                        ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5036 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 8 ms 5588 KB Output is correct
6 Correct 7 ms 5300 KB Output is correct
7 Correct 5 ms 5280 KB Output is correct
8 Correct 8 ms 5548 KB Output is correct
9 Correct 7 ms 5332 KB Output is correct
10 Correct 6 ms 5332 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 7 ms 6404 KB Output is correct
13 Correct 6 ms 6452 KB Output is correct
14 Correct 6 ms 5972 KB Output is correct
15 Correct 6 ms 5860 KB Output is correct
16 Correct 9 ms 5588 KB Output is correct
17 Correct 6 ms 5332 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 6 ms 5936 KB Output is correct
20 Correct 5 ms 5812 KB Output is correct
21 Correct 5 ms 5812 KB Output is correct
22 Correct 6 ms 5460 KB Output is correct
23 Correct 6 ms 5204 KB Output is correct
24 Correct 7 ms 6064 KB Output is correct
25 Correct 6 ms 5844 KB Output is correct
26 Correct 7 ms 6740 KB Output is correct
27 Correct 6 ms 5940 KB Output is correct
28 Correct 6 ms 6100 KB Output is correct
29 Correct 7 ms 6456 KB Output is correct
30 Correct 7 ms 6356 KB Output is correct
31 Correct 7 ms 6324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5036 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 8 ms 5588 KB Output is correct
6 Correct 7 ms 5300 KB Output is correct
7 Correct 5 ms 5280 KB Output is correct
8 Correct 8 ms 5548 KB Output is correct
9 Correct 7 ms 5332 KB Output is correct
10 Correct 6 ms 5332 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 7 ms 6404 KB Output is correct
13 Correct 6 ms 6452 KB Output is correct
14 Correct 6 ms 5972 KB Output is correct
15 Correct 6 ms 5860 KB Output is correct
16 Correct 9 ms 5588 KB Output is correct
17 Correct 6 ms 5332 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 6 ms 5936 KB Output is correct
20 Correct 5 ms 5812 KB Output is correct
21 Correct 5 ms 5812 KB Output is correct
22 Correct 6 ms 5460 KB Output is correct
23 Correct 6 ms 5204 KB Output is correct
24 Correct 7 ms 6064 KB Output is correct
25 Correct 6 ms 5844 KB Output is correct
26 Correct 7 ms 6740 KB Output is correct
27 Correct 6 ms 5940 KB Output is correct
28 Correct 6 ms 6100 KB Output is correct
29 Correct 7 ms 6456 KB Output is correct
30 Correct 7 ms 6356 KB Output is correct
31 Correct 7 ms 6324 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 268 ms 24104 KB Output is correct
34 Correct 198 ms 12024 KB Output is correct
35 Correct 261 ms 21884 KB Output is correct
36 Correct 188 ms 11904 KB Output is correct
37 Correct 141 ms 11692 KB Output is correct
38 Correct 126 ms 11668 KB Output is correct
39 Correct 163 ms 58656 KB Output is correct
40 Correct 147 ms 58532 KB Output is correct
41 Correct 109 ms 58572 KB Output is correct
42 Correct 154 ms 36744 KB Output is correct
43 Correct 143 ms 36688 KB Output is correct
44 Correct 267 ms 23848 KB Output is correct
45 Correct 177 ms 12156 KB Output is correct
46 Correct 93 ms 11468 KB Output is correct
47 Correct 201 ms 40484 KB Output is correct
48 Correct 125 ms 38468 KB Output is correct
49 Correct 106 ms 38376 KB Output is correct
50 Correct 188 ms 25928 KB Output is correct
51 Correct 96 ms 13384 KB Output is correct
52 Correct 218 ms 46052 KB Output is correct
53 Correct 120 ms 38948 KB Output is correct
54 Correct 212 ms 76192 KB Output is correct
55 Correct 181 ms 46924 KB Output is correct
56 Correct 178 ms 59884 KB Output is correct
57 Correct 177 ms 65712 KB Output is correct
58 Correct 188 ms 59684 KB Output is correct
59 Correct 194 ms 59740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5036 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 8 ms 5588 KB Output is correct
6 Correct 7 ms 5300 KB Output is correct
7 Correct 5 ms 5280 KB Output is correct
8 Correct 8 ms 5548 KB Output is correct
9 Correct 7 ms 5332 KB Output is correct
10 Correct 6 ms 5332 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 7 ms 6404 KB Output is correct
13 Correct 6 ms 6452 KB Output is correct
14 Correct 6 ms 5972 KB Output is correct
15 Correct 6 ms 5860 KB Output is correct
16 Correct 9 ms 5588 KB Output is correct
17 Correct 6 ms 5332 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 6 ms 5936 KB Output is correct
20 Correct 5 ms 5812 KB Output is correct
21 Correct 5 ms 5812 KB Output is correct
22 Correct 6 ms 5460 KB Output is correct
23 Correct 6 ms 5204 KB Output is correct
24 Correct 7 ms 6064 KB Output is correct
25 Correct 6 ms 5844 KB Output is correct
26 Correct 7 ms 6740 KB Output is correct
27 Correct 6 ms 5940 KB Output is correct
28 Correct 6 ms 6100 KB Output is correct
29 Correct 7 ms 6456 KB Output is correct
30 Correct 7 ms 6356 KB Output is correct
31 Correct 7 ms 6324 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 268 ms 24104 KB Output is correct
34 Correct 198 ms 12024 KB Output is correct
35 Correct 261 ms 21884 KB Output is correct
36 Correct 188 ms 11904 KB Output is correct
37 Correct 141 ms 11692 KB Output is correct
38 Correct 126 ms 11668 KB Output is correct
39 Correct 163 ms 58656 KB Output is correct
40 Correct 147 ms 58532 KB Output is correct
41 Correct 109 ms 58572 KB Output is correct
42 Correct 154 ms 36744 KB Output is correct
43 Correct 143 ms 36688 KB Output is correct
44 Correct 267 ms 23848 KB Output is correct
45 Correct 177 ms 12156 KB Output is correct
46 Correct 93 ms 11468 KB Output is correct
47 Correct 201 ms 40484 KB Output is correct
48 Correct 125 ms 38468 KB Output is correct
49 Correct 106 ms 38376 KB Output is correct
50 Correct 188 ms 25928 KB Output is correct
51 Correct 96 ms 13384 KB Output is correct
52 Correct 218 ms 46052 KB Output is correct
53 Correct 120 ms 38948 KB Output is correct
54 Correct 212 ms 76192 KB Output is correct
55 Correct 181 ms 46924 KB Output is correct
56 Correct 178 ms 59884 KB Output is correct
57 Correct 177 ms 65712 KB Output is correct
58 Correct 188 ms 59684 KB Output is correct
59 Correct 194 ms 59740 KB Output is correct
60 Correct 3 ms 4948 KB Output is correct
61 Correct 3 ms 4948 KB Output is correct
62 Correct 3 ms 5032 KB Output is correct
63 Correct 3 ms 5016 KB Output is correct
64 Correct 256 ms 26280 KB Output is correct
65 Correct 202 ms 18472 KB Output is correct
66 Correct 257 ms 27196 KB Output is correct
67 Correct 212 ms 19004 KB Output is correct
68 Correct 163 ms 18020 KB Output is correct
69 Correct 140 ms 17720 KB Output is correct
70 Correct 192 ms 29056 KB Output is correct
71 Correct 113 ms 20340 KB Output is correct
72 Correct 225 ms 33132 KB Output is correct
73 Correct 113 ms 20788 KB Output is correct
74 Correct 234 ms 29624 KB Output is correct
75 Correct 148 ms 17284 KB Output is correct
76 Correct 126 ms 17324 KB Output is correct
77 Correct 187 ms 30056 KB Output is correct
78 Correct 113 ms 17604 KB Output is correct
79 Correct 240 ms 29992 KB Output is correct
80 Correct 170 ms 19264 KB Output is correct
81 Correct 125 ms 18864 KB Output is correct
82 Correct 145 ms 33364 KB Output is correct
83 Correct 89 ms 19836 KB Output is correct
84 Correct 214 ms 41692 KB Output is correct
85 Correct 199 ms 41636 KB Output is correct
86 Correct 203 ms 38480 KB Output is correct
87 Correct 215 ms 41632 KB Output is correct
88 Correct 202 ms 41752 KB Output is correct