Submission #889882

# Submission time Handle Problem Language Result Execution time Memory
889882 2023-12-20T08:56:00 Z shivensinha4 Passport (JOI23_passport) C++17
48 / 100
2000 ms 782388 KB
#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'

const int INF = 1e9, BS = 300;

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

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

    int n; cin >> n;
    const int blocks = (n + BS - 1) / BS;
    vector<ii> seg(n);
    vector<vi> can_reach(n), can_reach_b(blocks);
    for_(i, 0, n){
        cin >> seg[i][0] >> seg[i][1], seg[i][0]--, seg[i][1]--;

        int pt = seg[i][0];
        while (pt % BS != 0 and pt <= seg[i][1]) {
            can_reach[pt].push_back(i);
            pt++;
        }
        while (pt + BS <= seg[i][1]) {
            can_reach_b[pt / BS].push_back(i);
            pt += BS;
        }
        while (pt <= seg[i][1]) {
            can_reach[pt].push_back(i);
            pt++;
        }
    }

    function<vi(int)> bfs = [&] (int s) {
        vi dist(n, INF);
        auto cr = can_reach, crb = can_reach_b;

        dist[s] = 0;
        queue<int> q;
        q.push(s);
        while (q.size()) {
            auto p = q.front(); q.pop();
            for (auto i: cr[p]) if (dist[i] == INF) {
                dist[i] = dist[p] + 1;
                q.push(i);
            }
            for (auto i: crb[p / BS]) {
                if (dist[i] == INF) {
                    dist[i] = dist[p] + 1;
                    q.push(i);
                }
            }
            crb[p / BS].clear();
        }
        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]--;

    priority_queue<ii, vector<ii>, greater<ii>> pq;
    for_(i, 0, n) pq.push({ans[i], i});
    while (pq.size()) {
        auto [dist, p] = pq.top(); pq.pop();
        if (dist != ans[p]) continue;
        for (auto i: can_reach[p]) {
            if (ans[i] > ans[p] + 1) {
                ans[i] = ans[p] + 1;
                pq.push({ans[i], i});
            }
        }
        for (auto i: can_reach_b[p / BS]) {
            if (ans[i] > ans[p] + 1) {
                ans[i] = ans[p] + 1;
                pq.push({ans[i], i});
            }
        }
        can_reach_b[p / BS].clear();
    }

    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 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Execution timed out 2117 ms 782388 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 6 ms 4900 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 8 ms 7516 KB Output is correct
19 Correct 8 ms 7516 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 3 ms 2180 KB Output is correct
22 Correct 6 ms 5896 KB Output is correct
23 Correct 4 ms 3228 KB Output is correct
24 Correct 4 ms 4188 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 6 ms 4900 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 8 ms 7516 KB Output is correct
19 Correct 8 ms 7516 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 3 ms 2180 KB Output is correct
22 Correct 6 ms 5896 KB Output is correct
23 Correct 4 ms 3228 KB Output is correct
24 Correct 4 ms 4188 KB Output is correct
25 Correct 1 ms 500 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 7 ms 5212 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 4 ms 2140 KB Output is correct
31 Correct 4 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Execution timed out 2117 ms 782388 KB Time limit exceeded
5 Halted 0 ms 0 KB -