제출 #943504

#제출 시각아이디문제언어결과실행 시간메모리
943504vjudge1Passport (JOI23_passport)C++17
22 / 100
329 ms132164 KiB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define x first
#define y second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;

const int Max = 3e3 + 7, Inf = 1e9 + 7;

void print(bool x) { cout << (x ? "YES" : "NO") << endl; }

string tostring (__int128 x)
{
    string ans = "";
    while(x > 0)
    {
        ans += (x % 10 + '0');
        x /= 10;
    }
    reverse(all(ans));
    return ans;
}

struct Segment_tree_iterative_max
{
    ll n;
    vector<pair<ll,ll>> st;
    pair<ll,ll> merge(pair<ll,ll> a, pair<ll,ll> b)
    {
        if(a.first==b.first)
        {
            a.second=min(a.second,b.second);
            return a;
        }
        a=max(a,b);
        return a;
    }
    void update(ll a, pair<ll,ll> b)
    {
        a+=n;
        st[a].x=b.x;
        st[a].y=b.y;
        for(a/=2;a>0;a/=2)
            st[a]=merge(st[a*2],st[a*2+1]);
    }
    pair<ll,ll> query(ll a, ll b){
        pair<ll,ll> s = {0,1e9};
        a+=n;b+=n;
        while(a<=b){
            if(a%2==1) s=merge(s,st[a++]);
            if(b%2==0) s=merge(s,st[b--]);
            a/=2;b/=2;
        }
        return s;
    }
    pair<ll,ll> query(ll a) { return query(a, a); }
    Segment_tree_iterative_max(ll N){
        n = N * 2;
        st.assign(n*2,{0,1e9});
    }
}maximo(2e5+5);
struct Segment_tree_iterative_min{
    ll n;
    vector<pair<ll,ll>> st;
    pair<ll,ll> merge(pair<ll,ll> a, pair<ll,ll> b)
    {
        if(a.first==b.first)
        {
            a.second=max(a.second,b.second);
            return a;
        }
        a=min(a,b);
        return a;
    }
    void update(ll a, pair<ll,ll> b)
    {
        a+=n;
        st[a].x=b.x;
        st[a].y=b.y;
        for(a/=2;a>0;a/=2)
            st[a]=merge(st[a*2],st[a*2+1]);
    }
    pair<ll,ll> query(ll a, ll b){
        pair<ll,ll> s = {1e9,0};
        a+=n;b+=n;
        while(a<=b){
            if(a%2==1) s=merge(s,st[a++]);
            if(b%2==0) s=merge(s,st[b--]);
            a/=2;b/=2;
        }
        return s;
    }
    pair<ll,ll> query(ll a) { return query(a, a); }
    Segment_tree_iterative_min(ll N){
        n = N * 2;
        st.assign(n*2,{1e9,0});
    }
}minimo(2e5+5);
map <int, map <int, int>> mp; int n; 

int dp(int l, int r)
{
    if(l == 1 && r == n) return 0;
    if(mp[l][r] != 0) return mp[l][r];
    mp[l][r] = Inf;
    pair<int, int> a = maximo.query(l, r); a.sd = min(l, a.sd);
    pair<int, int> b = minimo.query(l, r); b.sd = max(r, b.sd);
    return mp[l][r] = min(dp(a.sd, a.fs), dp(b.fs, b.sd)) + 1;
}

void solve()
{
    cin >> n; vector <pair<int, int>> v(n);

    for(int i = 0; i < n; i++) 
    {
        cin >> v[i].fs >> v[i].sd;
        maximo.update(i+1, { v[i].sd, v[i].fs });
        minimo.update(i+1, { v[i].fs, v[i].sd });
    }

    int q; cin >> q; 

    while(q--)
    {
        int x, ans; cin >> x; x--;
        ans = dp(v[x].fs, v[x].sd);
        cout << (ans >= Inf ? -1 : ans + 1) << endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int Q = 1; //cin >> Q;

    while (Q--)
    {
        solve();
    }

    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...