Submission #952667

# Submission time Handle Problem Language Result Execution time Memory
952667 2024-03-24T13:02:08 Z LOLOLO Passport (JOI23_passport) C++17
100 / 100
655 ms 239924 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 2e5 + 10;
int lst, ss[N], n;
vector <pair <int, int>> ed[N * 6];

void build(int id, int l, int r) {
    if (l == r) {
        ss[l] = id;
        return;
    }

    int m = (l + r) / 2;
    ed[id * 2].pb({id, 0});
    ed[id * 2 + 1].pb({id, 0});
    build(id * 2, l, m);
    build(id * 2 + 1, m + 1, r);
}

void dfs(int id, int l, int r, int u, int v, int cur) {
    if (l > v || r < u)
        return;

    if (l >= u && r <= v) {
        ed[id].pb({cur, 0});
        return;
    }

    int m = (l + r) / 2;
    dfs(id * 2, l, m, u, v, cur);
    dfs(id * 2 + 1, m + 1, r, u, v, cur);
}

deque <int> dq[N];

void dijkstra(vector <int> &dis, vector <int>& save, vector <int>& cc) {
    vector <bool> used(lst + 1, 0);
    dis.assign(lst + 1, 2e8);
    priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> pq;

    for (auto x : save) {
        if (cc[x] <= n) {
            dis[x] = cc[x];
            dq[cc[x]].pb(x);
        } 
    }

    for (int i = 0; i <= n; i++) {
        while (sz(dq[i])) {
            int t = dq[i].front();
            dq[i].pop_front();

            if (used[t])
                continue;

            used[t] = 1;
            for (auto x : ed[t]) {
                if (used[x.f])
                    continue;

                if (x.s == 0) {
                    if (dis[x.f] > dis[t]) {
                        dis[x.f] = dis[t];
                        dq[dis[t]].push_back(x.f);
                    }
                } else {
                    if (dis[x.f] > dis[t] + 1) {
                        dis[x.f] = dis[t] + 1;
                        dq[dis[t] + 1].push_back(x.f);
                    }
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    lst = n * 4;
    build(1, 1, n);

    for (int i = 1; i <= n; i++) {
        int l, r;
        cin >> l >> r;
        lst++;
        ed[lst].pb({ss[i], 1});
        dfs(1, 1, n, l, r, lst);
    }

    vector <int> d1, d2, d3, cst(lst + 1, 0), id;
    vector <int> s1 = {ss[1]}, s2 = {ss[n]};
    dijkstra(d1, s1, cst);
    dijkstra(d2, s2, cst);

    for (int i = 1; i <= lst; i++) {
        id.pb(i);
        cst[i] = d1[i] + d2[i];
    }

    dijkstra(d3, id, cst);

    int q;
    cin >> q;

    while (q--) {
        int x;
        cin >> x;
        if (d3[ss[x]] >= 1e8) {
            cout << -1 << '\n';
        } else {
            cout << d3[ss[x]] << '\n';
        }
    }


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 84 ms 163156 KB Output is correct
2 Correct 78 ms 163156 KB Output is correct
3 Correct 75 ms 163156 KB Output is correct
4 Correct 580 ms 232668 KB Output is correct
5 Correct 310 ms 213088 KB Output is correct
6 Correct 250 ms 208572 KB Output is correct
7 Correct 179 ms 207080 KB Output is correct
8 Correct 171 ms 207908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 163152 KB Output is correct
2 Correct 77 ms 163080 KB Output is correct
3 Correct 78 ms 163152 KB Output is correct
4 Correct 75 ms 163156 KB Output is correct
5 Correct 73 ms 163232 KB Output is correct
6 Correct 75 ms 163152 KB Output is correct
7 Correct 80 ms 163156 KB Output is correct
8 Correct 75 ms 163152 KB Output is correct
9 Correct 75 ms 163156 KB Output is correct
10 Correct 76 ms 163224 KB Output is correct
11 Correct 88 ms 163304 KB Output is correct
12 Correct 103 ms 163152 KB Output is correct
13 Correct 85 ms 163156 KB Output is correct
14 Correct 77 ms 163244 KB Output is correct
15 Correct 75 ms 163264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 163152 KB Output is correct
2 Correct 77 ms 163080 KB Output is correct
3 Correct 78 ms 163152 KB Output is correct
4 Correct 75 ms 163156 KB Output is correct
5 Correct 73 ms 163232 KB Output is correct
6 Correct 75 ms 163152 KB Output is correct
7 Correct 80 ms 163156 KB Output is correct
8 Correct 75 ms 163152 KB Output is correct
9 Correct 75 ms 163156 KB Output is correct
10 Correct 76 ms 163224 KB Output is correct
11 Correct 88 ms 163304 KB Output is correct
12 Correct 103 ms 163152 KB Output is correct
13 Correct 85 ms 163156 KB Output is correct
14 Correct 77 ms 163244 KB Output is correct
15 Correct 75 ms 163264 KB Output is correct
16 Correct 76 ms 163920 KB Output is correct
17 Correct 86 ms 163852 KB Output is correct
18 Correct 86 ms 163924 KB Output is correct
19 Correct 87 ms 164044 KB Output is correct
20 Correct 84 ms 163820 KB Output is correct
21 Correct 75 ms 163636 KB Output is correct
22 Correct 81 ms 163664 KB Output is correct
23 Correct 96 ms 163920 KB Output is correct
24 Correct 83 ms 163752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 163152 KB Output is correct
2 Correct 77 ms 163080 KB Output is correct
3 Correct 78 ms 163152 KB Output is correct
4 Correct 75 ms 163156 KB Output is correct
5 Correct 73 ms 163232 KB Output is correct
6 Correct 75 ms 163152 KB Output is correct
7 Correct 80 ms 163156 KB Output is correct
8 Correct 75 ms 163152 KB Output is correct
9 Correct 75 ms 163156 KB Output is correct
10 Correct 76 ms 163224 KB Output is correct
11 Correct 88 ms 163304 KB Output is correct
12 Correct 103 ms 163152 KB Output is correct
13 Correct 85 ms 163156 KB Output is correct
14 Correct 77 ms 163244 KB Output is correct
15 Correct 75 ms 163264 KB Output is correct
16 Correct 76 ms 163920 KB Output is correct
17 Correct 86 ms 163852 KB Output is correct
18 Correct 86 ms 163924 KB Output is correct
19 Correct 87 ms 164044 KB Output is correct
20 Correct 84 ms 163820 KB Output is correct
21 Correct 75 ms 163636 KB Output is correct
22 Correct 81 ms 163664 KB Output is correct
23 Correct 96 ms 163920 KB Output is correct
24 Correct 83 ms 163752 KB Output is correct
25 Correct 85 ms 163156 KB Output is correct
26 Correct 79 ms 163028 KB Output is correct
27 Correct 83 ms 163948 KB Output is correct
28 Correct 83 ms 163664 KB Output is correct
29 Correct 84 ms 163704 KB Output is correct
30 Correct 77 ms 163652 KB Output is correct
31 Correct 88 ms 163668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 163156 KB Output is correct
2 Correct 78 ms 163156 KB Output is correct
3 Correct 75 ms 163156 KB Output is correct
4 Correct 580 ms 232668 KB Output is correct
5 Correct 310 ms 213088 KB Output is correct
6 Correct 250 ms 208572 KB Output is correct
7 Correct 179 ms 207080 KB Output is correct
8 Correct 171 ms 207908 KB Output is correct
9 Correct 75 ms 163152 KB Output is correct
10 Correct 77 ms 163080 KB Output is correct
11 Correct 78 ms 163152 KB Output is correct
12 Correct 75 ms 163156 KB Output is correct
13 Correct 73 ms 163232 KB Output is correct
14 Correct 75 ms 163152 KB Output is correct
15 Correct 80 ms 163156 KB Output is correct
16 Correct 75 ms 163152 KB Output is correct
17 Correct 75 ms 163156 KB Output is correct
18 Correct 76 ms 163224 KB Output is correct
19 Correct 88 ms 163304 KB Output is correct
20 Correct 103 ms 163152 KB Output is correct
21 Correct 85 ms 163156 KB Output is correct
22 Correct 77 ms 163244 KB Output is correct
23 Correct 75 ms 163264 KB Output is correct
24 Correct 76 ms 163920 KB Output is correct
25 Correct 86 ms 163852 KB Output is correct
26 Correct 86 ms 163924 KB Output is correct
27 Correct 87 ms 164044 KB Output is correct
28 Correct 84 ms 163820 KB Output is correct
29 Correct 75 ms 163636 KB Output is correct
30 Correct 81 ms 163664 KB Output is correct
31 Correct 96 ms 163920 KB Output is correct
32 Correct 83 ms 163752 KB Output is correct
33 Correct 85 ms 163156 KB Output is correct
34 Correct 79 ms 163028 KB Output is correct
35 Correct 83 ms 163948 KB Output is correct
36 Correct 83 ms 163664 KB Output is correct
37 Correct 84 ms 163704 KB Output is correct
38 Correct 77 ms 163652 KB Output is correct
39 Correct 88 ms 163668 KB Output is correct
40 Correct 655 ms 234424 KB Output is correct
41 Correct 333 ms 216056 KB Output is correct
42 Correct 456 ms 239728 KB Output is correct
43 Correct 415 ms 239924 KB Output is correct
44 Correct 297 ms 211176 KB Output is correct
45 Correct 354 ms 215224 KB Output is correct
46 Correct 168 ms 182248 KB Output is correct
47 Correct 395 ms 217992 KB Output is correct
48 Correct 379 ms 224928 KB Output is correct