답안 #747845

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747845 2023-05-24T22:02:49 Z stevancv Passport (JOI23_passport) C++14
48 / 100
374 ms 136336 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 1e9;
int n, l[N], r[N];
int dl[N], dr[N], dist[N], gde[N];
void Init(int node, int l, int r) {
    if (l == r) {gde[l] = node; return;}
    int mid = l + r >> 1;
    Init(2 * node, l, mid);
    Init(2 * node + 1, mid + 1, r);
}
vector<int> g[5 * N];
void Add(int node, int l, int r, int ql, int qr, int i) {
    if (r < ql || qr < l) return;
    if (ql <= l && r <= qr) {
        g[i].push_back(n + node);
        g[n + node].push_back(i);
        return;
    }
    int mid = l + r >> 1;
    Add(2 * node, l, mid, ql, qr, i);
    Add(2 * node + 1, mid + 1, r, ql, qr, i);
}
void Bfs(int* d) {
    set<pair<int, int>> st;
    for (int i = 1; i <= 5 * n; i++) st.insert({d[i], i});
    while (!st.empty()) {
        auto it = st.begin();
        int s = it->second;
        st.erase(it);
        if (s <= n) {
            for (int u = gde[s]; u >= 1; u /= 2) {
                if (d[u + n] > d[s]) {
                    st.erase({d[u + n], u + n});
                    d[u + n] = d[s];
                    st.insert({d[u + n], u + n});
                }
            }
            continue;
        }
        for (int u : g[s]) {
            if (d[u] > d[s] + 1) {
                st.erase({d[u], u});
                d[u] = d[s] + 1;
                st.insert({d[u], u});
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    Init(1, 1, n);
    for (int i = 1; i <= n; i++) {
        cin >> l[i] >> r[i];
        Add(1, 1, n, l[i], r[i], i);
    }
    for (int i = 1; i <= 5 * n; i++) dl[i] = dr[i] = dist[i] = inf;
    dl[1] = 0; dr[n] = 0;
    Bfs(dl); Bfs(dr);
    dl[1] = 1; dr[n] = 1;
    for (int i = 1; i <= n; i++) dist[i] = dl[i] + dr[i];
    Bfs(dist);
    for (int i = 1; i <= n; i++) {
        if (dist[i] >= inf) dist[i] = -1;
        else dist[i] -= 1;
    }
    int q; cin >> q;
    while (q--) {
        int x; cin >> x;
        cout << dist[x] << en;
    }
    return 0;
}

Compilation message

passport.cpp: In function 'void Init(int, int, int)':
passport.cpp:15:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   15 |     int mid = l + r >> 1;
      |               ~~^~~
passport.cpp: In function 'void Add(int, int, int, int, int, int)':
passport.cpp:27:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   27 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 14 ms 23764 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Runtime error 374 ms 136336 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23816 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23844 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 12 ms 23776 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23816 KB Output is correct
11 Correct 14 ms 23892 KB Output is correct
12 Correct 14 ms 23944 KB Output is correct
13 Correct 13 ms 23944 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 15 ms 23836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23816 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23844 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 12 ms 23776 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23816 KB Output is correct
11 Correct 14 ms 23892 KB Output is correct
12 Correct 14 ms 23944 KB Output is correct
13 Correct 13 ms 23944 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 15 ms 23836 KB Output is correct
16 Correct 24 ms 24788 KB Output is correct
17 Correct 23 ms 24788 KB Output is correct
18 Correct 25 ms 24972 KB Output is correct
19 Correct 25 ms 24968 KB Output is correct
20 Correct 26 ms 24832 KB Output is correct
21 Correct 22 ms 24788 KB Output is correct
22 Correct 20 ms 24704 KB Output is correct
23 Correct 24 ms 24764 KB Output is correct
24 Correct 21 ms 24848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23816 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23844 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 12 ms 23776 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23816 KB Output is correct
11 Correct 14 ms 23892 KB Output is correct
12 Correct 14 ms 23944 KB Output is correct
13 Correct 13 ms 23944 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 15 ms 23836 KB Output is correct
16 Correct 24 ms 24788 KB Output is correct
17 Correct 23 ms 24788 KB Output is correct
18 Correct 25 ms 24972 KB Output is correct
19 Correct 25 ms 24968 KB Output is correct
20 Correct 26 ms 24832 KB Output is correct
21 Correct 22 ms 24788 KB Output is correct
22 Correct 20 ms 24704 KB Output is correct
23 Correct 24 ms 24764 KB Output is correct
24 Correct 21 ms 24848 KB Output is correct
25 Correct 12 ms 23812 KB Output is correct
26 Correct 12 ms 23808 KB Output is correct
27 Correct 24 ms 24788 KB Output is correct
28 Correct 25 ms 24824 KB Output is correct
29 Correct 21 ms 24780 KB Output is correct
30 Correct 23 ms 24844 KB Output is correct
31 Correct 26 ms 24828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 14 ms 23764 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Runtime error 374 ms 136336 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -