Submission #876682

# Submission time Handle Problem Language Result Execution time Memory
876682 2023-11-22T08:16:59 Z LucaIlie Passport (JOI23_passport) C++17
100 / 100
571 ms 66304 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int MAX_N = 2e5 + 10;
const int INF = 1e9;
int l[MAX_N + 1], r[MAX_N + 1], minL[MAX_N + 1], maxR[MAX_N + 1], minSufL[MAX_N + 1], maxPrefR[MAX_N + 1], dist1[MAX_N + 1], distN[MAX_N + 1], dist1R[MAX_N + 1], distNR[MAX_N + 1];
 
int n;
 
struct AINT {
    struct px {
        int p, x;
 
        bool operator < ( const px &a ) const {
            if ( x == a.x )
                return p < a.p;
            return x < a.x;
        }
    };
    set<px> s[MAX_N + 1];
    px aint[4 * MAX_N];
 
    void insert( int v, int l, int r, int p, int x, int y ) {
        if ( l > x || r < x )
            return;
 
        if ( l == r ) {
            s[x].insert( { p, y } );
            aint[v] = *s[x].rbegin();
            return;
        }
        
        int mid = (l + r) / 2;
        insert( v * 2 + 1, l, mid, p, x, y );
        insert( v * 2 + 2, mid + 1, r, p, x, y );
        aint[v] = max( aint[v * 2 + 1], aint[v * 2 + 2] );
    }
 
    void erase( int v, int l, int r, int p, int x, int y ) {
        if ( l > x || r < x )
            return;
 
        if ( l == r ) {
            s[x].erase( { p, y } );
            px z = { 0, 0 };
            aint[v] = (s[x].empty() ? z : (*s[x].rbegin()));
            return;
        }
        
        int mid = (l + r) / 2;
        erase( v * 2 + 1, l, mid, p, x, y );
        erase( v * 2 + 2, mid + 1, r, p, x, y );
        aint[v] = max( aint[v * 2 + 1], aint[v * 2 + 2] );
    }
 
    px query( int v, int l, int r, int lq, int rq ) {
        if ( l > rq || r < lq )
            return { 0, 0 };
 
        if ( lq <= l && r <= rq )
            return aint[v];
 
        int mid = (l + r) / 2;
        return max( query( v * 2 + 1, l, mid, lq, rq ), query( v * 2 + 2, mid + 1, r, lq, rq ) );
    }
} aint1, aintN;
 
void calc_dist1() {
    struct dr {
        int i, dist, maxR;
 
        bool operator < ( const dr &x ) const {
            if ( dist == x.dist )
                return maxR < x.maxR;
            return dist > x.dist;
        }
    };
    for ( int i = 0; i <= n; i++ )
        dist1[i] = INF, maxR[i] = 0;
 
    priority_queue<dr> pq;
    dist1[1] = 0;
    maxR[1] = 0;
    pq.push( { 1, dist1[1], maxR[1] } );
    while ( !pq.empty() ) {
        auto crt = pq.top();
        pq.pop();
 
        int i = crt.i;
 
        if ( crt.dist != dist1[i] || crt.maxR != maxR[i] )
            continue;
 
        auto a = aint1.query( 0, 1, n, 1, i );
        while ( a.x >= i ) {
            int j = a.p;
            if ( dist1[i] + 1 <= dist1[j] && max( maxR[i], r[j] ) > maxR[j] ) {
                dist1[j] = dist1[i] + 1;
                maxR[j] = max( maxR[i], r[j] );
                pq.push( { j, dist1[j], maxR[j] } );
            }
            aint1.erase( 0, 1, n, j, l[j], r[j] );
            a = aint1.query( 0, 1, n, 1, i );
        }
    }
    dist1[1] = 1;
    maxR[1] = r[1];
}
 
void calc_distN() {
    struct dr {
        int i, dist, minL;
 
        bool operator < ( const dr &x ) const {
            if ( dist == x.dist )
                return minL > x.minL;
            return dist > x.dist;
        }
    };
    for ( int i = 0; i <= n; i++ )
        distN[i] = INF, minL[i] = n + 1;
 
    priority_queue<dr> pq;
    distN[n] = 0;
    minL[n] = n + 1;
    pq.push( { n, distN[n], minL[n] } );
    while ( !pq.empty() ) {
        auto crt = pq.top();
        pq.pop();
 
        int i = crt.i;
        if ( crt.dist != distN[i] || crt.minL != minL[i] )
            continue;
 
        auto a = aintN.query( 0, 1, n, 1, i );
        while ( a.x >= i ) {
            int j = a.p;
            if ( distN[i] + 1 <= distN[j] && min( minL[i], l[j] ) < minL[j] ) {
                distN[j] = distN[i] + 1;
                minL[j] = min( minL[i], l[j] );
                pq.push( { j, distN[j], minL[j] } );
            }
            aintN.erase( 0, 1, n, j, l[j], r[j] );
            a = aintN.query( 0, 1, n, 1, i );
        }
    }
    distN[n] = 1;
    minL[n] = r[n];
}
 
void calc_dist1_rest() {
    for ( int i = 0; i <= n; i++ )
        dist1R[i] = INF;
    minSufL[n] = l[n];
    for ( int i = n - 1; i >= 1; i-- )
        minSufL[i] = min( minSufL[i + 1], l[i] );
    dist1R[1] = 0;
    for ( int i = 2; i <= n; i++ )
        dist1R[i] = dist1R[minSufL[i]] + 1;
}
 
void calc_distN_rest() {
    for ( int i = n - 1; i >= 0; i-- )
        distNR[i] = INF;
    for ( int i = 1; i <= n; i++ )
        maxPrefR[i] = max( maxPrefR[i - 1], r[i] );
    distNR[n] = 0;
    for ( int i = n - 1; i >= 1; i-- )
        distNR[i] = distNR[maxPrefR[i]] + 1;
}
 
int main() {
    ios_base::sync_with_stdio( false );
    cin.tie( NULL );
    cout.tie( NULL );
 
    int q;
 
    cin >> n;
    for ( int i = 1; i <= n; i++ ) {
        cin >> l[i] >> r[i];
        aint1.insert( 0, 1, n, i, l[i], r[i] );
        aintN.insert( 0, 1, n, i, l[i], r[i] );
    }
 
    calc_dist1();
    calc_distN();
    calc_dist1_rest();
    calc_distN_rest();
    
   
    cin >> q;
    while ( q-- ) {
        int x;
        cin >> x;
        int ans = min( distN[x] + dist1R[minL[x]], dist1[x] + distNR[maxR[x]] );
        cout << (ans > n ? -1 : ans) << "\n";
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31320 KB Output is correct
2 Correct 5 ms 31324 KB Output is correct
3 Correct 5 ms 31324 KB Output is correct
4 Correct 552 ms 66304 KB Output is correct
5 Correct 293 ms 60676 KB Output is correct
6 Correct 248 ms 61040 KB Output is correct
7 Correct 317 ms 60668 KB Output is correct
8 Correct 176 ms 54044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31520 KB Output is correct
2 Correct 5 ms 31320 KB Output is correct
3 Correct 5 ms 31320 KB Output is correct
4 Correct 5 ms 31336 KB Output is correct
5 Correct 5 ms 31324 KB Output is correct
6 Correct 5 ms 31324 KB Output is correct
7 Correct 5 ms 31324 KB Output is correct
8 Correct 5 ms 31324 KB Output is correct
9 Correct 5 ms 31324 KB Output is correct
10 Correct 5 ms 31344 KB Output is correct
11 Correct 7 ms 31424 KB Output is correct
12 Correct 5 ms 31576 KB Output is correct
13 Correct 5 ms 31324 KB Output is correct
14 Correct 7 ms 31324 KB Output is correct
15 Correct 5 ms 31324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31520 KB Output is correct
2 Correct 5 ms 31320 KB Output is correct
3 Correct 5 ms 31320 KB Output is correct
4 Correct 5 ms 31336 KB Output is correct
5 Correct 5 ms 31324 KB Output is correct
6 Correct 5 ms 31324 KB Output is correct
7 Correct 5 ms 31324 KB Output is correct
8 Correct 5 ms 31324 KB Output is correct
9 Correct 5 ms 31324 KB Output is correct
10 Correct 5 ms 31344 KB Output is correct
11 Correct 7 ms 31424 KB Output is correct
12 Correct 5 ms 31576 KB Output is correct
13 Correct 5 ms 31324 KB Output is correct
14 Correct 7 ms 31324 KB Output is correct
15 Correct 5 ms 31324 KB Output is correct
16 Correct 8 ms 31580 KB Output is correct
17 Correct 8 ms 31644 KB Output is correct
18 Correct 8 ms 31580 KB Output is correct
19 Correct 8 ms 31580 KB Output is correct
20 Correct 8 ms 31580 KB Output is correct
21 Correct 7 ms 31580 KB Output is correct
22 Correct 7 ms 31604 KB Output is correct
23 Correct 8 ms 31580 KB Output is correct
24 Correct 8 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31520 KB Output is correct
2 Correct 5 ms 31320 KB Output is correct
3 Correct 5 ms 31320 KB Output is correct
4 Correct 5 ms 31336 KB Output is correct
5 Correct 5 ms 31324 KB Output is correct
6 Correct 5 ms 31324 KB Output is correct
7 Correct 5 ms 31324 KB Output is correct
8 Correct 5 ms 31324 KB Output is correct
9 Correct 5 ms 31324 KB Output is correct
10 Correct 5 ms 31344 KB Output is correct
11 Correct 7 ms 31424 KB Output is correct
12 Correct 5 ms 31576 KB Output is correct
13 Correct 5 ms 31324 KB Output is correct
14 Correct 7 ms 31324 KB Output is correct
15 Correct 5 ms 31324 KB Output is correct
16 Correct 8 ms 31580 KB Output is correct
17 Correct 8 ms 31644 KB Output is correct
18 Correct 8 ms 31580 KB Output is correct
19 Correct 8 ms 31580 KB Output is correct
20 Correct 8 ms 31580 KB Output is correct
21 Correct 7 ms 31580 KB Output is correct
22 Correct 7 ms 31604 KB Output is correct
23 Correct 8 ms 31580 KB Output is correct
24 Correct 8 ms 31580 KB Output is correct
25 Correct 5 ms 31324 KB Output is correct
26 Correct 5 ms 31148 KB Output is correct
27 Correct 8 ms 31580 KB Output is correct
28 Correct 8 ms 31580 KB Output is correct
29 Correct 8 ms 31580 KB Output is correct
30 Correct 8 ms 31664 KB Output is correct
31 Correct 8 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31320 KB Output is correct
2 Correct 5 ms 31324 KB Output is correct
3 Correct 5 ms 31324 KB Output is correct
4 Correct 552 ms 66304 KB Output is correct
5 Correct 293 ms 60676 KB Output is correct
6 Correct 248 ms 61040 KB Output is correct
7 Correct 317 ms 60668 KB Output is correct
8 Correct 176 ms 54044 KB Output is correct
9 Correct 5 ms 31520 KB Output is correct
10 Correct 5 ms 31320 KB Output is correct
11 Correct 5 ms 31320 KB Output is correct
12 Correct 5 ms 31336 KB Output is correct
13 Correct 5 ms 31324 KB Output is correct
14 Correct 5 ms 31324 KB Output is correct
15 Correct 5 ms 31324 KB Output is correct
16 Correct 5 ms 31324 KB Output is correct
17 Correct 5 ms 31324 KB Output is correct
18 Correct 5 ms 31344 KB Output is correct
19 Correct 7 ms 31424 KB Output is correct
20 Correct 5 ms 31576 KB Output is correct
21 Correct 5 ms 31324 KB Output is correct
22 Correct 7 ms 31324 KB Output is correct
23 Correct 5 ms 31324 KB Output is correct
24 Correct 8 ms 31580 KB Output is correct
25 Correct 8 ms 31644 KB Output is correct
26 Correct 8 ms 31580 KB Output is correct
27 Correct 8 ms 31580 KB Output is correct
28 Correct 8 ms 31580 KB Output is correct
29 Correct 7 ms 31580 KB Output is correct
30 Correct 7 ms 31604 KB Output is correct
31 Correct 8 ms 31580 KB Output is correct
32 Correct 8 ms 31580 KB Output is correct
33 Correct 5 ms 31324 KB Output is correct
34 Correct 5 ms 31148 KB Output is correct
35 Correct 8 ms 31580 KB Output is correct
36 Correct 8 ms 31580 KB Output is correct
37 Correct 8 ms 31580 KB Output is correct
38 Correct 8 ms 31664 KB Output is correct
39 Correct 8 ms 31580 KB Output is correct
40 Correct 571 ms 66292 KB Output is correct
41 Correct 315 ms 63124 KB Output is correct
42 Correct 328 ms 61032 KB Output is correct
43 Correct 340 ms 61492 KB Output is correct
44 Correct 269 ms 63568 KB Output is correct
45 Correct 245 ms 63056 KB Output is correct
46 Correct 122 ms 44132 KB Output is correct
47 Correct 340 ms 63508 KB Output is correct
48 Correct 340 ms 63572 KB Output is correct