Submission #876532

#TimeUsernameProblemLanguageResultExecution timeMemory
876532LucaIliePassport (JOI23_passport)C++17
48 / 100
2089 ms13124 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5;
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], par[MAX_N + 1];

int n;

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;

        for ( int j = 1; j <= n; j++ ) {
            if ( l[j] <= i && i <= r[j] ) {
                if ( dist1[i] + 1 <= dist1[j] && max( maxR[i], r[j] ) > maxR[j] ) {
                    dist1[j] = dist1[i] + 1;
                    par[j] = i;
                    maxR[j] = max( maxR[i], r[j] );
                    pq.push( { j, dist1[j], maxR[j] } );
                }
            }
        }
    }
    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;

        for ( int j = 1; j <= n; j++ ) {
            if ( l[j] <= i && i <= r[j] ) {
                if ( distN[i] + 1 <= distN[j] && min( minL[i], l[j] ) < minL[j] ) {
                    distN[j] = distN[i] + 1;
                    par[j] = i;
                    minL[j] = min( minL[i], l[j] );
                    pq.push( { j, distN[j], minL[j] } );
                }
            }
        }
    }
    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];

    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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...