답안 #1015226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1015226 2024-07-06T07:41:11 Z aufan Passport (JOI23_passport) C++17
100 / 100
290 ms 73084 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

const int INFF = 1e18;

int32_t main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        int n;
        cin >> n;

        vector<int> l(n), r(n);
        for (int i = 0; i < n; i++) cin >> l[i] >> r[i];

        // build somewhat iterative segtree-like segments (?)
        int sz = 1;
        while (sz < n) {
                sz *= 2;
        }

        vector<vector<int>> v(2 * sz);
        for (int i = 0; i < n; i++) {
                int cl = l[i] + sz - 1;
                int cr = r[i] + sz;

                while (cl < cr) {
                        if (cl & 1) {
                                v[cl++].push_back(i);
                        }

                        if (cr & 1) {
                                v[--cr].push_back(i);
                        }

                        cl >>= 1;
                        cr >>= 1;
                }
        }

        auto bfs = [&](int x) { 
                vector<int> dist(n, INFF), q(1, x), vis(n), used(2 * sz);
                dist[x] = 0;
                for (int i = 0; i < (int)q.size(); i++) {
                        int x = q[i];

                        if (!vis[x]) {
                                vis[x] = 1;
                                int id = x + sz;
                                while (id >= 1 && !used[id]) {
                                        used[id] = 1;
                                        for (auto z : v[id]) {
                                                if (dist[z] == INFF) {
                                                        dist[z] = dist[x] + 1;
                                                        q.push_back(z);
                                                }
                                        }

                                        id >>= 1;
                                }
                        }
                }

                return dist;
        };

        vector<int> df = bfs(0), db = bfs(n - 1), dc(n, INFF);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        df[0] = db[n - 1] = 1;
        for (int i = 0; i < n; i++) {
                dc[i] = min(dc[i], df[i] + db[i]);
                pq.push({dc[i], i});
        }

        vector<int> vis(n), used(2 * sz);
        while (!pq.empty()) {
                auto [w, x] = pq.top();
                pq.pop();

                if (!vis[x]) {
                        vis[x] = 1;
                        int id = x + sz;
                        while (id >= 1 && !used[id]) {
                                used[id] = 1;
                                for (auto z : v[id]) {
                                        if (dc[x] + 1 < dc[z]) {
                                                dc[z] = dc[x] + 1;
                                                pq.push({dc[z], z});
                                        }
                                }

                                id >>= 1;
                        }
                }
        }

        vector<int> ans(n);
        for (int i = 0; i < n; i++) {
                ans[i] = (dc[i] == INFF ? -1 : dc[i] - 1);
        }

        int q;
        cin >> q;

        for (int i = 0; i < q; i++) {
                int x;
                cin >> x;
                --x;

                cout << ans[x] << '\n';
        }
        
        return 0;
}
# 결과 실행 시간 메모리 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 258 ms 66872 KB Output is correct
5 Correct 140 ms 47396 KB Output is correct
6 Correct 80 ms 43776 KB Output is correct
7 Correct 83 ms 50096 KB Output is correct
8 Correct 61 ms 49932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 2 ms 1116 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 2 ms 1220 KB Output is correct
20 Correct 1 ms 980 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 2 ms 1116 KB Output is correct
24 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 2 ms 1116 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 2 ms 1220 KB Output is correct
20 Correct 1 ms 980 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 2 ms 1116 KB Output is correct
24 Correct 1 ms 1116 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 2 ms 1116 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 1 ms 1116 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 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 258 ms 66872 KB Output is correct
5 Correct 140 ms 47396 KB Output is correct
6 Correct 80 ms 43776 KB Output is correct
7 Correct 83 ms 50096 KB Output is correct
8 Correct 61 ms 49932 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 348 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 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 3 ms 1228 KB Output is correct
27 Correct 2 ms 1220 KB Output is correct
28 Correct 1 ms 980 KB Output is correct
29 Correct 1 ms 1116 KB Output is correct
30 Correct 1 ms 1116 KB Output is correct
31 Correct 2 ms 1116 KB Output is correct
32 Correct 1 ms 1116 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 1 ms 1116 KB Output is correct
38 Correct 2 ms 1116 KB Output is correct
39 Correct 2 ms 1116 KB Output is correct
40 Correct 290 ms 73084 KB Output is correct
41 Correct 186 ms 50012 KB Output is correct
42 Correct 207 ms 72096 KB Output is correct
43 Correct 187 ms 72024 KB Output is correct
44 Correct 94 ms 46304 KB Output is correct
45 Correct 129 ms 52008 KB Output is correct
46 Correct 48 ms 21928 KB Output is correct
47 Correct 139 ms 54624 KB Output is correct
48 Correct 137 ms 57340 KB Output is correct