Submission #829491

# Submission time Handle Problem Language Result Execution time Memory
829491 2023-08-18T11:27:13 Z chonka Sky Walking (IOI19_walk) C++17
100 / 100
2781 ms 326980 KB
#include "walk.h"
#include<bits/stdc++.h>
using namespace std ;

const int MAXN = 1e5 + 7 ;
#define fi first
#define se second
typedef long long ll ;
typedef pair < int , int > pii ;

int n ; 

class Tree {
public :
    int mx[ 4 * MAXN ] ;
    void init ( int w , int l , int r , vector < int > &vals ) {
        if ( l == r ) {
            mx[ w ] = vals[ l - 1 ] ;
            return ;
        }
        int mid = ( l + r ) / 2 ;
        init ( 2 * w , l , mid , vals ) ;
        init ( 2 * w + 1 , mid + 1 , r , vals ) ;
        mx[ w ] = max ( mx[ 2 * w ] , mx[ 2 * w + 1 ] ) ;
    }
    int get_l ( int w , int l , int r , int pos , int sr ) {
        if ( pos < l ) { return -1 ; }
        if ( mx[ w ] < sr ) { return -1 ; }
        if ( l == r ) { return l ; }
        int mid = ( l + r ) / 2 ;
        int aux = get_l ( 2 * w + 1 , mid + 1 , r , pos , sr ) ;
        if ( aux != -1 ) { return aux ; }
        return get_l ( 2 * w , l , mid , pos , sr ) ;
    }
    int get_r ( int w , int l , int r , int pos , int sr ) {
        if ( r < pos ) { return -1 ; }
        if ( mx[ w ] < sr ) { return -1 ; }
        if ( l == r ) { return l ; }
        int mid = ( l + r ) / 2 ;
        int aux = get_r ( 2 * w , l , mid , pos , sr ) ;
        if ( aux != -1 ) { return aux ; }
        return get_r ( 2 * w + 1 , mid + 1 , r , pos , sr ) ;
    }
};
Tree w ;

const int coef = 70 ;

int rv[ MAXN ] ;
set < pii > spec[ MAXN ] , scd[ MAXN ] ;
vector < pii > aft[ MAXN ] ;
vector < pii > at_lvl[ MAXN ] ;


vector < pii > v[ coef * MAXN ] ;

void add_edge ( int x , int y , int len ) {
    v[ x ].push_back ( { y , len } ) ;
    v[ y ].push_back ( { x , len } ) ;
}

const ll inf = 1e18 ;
ll dist[ coef * MAXN ] ;

ll dijkstra ( int ori , int targ ) {
    priority_queue < pair < ll , int > > q ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        dist[ i ] = inf ;
    }
    dist[ ori ] = 0 ;
    q.push ( { 0 , ori } ) ;
    while ( q.empty ( ) == false ) {
        auto [ cost , x ] = q.top ( ) ;
        q.pop ( ) ;
        cost = -cost ;
        if ( cost != dist[ x ] ) { continue ; }
        for ( auto [ to , add ] : v[ x ] ) {
            if ( dist[ to ] > cost + add ) {
                dist[ to ] = cost + add ;
                q.push ( { -dist[ to ] , to } ) ;
            }
        }
    }
    if ( dist[ targ ] == inf ) { return -1 ; }
    return dist[ targ ] ;
}

vector < int > rem[ MAXN ] , nw[ MAXN ] ;

long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
    int len = x.size ( ) ;
    int m = l.size ( ) ;
    map < int , int > foo ;
    for ( int i = 0 ; i < m ; ++ i ) {
        foo[ y[ i ] ] = 0 ;
        nw[ l[ i ] ].push_back ( i ) ;
        rem[ r[ i ] ].push_back ( i ) ;
    }
    int tp = 0 ;
    for ( auto &e : foo ) {
        e.se = ++ tp ;
        rv[ tp ] = e.fi ;
    }
    w.init ( 1 , 1 , len , h ) ;
    for ( int i = 0 ; i < m ; ++ i ) {
        int aux1 = l[ i ] , aux2 = r[ i ] ;
        int act = foo[ y[ i ] ] ;
        for ( auto pt : { s , g } ) { 
            if ( aux1 < pt && pt < aux2 ) {
                int pos1 = w.get_l ( 1 , 1 , len , pt + 1 , y[ i ] ) - 1 ;
                int pos2 = w.get_r ( 1 , 1 , len , pt + 1 , y[ i ] ) - 1 ;
                spec[ pos1 ].insert ( { act , 0 } ) ;
                spec[ pos2 ].insert ( { act , 0 } ) ;
                assert ( h[ pos1 ] >= y[ i ] ) ;
                assert ( h[ pos2 ] >= y[ i ] ) ;
                for ( int j = pos1 + 1 ; j < pt ; ++ j ) {
                    assert ( h[ j ] < y[ i ] ) ;
                }
                for ( int j = pt + 1 ; j < pos2 ; ++ j ) {
                    assert ( h[ j ] < y[ i ] ) ;
                }
            }
        }
        spec[ aux1 ].insert ( { act , 0 } ) ;
        spec[ aux2 ].insert ( { act , 0 } ) ;
    }
    spec[ s ].insert ( { 0 , 0 } ) ;
    spec[ g ].insert ( { 0 , 0 } ) ;
    set < int > op ;
    for ( int i = 0 ; i < len ; ++ i ) {
        for ( auto id : nw[ i ] ) {
            op.insert ( y[ id ] ) ;
        }
        for ( auto [ hh , qq ] : spec[ i ] ) {
            auto it = op.lower_bound ( rv[ hh ] ) ;
            if ( op.size ( ) > 0 && it != op.begin ( ) ) {
                -- it ;
                scd[ i ].insert ( { foo[ *it ] , 0 } ) ;
            }
            it = op.upper_bound ( rv[ hh ] ) ;
            if ( it != op.end ( ) && *it <= h[ i ] ) {
                scd[ i ].insert ( { foo[ *it ] , 0 } ) ;
            }
        }

        for ( auto id : rem[ i ] ) {
            op.erase ( y[ id ] ) ;
        }
        for ( auto id : nw[ i ] ) {
            op.insert ( y[ id ] ) ;
        }

        /**
        for ( auto e : scd[ i ] ) {
            spec[ i ].insert ( e ) ;
        }
        **/
    }
    n = 0 ;
    int ori = -1 , targ = -1 ;
    for ( int i = 0 ; i < len ; ++ i ) {
        for ( auto e : spec[ i ] ) { aft[ i ].push_back ( e ) ; }
        int sz = aft[ i ].size ( ) ;
        for ( int j = 0 ; j < sz ; ++ j ) {
            aft[ i ][ j ].se = ++ n ;
            if ( aft[ i ][ j ].fi == 0 ) {
                if ( i == s ) { ori = n ; }
                else { targ = n ; }
            }
            // at_lvl[ aft[ i ][ j ].fi ].push_back ( { i , n } ) ;
            if ( j > 0 ) {
                add_edge ( aft[ i ][ j - 1 ].se , aft[ i ][ j ].se , rv[ aft[ i ][ j ].fi ] - rv[ aft[ i ][ j - 1 ].fi ] ) ;
            }
        }
        for ( auto e : scd[ i ] ) {
            auto it = spec[ i ].lower_bound ( e ) ;
            if ( it == spec[ i ].end ( ) || it->fi != e.fi ) {
                aft[ i ].push_back ( e ) ;
            }
        }
        sort ( aft[ i ].begin ( ) , aft[ i ].end ( ) ) ;
        sz = aft[ i ].size ( ) ;
        for ( int j = 0 ; j < sz ; ++ j ) {
            if ( aft[ i ][ j ].se == 0 ) { 
                aft[ i ][ j ].se = ++ n ;
            }
            at_lvl[ aft[ i ][ j ].fi ].push_back ( { i , aft[ i ][ j ].se } ) ;
            if ( j > 0 ) {
                add_edge ( aft[ i ][ j - 1 ].se , aft[ i ][ j ].se , rv[ aft[ i ][ j ].fi ] - rv[ aft[ i ][ j - 1 ].fi ] ) ;
            }
        }
    }
    for ( int i = 0 ; i < m ; ++ i ) {
        int aux1 = l[ i ] , aux2 = r[ i ] ;
        int act = foo[ y[ i ] ] ;
        int lo , hi , mid ;
        lo = 0 , hi = at_lvl[ act ].size ( ) - 1 ;
        while ( lo < hi ) {
            mid = ( lo + hi ) / 2 ;
            if ( at_lvl[ act ][ mid ].fi < aux1 ) { lo = mid + 1 ; }
            else { hi = mid ; }
        }
        ++ hi ;
        while ( hi < at_lvl[ act ].size ( ) && at_lvl[ act ][ hi ].fi <= aux2 ) {
            if ( hi >= 1 ) { 
                add_edge ( at_lvl[ act ][ hi - 1 ].se , at_lvl[ act ][ hi ].se , x[ at_lvl[ act ][ hi ].fi ] - x[ at_lvl[ act ][ hi - 1 ].fi ] ) ;
            }
            ++ hi ;
        }
    }
	return dijkstra ( ori , targ ) ;
}

Compilation message

walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:204:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  204 |         while ( hi < at_lvl[ act ].size ( ) && at_lvl[ act ][ hi ].fi <= aux2 ) {
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 78 ms 183376 KB Output is correct
2 Correct 78 ms 183464 KB Output is correct
3 Correct 83 ms 183484 KB Output is correct
4 Correct 81 ms 183444 KB Output is correct
5 Correct 79 ms 183440 KB Output is correct
6 Correct 79 ms 183404 KB Output is correct
7 Correct 81 ms 183500 KB Output is correct
8 Correct 79 ms 183444 KB Output is correct
9 Correct 80 ms 183388 KB Output is correct
10 Correct 78 ms 183524 KB Output is correct
11 Correct 80 ms 183400 KB Output is correct
12 Correct 80 ms 183384 KB Output is correct
13 Correct 78 ms 183508 KB Output is correct
14 Correct 85 ms 183420 KB Output is correct
15 Correct 81 ms 183432 KB Output is correct
16 Correct 81 ms 183448 KB Output is correct
17 Correct 79 ms 183468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 183392 KB Output is correct
2 Correct 81 ms 183488 KB Output is correct
3 Correct 902 ms 260028 KB Output is correct
4 Correct 776 ms 262732 KB Output is correct
5 Correct 553 ms 243800 KB Output is correct
6 Correct 953 ms 240120 KB Output is correct
7 Correct 650 ms 243936 KB Output is correct
8 Correct 961 ms 264548 KB Output is correct
9 Correct 805 ms 258000 KB Output is correct
10 Correct 841 ms 264704 KB Output is correct
11 Correct 723 ms 248248 KB Output is correct
12 Correct 616 ms 239668 KB Output is correct
13 Correct 836 ms 267404 KB Output is correct
14 Correct 2781 ms 231012 KB Output is correct
15 Correct 1089 ms 231588 KB Output is correct
16 Correct 440 ms 232188 KB Output is correct
17 Correct 389 ms 232324 KB Output is correct
18 Correct 732 ms 242368 KB Output is correct
19 Correct 96 ms 186188 KB Output is correct
20 Correct 866 ms 210804 KB Output is correct
21 Correct 385 ms 233268 KB Output is correct
22 Correct 352 ms 233920 KB Output is correct
23 Correct 711 ms 242688 KB Output is correct
24 Correct 398 ms 234880 KB Output is correct
25 Correct 322 ms 231984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 196816 KB Output is correct
2 Correct 1106 ms 277812 KB Output is correct
3 Correct 1018 ms 279548 KB Output is correct
4 Correct 1097 ms 283248 KB Output is correct
5 Correct 1310 ms 283104 KB Output is correct
6 Correct 1291 ms 278592 KB Output is correct
7 Correct 514 ms 235628 KB Output is correct
8 Correct 520 ms 239748 KB Output is correct
9 Correct 1136 ms 273204 KB Output is correct
10 Correct 488 ms 243040 KB Output is correct
11 Correct 92 ms 184780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 196816 KB Output is correct
2 Correct 1106 ms 277812 KB Output is correct
3 Correct 1018 ms 279548 KB Output is correct
4 Correct 1097 ms 283248 KB Output is correct
5 Correct 1310 ms 283104 KB Output is correct
6 Correct 1291 ms 278592 KB Output is correct
7 Correct 514 ms 235628 KB Output is correct
8 Correct 520 ms 239748 KB Output is correct
9 Correct 1136 ms 273204 KB Output is correct
10 Correct 488 ms 243040 KB Output is correct
11 Correct 92 ms 184780 KB Output is correct
12 Correct 1125 ms 279300 KB Output is correct
13 Correct 1004 ms 282176 KB Output is correct
14 Correct 1222 ms 283064 KB Output is correct
15 Correct 809 ms 259620 KB Output is correct
16 Correct 765 ms 264968 KB Output is correct
17 Correct 840 ms 276616 KB Output is correct
18 Correct 787 ms 259620 KB Output is correct
19 Correct 770 ms 264864 KB Output is correct
20 Correct 500 ms 234444 KB Output is correct
21 Correct 104 ms 186952 KB Output is correct
22 Correct 672 ms 259020 KB Output is correct
23 Correct 616 ms 255852 KB Output is correct
24 Correct 484 ms 238356 KB Output is correct
25 Correct 613 ms 251156 KB Output is correct
26 Correct 502 ms 230968 KB Output is correct
27 Correct 1351 ms 283648 KB Output is correct
28 Correct 873 ms 280952 KB Output is correct
29 Correct 1293 ms 277528 KB Output is correct
30 Correct 475 ms 235488 KB Output is correct
31 Correct 1187 ms 273244 KB Output is correct
32 Correct 434 ms 236604 KB Output is correct
33 Correct 392 ms 236920 KB Output is correct
34 Correct 625 ms 243268 KB Output is correct
35 Correct 516 ms 246604 KB Output is correct
36 Correct 412 ms 238340 KB Output is correct
37 Correct 320 ms 233160 KB Output is correct
38 Correct 298 ms 234000 KB Output is correct
39 Correct 619 ms 242648 KB Output is correct
40 Correct 338 ms 234872 KB Output is correct
41 Correct 343 ms 232152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 183376 KB Output is correct
2 Correct 78 ms 183464 KB Output is correct
3 Correct 83 ms 183484 KB Output is correct
4 Correct 81 ms 183444 KB Output is correct
5 Correct 79 ms 183440 KB Output is correct
6 Correct 79 ms 183404 KB Output is correct
7 Correct 81 ms 183500 KB Output is correct
8 Correct 79 ms 183444 KB Output is correct
9 Correct 80 ms 183388 KB Output is correct
10 Correct 78 ms 183524 KB Output is correct
11 Correct 80 ms 183400 KB Output is correct
12 Correct 80 ms 183384 KB Output is correct
13 Correct 78 ms 183508 KB Output is correct
14 Correct 85 ms 183420 KB Output is correct
15 Correct 81 ms 183432 KB Output is correct
16 Correct 81 ms 183448 KB Output is correct
17 Correct 79 ms 183468 KB Output is correct
18 Correct 79 ms 183392 KB Output is correct
19 Correct 81 ms 183488 KB Output is correct
20 Correct 902 ms 260028 KB Output is correct
21 Correct 776 ms 262732 KB Output is correct
22 Correct 553 ms 243800 KB Output is correct
23 Correct 953 ms 240120 KB Output is correct
24 Correct 650 ms 243936 KB Output is correct
25 Correct 961 ms 264548 KB Output is correct
26 Correct 805 ms 258000 KB Output is correct
27 Correct 841 ms 264704 KB Output is correct
28 Correct 723 ms 248248 KB Output is correct
29 Correct 616 ms 239668 KB Output is correct
30 Correct 836 ms 267404 KB Output is correct
31 Correct 2781 ms 231012 KB Output is correct
32 Correct 1089 ms 231588 KB Output is correct
33 Correct 440 ms 232188 KB Output is correct
34 Correct 389 ms 232324 KB Output is correct
35 Correct 732 ms 242368 KB Output is correct
36 Correct 96 ms 186188 KB Output is correct
37 Correct 866 ms 210804 KB Output is correct
38 Correct 385 ms 233268 KB Output is correct
39 Correct 352 ms 233920 KB Output is correct
40 Correct 711 ms 242688 KB Output is correct
41 Correct 398 ms 234880 KB Output is correct
42 Correct 322 ms 231984 KB Output is correct
43 Correct 152 ms 196816 KB Output is correct
44 Correct 1106 ms 277812 KB Output is correct
45 Correct 1018 ms 279548 KB Output is correct
46 Correct 1097 ms 283248 KB Output is correct
47 Correct 1310 ms 283104 KB Output is correct
48 Correct 1291 ms 278592 KB Output is correct
49 Correct 514 ms 235628 KB Output is correct
50 Correct 520 ms 239748 KB Output is correct
51 Correct 1136 ms 273204 KB Output is correct
52 Correct 488 ms 243040 KB Output is correct
53 Correct 92 ms 184780 KB Output is correct
54 Correct 1125 ms 279300 KB Output is correct
55 Correct 1004 ms 282176 KB Output is correct
56 Correct 1222 ms 283064 KB Output is correct
57 Correct 809 ms 259620 KB Output is correct
58 Correct 765 ms 264968 KB Output is correct
59 Correct 840 ms 276616 KB Output is correct
60 Correct 787 ms 259620 KB Output is correct
61 Correct 770 ms 264864 KB Output is correct
62 Correct 500 ms 234444 KB Output is correct
63 Correct 104 ms 186952 KB Output is correct
64 Correct 672 ms 259020 KB Output is correct
65 Correct 616 ms 255852 KB Output is correct
66 Correct 484 ms 238356 KB Output is correct
67 Correct 613 ms 251156 KB Output is correct
68 Correct 502 ms 230968 KB Output is correct
69 Correct 1351 ms 283648 KB Output is correct
70 Correct 873 ms 280952 KB Output is correct
71 Correct 1293 ms 277528 KB Output is correct
72 Correct 475 ms 235488 KB Output is correct
73 Correct 1187 ms 273244 KB Output is correct
74 Correct 434 ms 236604 KB Output is correct
75 Correct 392 ms 236920 KB Output is correct
76 Correct 625 ms 243268 KB Output is correct
77 Correct 516 ms 246604 KB Output is correct
78 Correct 412 ms 238340 KB Output is correct
79 Correct 320 ms 233160 KB Output is correct
80 Correct 298 ms 234000 KB Output is correct
81 Correct 619 ms 242648 KB Output is correct
82 Correct 338 ms 234872 KB Output is correct
83 Correct 343 ms 232152 KB Output is correct
84 Correct 134 ms 194832 KB Output is correct
85 Correct 1362 ms 283948 KB Output is correct
86 Correct 1644 ms 303536 KB Output is correct
87 Correct 125 ms 194948 KB Output is correct
88 Correct 133 ms 195400 KB Output is correct
89 Correct 126 ms 194880 KB Output is correct
90 Correct 104 ms 188160 KB Output is correct
91 Correct 81 ms 183604 KB Output is correct
92 Correct 98 ms 187380 KB Output is correct
93 Correct 380 ms 222048 KB Output is correct
94 Correct 105 ms 189116 KB Output is correct
95 Correct 818 ms 266792 KB Output is correct
96 Correct 757 ms 260072 KB Output is correct
97 Correct 1434 ms 242028 KB Output is correct
98 Correct 675 ms 254752 KB Output is correct
99 Correct 2459 ms 326980 KB Output is correct
100 Correct 1295 ms 287016 KB Output is correct
101 Correct 1573 ms 295300 KB Output is correct
102 Correct 548 ms 238580 KB Output is correct
103 Correct 411 ms 240400 KB Output is correct
104 Correct 444 ms 240336 KB Output is correct
105 Correct 547 ms 244040 KB Output is correct
106 Correct 781 ms 241588 KB Output is correct
107 Correct 853 ms 240132 KB Output is correct
108 Correct 127 ms 191416 KB Output is correct
109 Correct 870 ms 263580 KB Output is correct
110 Correct 910 ms 284600 KB Output is correct
111 Correct 888 ms 284644 KB Output is correct
112 Correct 559 ms 244556 KB Output is correct
113 Correct 472 ms 241296 KB Output is correct