Submission #1086560

# Submission time Handle Problem Language Result Execution time Memory
1086560 2024-09-11T03:31:06 Z adaawf Passport (JOI23_passport) C++17
100 / 100
616 ms 96536 KB
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
vector<pair<int, int>> t[1000005];
int f[1000005], g[1000005], d[1000005], n;
void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[tl].push_back({v + n, 0});
        return;
    }
    int mid = (tl + tr) / 2;
    build(v * 2, tl, mid);
    build(v * 2 + 1, mid + 1, tr);
    t[v * 2 + n].push_back({v + n, 0});
    t[v * 2 + 1 + n].push_back({v + n, 0});
}
void update(int v, int tl, int tr, int l, int r, int x) {
    if (l > r) return;
    if (tl == l && tr == r) {
        t[v + n].push_back({x, 1});
        return;
    }
    int mid = (tl + tr) / 2;
    update(v * 2, tl, mid, l, min(r, mid), x);
    update(v * 2 + 1, mid + 1, tr, max(l, mid + 1), r, x);
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    build(1, 1, n);
    for (int i = 1; i <= n; i++) {
        int l, r;
        cin >> l >> r;
        update(1, 1, n, l, r, i);
    }
    priority_queue<pair<int, int>> q;
    memset(f, 0x3f, sizeof f);
    memset(g, 0x3f, sizeof g);
    memset(d, 0x3f, sizeof d);
    f[1] = 0; g[n] = 0;
    q.push({0, 1});
    while (!q.empty()) {
        pair<int, int> p = q.top();
        q.pop();
        int x = -p.first, u = p.second;
        if (f[u] != x) continue;
        for (auto w : t[u]) {
            if (f[w.first] > f[u] + w.second) {
                f[w.first] = f[u] + w.second;
                q.push({-f[w.first], w.first});
            }
        }
    }
    q.push({0, n});
    while (!q.empty()) {
        pair<int, int> p = q.top();
        q.pop();
        int x = -p.first, u = p.second;
        if (g[u] != x) continue;
        for (auto w : t[u]) {
            if (g[w.first] > g[u] + w.second) {
                g[w.first] = g[u] + w.second;
                q.push({-g[w.first], w.first});
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (i == 1 || i == n) {
            d[i] = f[i] + g[i];
        }
        else d[i] = f[i] + g[i] - 1;
        q.push({-d[i], i});
    }
    while (!q.empty()) {
        pair<int, int> p = q.top();
        q.pop();
        int x = -p.first, u = p.second;
        if (d[u] != x) continue;
        for (auto w : t[u]) {
            if (d[w.first] > d[u] + w.second) {
                d[w.first] = d[u] + w.second;
                q.push({-d[w.first], w.first});
            }
        }
    }
    int qq;
    cin >> qq;
    for (int jj = 0; jj < qq; jj++) {
        int x;
        cin >> x;
        if (d[x] > 1e9) cout << -1;
        else cout << d[x];
        cout << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35688 KB Output is correct
3 Correct 7 ms 35676 KB Output is correct
4 Correct 597 ms 87980 KB Output is correct
5 Correct 301 ms 68044 KB Output is correct
6 Correct 161 ms 62148 KB Output is correct
7 Correct 198 ms 60096 KB Output is correct
8 Correct 130 ms 65556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 6 ms 35536 KB Output is correct
3 Correct 8 ms 35676 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 8 ms 35672 KB Output is correct
6 Correct 7 ms 35676 KB Output is correct
7 Correct 7 ms 35456 KB Output is correct
8 Correct 6 ms 35672 KB Output is correct
9 Correct 7 ms 35676 KB Output is correct
10 Correct 7 ms 35676 KB Output is correct
11 Correct 7 ms 35676 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35676 KB Output is correct
14 Correct 7 ms 35676 KB Output is correct
15 Correct 7 ms 35676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 6 ms 35536 KB Output is correct
3 Correct 8 ms 35676 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 8 ms 35672 KB Output is correct
6 Correct 7 ms 35676 KB Output is correct
7 Correct 7 ms 35456 KB Output is correct
8 Correct 6 ms 35672 KB Output is correct
9 Correct 7 ms 35676 KB Output is correct
10 Correct 7 ms 35676 KB Output is correct
11 Correct 7 ms 35676 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35676 KB Output is correct
14 Correct 7 ms 35676 KB Output is correct
15 Correct 7 ms 35676 KB Output is correct
16 Correct 13 ms 36176 KB Output is correct
17 Correct 10 ms 35932 KB Output is correct
18 Correct 11 ms 36312 KB Output is correct
19 Correct 11 ms 36188 KB Output is correct
20 Correct 10 ms 36036 KB Output is correct
21 Correct 8 ms 35932 KB Output is correct
22 Correct 8 ms 36012 KB Output is correct
23 Correct 9 ms 35928 KB Output is correct
24 Correct 9 ms 36188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 6 ms 35536 KB Output is correct
3 Correct 8 ms 35676 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 8 ms 35672 KB Output is correct
6 Correct 7 ms 35676 KB Output is correct
7 Correct 7 ms 35456 KB Output is correct
8 Correct 6 ms 35672 KB Output is correct
9 Correct 7 ms 35676 KB Output is correct
10 Correct 7 ms 35676 KB Output is correct
11 Correct 7 ms 35676 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35676 KB Output is correct
14 Correct 7 ms 35676 KB Output is correct
15 Correct 7 ms 35676 KB Output is correct
16 Correct 13 ms 36176 KB Output is correct
17 Correct 10 ms 35932 KB Output is correct
18 Correct 11 ms 36312 KB Output is correct
19 Correct 11 ms 36188 KB Output is correct
20 Correct 10 ms 36036 KB Output is correct
21 Correct 8 ms 35932 KB Output is correct
22 Correct 8 ms 36012 KB Output is correct
23 Correct 9 ms 35928 KB Output is correct
24 Correct 9 ms 36188 KB Output is correct
25 Correct 7 ms 35676 KB Output is correct
26 Correct 7 ms 35480 KB Output is correct
27 Correct 10 ms 36188 KB Output is correct
28 Correct 10 ms 36144 KB Output is correct
29 Correct 9 ms 35932 KB Output is correct
30 Correct 10 ms 35928 KB Output is correct
31 Correct 11 ms 36016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35688 KB Output is correct
3 Correct 7 ms 35676 KB Output is correct
4 Correct 597 ms 87980 KB Output is correct
5 Correct 301 ms 68044 KB Output is correct
6 Correct 161 ms 62148 KB Output is correct
7 Correct 198 ms 60096 KB Output is correct
8 Correct 130 ms 65556 KB Output is correct
9 Correct 8 ms 35676 KB Output is correct
10 Correct 6 ms 35536 KB Output is correct
11 Correct 8 ms 35676 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35672 KB Output is correct
14 Correct 7 ms 35676 KB Output is correct
15 Correct 7 ms 35456 KB Output is correct
16 Correct 6 ms 35672 KB Output is correct
17 Correct 7 ms 35676 KB Output is correct
18 Correct 7 ms 35676 KB Output is correct
19 Correct 7 ms 35676 KB Output is correct
20 Correct 8 ms 35676 KB Output is correct
21 Correct 8 ms 35676 KB Output is correct
22 Correct 7 ms 35676 KB Output is correct
23 Correct 7 ms 35676 KB Output is correct
24 Correct 13 ms 36176 KB Output is correct
25 Correct 10 ms 35932 KB Output is correct
26 Correct 11 ms 36312 KB Output is correct
27 Correct 11 ms 36188 KB Output is correct
28 Correct 10 ms 36036 KB Output is correct
29 Correct 8 ms 35932 KB Output is correct
30 Correct 8 ms 36012 KB Output is correct
31 Correct 9 ms 35928 KB Output is correct
32 Correct 9 ms 36188 KB Output is correct
33 Correct 7 ms 35676 KB Output is correct
34 Correct 7 ms 35480 KB Output is correct
35 Correct 10 ms 36188 KB Output is correct
36 Correct 10 ms 36144 KB Output is correct
37 Correct 9 ms 35932 KB Output is correct
38 Correct 10 ms 35928 KB Output is correct
39 Correct 11 ms 36016 KB Output is correct
40 Correct 616 ms 90352 KB Output is correct
41 Correct 328 ms 70328 KB Output is correct
42 Correct 413 ms 96536 KB Output is correct
43 Correct 420 ms 96172 KB Output is correct
44 Correct 194 ms 64388 KB Output is correct
45 Correct 233 ms 71744 KB Output is correct
46 Correct 113 ms 47820 KB Output is correct
47 Correct 334 ms 72004 KB Output is correct
48 Correct 250 ms 79668 KB Output is correct