답안 #869917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
869917 2023-11-06T10:26:23 Z borisAngelov Passport (JOI23_passport) C++17
100 / 100
596 ms 103204 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 250005;
const int inf = 1000000000;

int n, q;

int minCity[4 * maxn];
int maxCity[4 * maxn];
int distFrom1[4 * maxn];
int distFromN[4 * maxn];
int distCombined[4 * maxn];

struct Edge
{
    int to;
    int w;

    Edge(int _to, int _w)
    {
        to = _to;
        w = _w;
    }
};

struct SegmentTree
{
    int root = 0;
    int numberNodes = 0;

    struct Node
    {
        int lc;
        int rc;
        vector<Edge> edges;
    };

    Node tree[4 * maxn];

    int build(int l, int r)
    {
        if (l == r)
        {
            return l;
        }

        int mid = (l + r) / 2;

        int leftChild = build(l, mid);
        int rightChild = build(mid + 1, r);

        int node = ++numberNodes;

        tree[node].lc = leftChild;
        tree[node].rc = rightChild;

        tree[leftChild].edges.push_back(Edge(node, 0));
        tree[rightChild].edges.push_back(Edge(node, 0));

        return node;
    }

    void addRangeEdge(int node, int l, int r, int ql, int qr, int toNode)
    {
        if (l > qr || r < ql)
        {
            return;
        }

        if (ql <= l && r <= qr)
        {
            tree[node].edges.push_back(Edge(toNode, 1));
            return;
        }

        int mid = (l + r) / 2;

        addRangeEdge(tree[node].lc, l, mid, ql, qr, toNode);
        addRangeEdge(tree[node].rc, mid + 1, r, ql, qr, toNode);
    }

    void addEdge(int x, int y, int w)
    {
        tree[x].edges.push_back(Edge(y, w));
    }

    void addRangeEdge(int to, int l, int r)
    {
        addRangeEdge(root, 1, n + 1, l, r, to);
    }

    void build()
    {
        numberNodes = n + 1;
        root = build(1, n + 1);
    }
};

SegmentTree tree;

void Dijkstra(int start, int dist[])
{
    for (int i = 1; i <= tree.numberNodes; ++i)
    {
        dist[i] = inf;
    }

    priority_queue<pair<int, int>> pq;

    pq.push(make_pair(0, start));
    dist[start] = 0;

    while (!pq.empty())
    {
        int node = pq.top().second;
        int curr = -pq.top().first;
        pq.pop();

        for (int i = 0; i < tree.tree[node].edges.size(); ++i)
        {
            int nextNode = tree.tree[node].edges[i].to;
            int path = curr + tree.tree[node].edges[i].w;

            if (dist[nextNode] > path)
            {
                dist[nextNode] = path;
                pq.push(make_pair(-path, nextNode));
            }
        }
    }
}

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    cin >> n;

    for (int i = 1; i <= n; ++i)
    {
        cin >> minCity[i] >> maxCity[i];
    }

    tree.build();

    for (int i = 1; i <= n; ++i)
    {
        tree.addRangeEdge(i, minCity[i], maxCity[i]);
    }

    Dijkstra(1, distFrom1);
    Dijkstra(n, distFromN);

    for (int i = 1; i <= n; ++i)
    {
        int weight = distFrom1[i] + distFromN[i];

        if (distFrom1[i] > 0 && distFromN[i] > 0)
        {
            --weight;
            // this is for the two issued passports from city i
        }

        tree.addEdge(n + 1, i, weight);
    }

    Dijkstra(n + 1, distCombined);

    cin >> q;

    for (int i = 1; i <= q; ++i)
    {
        int node;
        cin >> node;

        if (distCombined[node] > n)
        {
            cout << -1 << "\n";
        }
        else
        {
            cout << distCombined[node] << "\n";
        }
    }

    return 0;
}

Compilation message

passport.cpp: In function 'void Dijkstra(int, int*)':
passport.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for (int i = 0; i < tree.tree[node].edges.size(); ++i)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 41564 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 532 ms 95484 KB Output is correct
5 Correct 257 ms 75592 KB Output is correct
6 Correct 142 ms 70360 KB Output is correct
7 Correct 221 ms 92828 KB Output is correct
8 Correct 114 ms 86352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 41564 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41588 KB Output is correct
7 Correct 8 ms 41564 KB Output is correct
8 Correct 8 ms 41564 KB Output is correct
9 Correct 8 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 9 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41564 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 41564 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41588 KB Output is correct
7 Correct 8 ms 41564 KB Output is correct
8 Correct 8 ms 41564 KB Output is correct
9 Correct 8 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 9 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41564 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 11 ms 42076 KB Output is correct
17 Correct 11 ms 43868 KB Output is correct
18 Correct 11 ms 44160 KB Output is correct
19 Correct 11 ms 42100 KB Output is correct
20 Correct 9 ms 43868 KB Output is correct
21 Correct 10 ms 43868 KB Output is correct
22 Correct 9 ms 44300 KB Output is correct
23 Correct 10 ms 44120 KB Output is correct
24 Correct 10 ms 44124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 41564 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 8 ms 41564 KB Output is correct
5 Correct 8 ms 41564 KB Output is correct
6 Correct 8 ms 41588 KB Output is correct
7 Correct 8 ms 41564 KB Output is correct
8 Correct 8 ms 41564 KB Output is correct
9 Correct 8 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 9 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41564 KB Output is correct
14 Correct 8 ms 41564 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 11 ms 42076 KB Output is correct
17 Correct 11 ms 43868 KB Output is correct
18 Correct 11 ms 44160 KB Output is correct
19 Correct 11 ms 42100 KB Output is correct
20 Correct 9 ms 43868 KB Output is correct
21 Correct 10 ms 43868 KB Output is correct
22 Correct 9 ms 44300 KB Output is correct
23 Correct 10 ms 44120 KB Output is correct
24 Correct 10 ms 44124 KB Output is correct
25 Correct 8 ms 41560 KB Output is correct
26 Correct 8 ms 41564 KB Output is correct
27 Correct 11 ms 42076 KB Output is correct
28 Correct 11 ms 43864 KB Output is correct
29 Correct 11 ms 43864 KB Output is correct
30 Correct 10 ms 44080 KB Output is correct
31 Correct 11 ms 42076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 41564 KB Output is correct
2 Correct 8 ms 41564 KB Output is correct
3 Correct 8 ms 41564 KB Output is correct
4 Correct 532 ms 95484 KB Output is correct
5 Correct 257 ms 75592 KB Output is correct
6 Correct 142 ms 70360 KB Output is correct
7 Correct 221 ms 92828 KB Output is correct
8 Correct 114 ms 86352 KB Output is correct
9 Correct 8 ms 41564 KB Output is correct
10 Correct 8 ms 41564 KB Output is correct
11 Correct 8 ms 41564 KB Output is correct
12 Correct 8 ms 41564 KB Output is correct
13 Correct 8 ms 41564 KB Output is correct
14 Correct 8 ms 41588 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 8 ms 41564 KB Output is correct
17 Correct 8 ms 41564 KB Output is correct
18 Correct 8 ms 41564 KB Output is correct
19 Correct 9 ms 41564 KB Output is correct
20 Correct 8 ms 41564 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 8 ms 41564 KB Output is correct
24 Correct 11 ms 42076 KB Output is correct
25 Correct 11 ms 43868 KB Output is correct
26 Correct 11 ms 44160 KB Output is correct
27 Correct 11 ms 42100 KB Output is correct
28 Correct 9 ms 43868 KB Output is correct
29 Correct 10 ms 43868 KB Output is correct
30 Correct 9 ms 44300 KB Output is correct
31 Correct 10 ms 44120 KB Output is correct
32 Correct 10 ms 44124 KB Output is correct
33 Correct 8 ms 41560 KB Output is correct
34 Correct 8 ms 41564 KB Output is correct
35 Correct 11 ms 42076 KB Output is correct
36 Correct 11 ms 43864 KB Output is correct
37 Correct 11 ms 43864 KB Output is correct
38 Correct 10 ms 44080 KB Output is correct
39 Correct 11 ms 42076 KB Output is correct
40 Correct 596 ms 98556 KB Output is correct
41 Correct 286 ms 75972 KB Output is correct
42 Correct 373 ms 103204 KB Output is correct
43 Correct 351 ms 103116 KB Output is correct
44 Correct 169 ms 70604 KB Output is correct
45 Correct 192 ms 77744 KB Output is correct
46 Correct 106 ms 58172 KB Output is correct
47 Correct 325 ms 85304 KB Output is correct
48 Correct 233 ms 85524 KB Output is correct