Submission #889896

# Submission time Handle Problem Language Result Execution time Memory
889896 2023-12-20T09:21:47 Z shivensinha4 Passport (JOI23_passport) C++17
100 / 100
738 ms 91488 KB
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'

class SegmentTree {
private:
    vector<vi> tree; vector<ll> raw; int n;

    void update(int i, int j, int v, int l, int r, int p) {
        if (l > j or r < i) return;
        if (l >= i and r <= j) {
            tree[p].push_back(v);
            return;
        }

        int mid = (l + r) / 2;
        int c1 = 2*p+1, c2 = 2*p+2;
        update(i, j, v, l, mid, c1); update(i, j, v, mid+1, r, c2);
    }

    vi get(int i, int l, int r, int p) {
        if (l > i or r < i) return {};

        vi ans = tree[p];
        tree[p].clear();

        if (l != r) {
            int mid = (l + r) / 2;
            int c1 = 2*p+1, c2 = 2*p+2;
            auto al = get(i, l, mid, c1), ar = get(i, mid+1, r, c2);
            ans.insert(ans.end(), al.begin(), al.end());
            ans.insert(ans.end(), ar.begin(), ar.end());
        }

        return ans;
    }

public:
    SegmentTree(int n): n(n) {
        tree.resize(4*n);
    }

    void update(int i, int j, int v) {
        update(i, j, v, 0, n-1, 0);
    }

    vi get(int i) {
        return get(i, 0, n-1, 0);
    }
};


const int INF = 1e9;

int main() {
#ifdef mlocal
    freopen("test.in", "r", stdin);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    vector<ii> seg(n);
    SegmentTree tree(n);
    for_(i, 0, n){
        cin >> seg[i][0] >> seg[i][1], seg[i][0]--, seg[i][1]--;
        tree.update(seg[i][0], seg[i][1], i);
    }

    function<vi(int)> bfs = [&] (int s) {
        vi dist(n, INF);
        dist[s] = 0;

        queue<int> q;
        q.push(s);

        auto curr_tree = tree;

        while (q.size()) {
            auto p = q.front(); q.pop();
            auto adj = curr_tree.get(p);
            for (auto i: adj) if (dist[i] == INF) {
                dist[i] = dist[p] + 1;
                q.push(i);
            }
        }
        return dist;
    };

    vi ans = bfs(0);
    {
        vi ans2 = bfs(n - 1);
        for_(i, 0, n) ans[i] += ans2[i];
    }
    for_(i, 1, n - 1) if (ans[i] < INF) ans[i]--;

    const int pq_size = 2 * n + 10;
    vector<vi> pq(pq_size);
    int dist = 0;

    for_(i, 0, n) if (ans[i] < INF) pq[ans[i]].push_back(i);

    while (true) {
        while (dist < pq_size and pq[dist].empty()) dist++;
        if (dist == pq_size) break;

        int p = pq[dist].back(); pq[dist].pop_back();
        if (dist != ans[p]) continue;

        auto adj = tree.get(p);

        for (auto i: adj) {
            if (ans[i] > ans[p] + 1) {
                ans[i] = ans[p] + 1;
                pq[ans[i]].push_back(i);
            }
        }
    }

    int q; cin >> q;
    while (q--) {
        int p; cin >> p;
        p--;

        cout << (ans[p] >= INF ? -1 : ans[p]) << endl;
    }
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 694 ms 78248 KB Output is correct
5 Correct 538 ms 63988 KB Output is correct
6 Correct 496 ms 64528 KB Output is correct
7 Correct 188 ms 44860 KB Output is correct
8 Correct 96 ms 45812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 600 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 552 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 600 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 552 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 5 ms 1148 KB Output is correct
17 Correct 5 ms 1136 KB Output is correct
18 Correct 5 ms 1136 KB Output is correct
19 Correct 5 ms 1140 KB Output is correct
20 Correct 5 ms 1220 KB Output is correct
21 Correct 4 ms 1136 KB Output is correct
22 Correct 2 ms 880 KB Output is correct
23 Correct 5 ms 1136 KB Output is correct
24 Correct 2 ms 1136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 600 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 552 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 5 ms 1148 KB Output is correct
17 Correct 5 ms 1136 KB Output is correct
18 Correct 5 ms 1136 KB Output is correct
19 Correct 5 ms 1140 KB Output is correct
20 Correct 5 ms 1220 KB Output is correct
21 Correct 4 ms 1136 KB Output is correct
22 Correct 2 ms 880 KB Output is correct
23 Correct 5 ms 1136 KB Output is correct
24 Correct 2 ms 1136 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 8 ms 1144 KB Output is correct
28 Correct 6 ms 1132 KB Output is correct
29 Correct 5 ms 1136 KB Output is correct
30 Correct 4 ms 1136 KB Output is correct
31 Correct 5 ms 1028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 694 ms 78248 KB Output is correct
5 Correct 538 ms 63988 KB Output is correct
6 Correct 496 ms 64528 KB Output is correct
7 Correct 188 ms 44860 KB Output is correct
8 Correct 96 ms 45812 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 552 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 5 ms 1148 KB Output is correct
25 Correct 5 ms 1136 KB Output is correct
26 Correct 5 ms 1136 KB Output is correct
27 Correct 5 ms 1140 KB Output is correct
28 Correct 5 ms 1220 KB Output is correct
29 Correct 4 ms 1136 KB Output is correct
30 Correct 2 ms 880 KB Output is correct
31 Correct 5 ms 1136 KB Output is correct
32 Correct 2 ms 1136 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 8 ms 1144 KB Output is correct
36 Correct 6 ms 1132 KB Output is correct
37 Correct 5 ms 1136 KB Output is correct
38 Correct 4 ms 1136 KB Output is correct
39 Correct 5 ms 1028 KB Output is correct
40 Correct 738 ms 82948 KB Output is correct
41 Correct 566 ms 66568 KB Output is correct
42 Correct 561 ms 91424 KB Output is correct
43 Correct 562 ms 91488 KB Output is correct
44 Correct 521 ms 67032 KB Output is correct
45 Correct 420 ms 68212 KB Output is correct
46 Correct 176 ms 24784 KB Output is correct
47 Correct 563 ms 70252 KB Output is correct
48 Correct 246 ms 69136 KB Output is correct