Submission #1262107

#TimeUsernameProblemLanguageResultExecution timeMemory
1262107chikien2009Passport (JOI23_passport)C++20
100 / 100
216 ms14756 KiB
#include <bits/stdc++.h>

using namespace std;

void setup()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

array<int, 3> a[200000];

struct SEGMENT_TREE
{
    int tree[800000];
    inline void Set(int ind, int l, int r)
    {
        if (l == r)
        {
            tree[ind] = a[l][1];
            return;
        }
        int m = (l + r) >> 1;
        Set(ind << 1, l, m);
        Set(ind << 1 | 1, m + 1, r);
        tree[ind] = max(tree[ind << 1], tree[ind << 1 | 1]);
    }
    inline void Get(int ind, int l, int r, int x, vector<int> & v)
    {
        if (tree[ind] < x || x < a[l][0])
        {
            return;
        }
        if (l == r)
        {
            v.push_back(l);
            tree[ind] = -1;
            return;
        }
        int m = (l + r) >> 1;
        Get(ind << 1, l, m, x, v);
        Get(ind << 1 | 1, m + 1, r, x, v);
        tree[ind] = max(tree[ind << 1], tree[ind << 1 | 1]);
    }
} st;

int n, q, b, c;
int id[200000], f[400000], g[400000], h[400000];
priority_queue<pair<int, int>> pq;
vector<int> temp;

inline void Dijkstra(int sp, int(&dist)[400000])
{
    if (sp == -1)
    {
        for (int i = 0; i < n; ++i)
        {
            dist[i] = min(dist[i], dist[n + i] + 1);
            pq.push({-dist[i], i});
        }
    }   
    else
    {
        fill_n(dist, 400000, 1e6);
        dist[sp] = dist[sp + n] = 0;
        pq.push({-dist[sp], sp});
    }
    while (!pq.empty())
    {
        b = -pq.top().first;
        c = pq.top().second;
        pq.pop();
        if (dist[c] != b)
        {
            continue;
        }
        temp.clear();
        st.Get(1, 0, n - 1, a[c][2], temp);
        for (auto & i : temp)
        {
            if (dist[i] > dist[c] + 1)
            {
                dist[n + i] = dist[c];
                dist[i] = dist[c] + 1;
                pq.push({-dist[i], i});
            }
        }
    }
}

int main()
{
    setup();

    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i][0] >> a[i][1];
        a[i][0]--;
        a[i][1]--;
        a[i][2] = i;
    }
    sort(a, a + n);
    for (int i = 0; i < n; ++i)
    {
        id[a[i][2]] = i;
    }
    st.Set(1, 0, n - 1);
    Dijkstra(id[0], f);
    st.Set(1, 0, n - 1);
    Dijkstra(id[n - 1], g);
    for (int i = 0; i < 2 * n; ++i)
    {
        h[i] = f[i] + g[i];
    }
    st.Set(1, 0, n - 1);
    Dijkstra(-1, h);
    cin >> q;
    while (q--)
    {
        cin >> b;
        b = h[id[b - 1]];
        cout << (b < 1e6 ? b : -1) << "\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...