답안 #768708

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768708 2023-06-28T13:02:25 Z danikoynov Passport (JOI23_passport) C++14
54 / 100
2000 ms 142468 KB
#include<bits/stdc++.h>
#define endl '\n'
 
using namespace std;
typedef long long ll;
 
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
 
const int maxn = 5e5 + 10;
 
struct graph
{
    vector < pair < int, int > > adj[maxn * 5];
    int dist[maxn * 5], lf[maxn * 5], rf[maxn * 5];
    int fict, root, n;
    graph() {};
 
    int build(int left, int right)
    {
        ///cout << left << " :: " << right << " " << fict << endl;
        if (left == right)
            return left;
        int mid = (left + right) / 2;
        int left_child = build(left, mid);
        int right_child = build(mid + 1, right);
        int ver = ++ fict;
        adj[left_child].push_back({ver, 0});
        adj[right_child].push_back({ver, 0});
        //cout << left_child << " " << ver << " " << 0 << endl;
        //cout << right_child << " " << ver << " " << 0 << endl;
        lf[ver] = left_child;
        rf[ver] = right_child;
        return ver;
    }
    graph(int _n)
    {
        n = _n;
        fict = n;
        root = build(1, n);
 
    }
 
    void range_update(int node, int l, int r, int ql, int qr, int v)
    {
        if (l > qr || r < ql)
        return;
 
        if (l >= ql && r <= qr)
        {
            adj[node].push_back({v, 1});
            ///cout << "make edge " << node << " " << l << " " << r << " " << v << endl;
            ///cout << node << " " << v << " " << 1 << endl;
            return;
        }
 
        int m = (l + r) / 2;
        range_update(lf[node], l, m, ql, qr, v);
        range_update(rf[node], m + 1, r, ql, qr, v);
    }
    void add_range_edge(int l, int r, int v)
    {
        range_update(root, 1, n, l, r, v);
    }
    void add_edge(int v, int u, int w)
    {
        adj[v].push_back({u, w});
        ///adj[u].push_back({v, w});
    }
 
    void bfs(int st)
    {
        for (int i = 1; i <= fict; i ++)
        {
            dist[i] = 1e9;
        }
 
        deque < int > q;
        dist[st] = 0;
        q.push_back(st);
        while(!q.empty())
        {
            int v = q.front();
            q.pop_front();
            ///cout << "here " << v << " " << dist[v] << endl;
            for (pair < int, int > nb : adj[v])
            {
                if (dist[nb.first] > dist[v] + nb.second)
                {
                    dist[nb.first] = dist[v] + nb.second;
                    if (nb.second == 0)
                        q.push_front(nb.first);
                    else
                        q.push_back(nb.first);
                }
            }
        }
    }
};
 
int n, q, l[maxn], r[maxn];
int a[maxn], b[maxn];
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        cin >> l[i] >> r[i];
    }
 
    graph st(n + 1);
    for (int i = 1; i <= n; i ++)
    {
        st.add_range_edge(l[i], r[i], i);
        ///for (int j = l[i]; j <= r[i]; j ++)
            ///st.add_edge(j, i, 1);
            ///cout << j << " " << i << " " << 1 << endl;
    }
 
    st.bfs(1);
    for (int i = 1; i <= n; i ++)
        a[i] = st.dist[i];
    st.bfs(n);
    for (int i = 1; i <= n; i ++)
        b[i] = st.dist[i];
 
    /**for (int i = 1; i <= n; i ++)
        cout << b[i] << " ";
    cout << endl;
    return;*/
    ///cout << "------------" << endl;
    for (int i = 1; i <= n; i ++)
    {
        int edge = a[i] + b[i];
        if (a[i] > 0 && b[i] > 0)
            edge --;
        st.add_edge(n + 1, i, edge);
        ///cout << n + 1 << " " << i << " " << a[i] << " " << b[i] << endl;
    }
 
 
    st.bfs(n + 1);
    /**for (int i = 1; i <= n; i ++)
        cout << st.dist[i] << " ";
    cout << endl;*/
    cin >> q;
    for (int i = 1; i <= q; i ++)
    {
        int x;
        cin >> x;
        int ans = st.dist[x];
        if (ans > n)
            cout << -1 << endl;
        else
        cout << ans << endl;
    }
 
 
 
}
 
int main()
{
    speed();
    ///freopen("text.txt", "r", stdin);
    solve();
    return 0;
}
/**
9
1 1
2 2
3 3
1 4
2 8
5 7
4 9
8 8
9 9
1
6
 
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 88276 KB Output is correct
2 Correct 38 ms 88268 KB Output is correct
3 Correct 37 ms 88380 KB Output is correct
4 Correct 435 ms 135120 KB Output is correct
5 Correct 206 ms 114632 KB Output is correct
6 Correct 151 ms 109524 KB Output is correct
7 Correct 163 ms 132684 KB Output is correct
8 Correct 124 ms 128268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 88380 KB Output is correct
2 Correct 37 ms 88288 KB Output is correct
3 Correct 37 ms 88396 KB Output is correct
4 Correct 38 ms 88356 KB Output is correct
5 Correct 38 ms 88392 KB Output is correct
6 Correct 38 ms 88332 KB Output is correct
7 Correct 38 ms 88328 KB Output is correct
8 Correct 37 ms 88280 KB Output is correct
9 Correct 38 ms 88388 KB Output is correct
10 Correct 38 ms 88404 KB Output is correct
11 Correct 37 ms 88404 KB Output is correct
12 Correct 39 ms 88340 KB Output is correct
13 Correct 37 ms 88396 KB Output is correct
14 Correct 37 ms 88396 KB Output is correct
15 Correct 41 ms 88512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 88380 KB Output is correct
2 Correct 37 ms 88288 KB Output is correct
3 Correct 37 ms 88396 KB Output is correct
4 Correct 38 ms 88356 KB Output is correct
5 Correct 38 ms 88392 KB Output is correct
6 Correct 38 ms 88332 KB Output is correct
7 Correct 38 ms 88328 KB Output is correct
8 Correct 37 ms 88280 KB Output is correct
9 Correct 38 ms 88388 KB Output is correct
10 Correct 38 ms 88404 KB Output is correct
11 Correct 37 ms 88404 KB Output is correct
12 Correct 39 ms 88340 KB Output is correct
13 Correct 37 ms 88396 KB Output is correct
14 Correct 37 ms 88396 KB Output is correct
15 Correct 41 ms 88512 KB Output is correct
16 Correct 39 ms 88832 KB Output is correct
17 Correct 46 ms 88748 KB Output is correct
18 Correct 41 ms 88908 KB Output is correct
19 Correct 41 ms 88884 KB Output is correct
20 Correct 39 ms 88652 KB Output is correct
21 Correct 57 ms 88664 KB Output is correct
22 Correct 40 ms 88908 KB Output is correct
23 Correct 41 ms 88788 KB Output is correct
24 Correct 40 ms 88780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 88380 KB Output is correct
2 Correct 37 ms 88288 KB Output is correct
3 Correct 37 ms 88396 KB Output is correct
4 Correct 38 ms 88356 KB Output is correct
5 Correct 38 ms 88392 KB Output is correct
6 Correct 38 ms 88332 KB Output is correct
7 Correct 38 ms 88328 KB Output is correct
8 Correct 37 ms 88280 KB Output is correct
9 Correct 38 ms 88388 KB Output is correct
10 Correct 38 ms 88404 KB Output is correct
11 Correct 37 ms 88404 KB Output is correct
12 Correct 39 ms 88340 KB Output is correct
13 Correct 37 ms 88396 KB Output is correct
14 Correct 37 ms 88396 KB Output is correct
15 Correct 41 ms 88512 KB Output is correct
16 Correct 39 ms 88832 KB Output is correct
17 Correct 46 ms 88748 KB Output is correct
18 Correct 41 ms 88908 KB Output is correct
19 Correct 41 ms 88884 KB Output is correct
20 Correct 39 ms 88652 KB Output is correct
21 Correct 57 ms 88664 KB Output is correct
22 Correct 40 ms 88908 KB Output is correct
23 Correct 41 ms 88788 KB Output is correct
24 Correct 40 ms 88780 KB Output is correct
25 Correct 45 ms 88268 KB Output is correct
26 Correct 39 ms 88388 KB Output is correct
27 Correct 50 ms 88748 KB Output is correct
28 Correct 41 ms 88660 KB Output is correct
29 Correct 42 ms 88704 KB Output is correct
30 Correct 56 ms 88780 KB Output is correct
31 Correct 42 ms 88788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 88276 KB Output is correct
2 Correct 38 ms 88268 KB Output is correct
3 Correct 37 ms 88380 KB Output is correct
4 Correct 435 ms 135120 KB Output is correct
5 Correct 206 ms 114632 KB Output is correct
6 Correct 151 ms 109524 KB Output is correct
7 Correct 163 ms 132684 KB Output is correct
8 Correct 124 ms 128268 KB Output is correct
9 Correct 36 ms 88380 KB Output is correct
10 Correct 37 ms 88288 KB Output is correct
11 Correct 37 ms 88396 KB Output is correct
12 Correct 38 ms 88356 KB Output is correct
13 Correct 38 ms 88392 KB Output is correct
14 Correct 38 ms 88332 KB Output is correct
15 Correct 38 ms 88328 KB Output is correct
16 Correct 37 ms 88280 KB Output is correct
17 Correct 38 ms 88388 KB Output is correct
18 Correct 38 ms 88404 KB Output is correct
19 Correct 37 ms 88404 KB Output is correct
20 Correct 39 ms 88340 KB Output is correct
21 Correct 37 ms 88396 KB Output is correct
22 Correct 37 ms 88396 KB Output is correct
23 Correct 41 ms 88512 KB Output is correct
24 Correct 39 ms 88832 KB Output is correct
25 Correct 46 ms 88748 KB Output is correct
26 Correct 41 ms 88908 KB Output is correct
27 Correct 41 ms 88884 KB Output is correct
28 Correct 39 ms 88652 KB Output is correct
29 Correct 57 ms 88664 KB Output is correct
30 Correct 40 ms 88908 KB Output is correct
31 Correct 41 ms 88788 KB Output is correct
32 Correct 40 ms 88780 KB Output is correct
33 Correct 45 ms 88268 KB Output is correct
34 Correct 39 ms 88388 KB Output is correct
35 Correct 50 ms 88748 KB Output is correct
36 Correct 41 ms 88660 KB Output is correct
37 Correct 42 ms 88704 KB Output is correct
38 Correct 56 ms 88780 KB Output is correct
39 Correct 42 ms 88788 KB Output is correct
40 Correct 463 ms 135412 KB Output is correct
41 Correct 224 ms 116096 KB Output is correct
42 Execution timed out 2089 ms 142468 KB Time limit exceeded
43 Halted 0 ms 0 KB -