Submission #404188

# Submission time Handle Problem Language Result Execution time Memory
404188 2021-05-13T23:30:13 Z timmyfeng Golf (JOI17_golf) C++17
100 / 100
5971 ms 916852 KB
#include <bits/stdc++.h>
using namespace std;

const int M = 1000000007;

struct segtree {
    segtree *left = nullptr, *right = nullptr;
    map<int, array<int, 2>> lines;

    void update(int a, int b, int x, int l, int r) {
        if (a <= l && r <= b) {
            lines[x] = {a, b};
        } else {
            int m = (l + r) / 2;

            if (a <= m) {
                if (!left) {
                    left = new segtree();
                }
                left->update(a, b, x, l, m);
            }

            if (b > m) {
                if (!right) {
                    right = new segtree();
                }
                right->update(a, b, x, m + 1, r);
            }
        }
    }

    void query(int x, int a, int b, vector<array<int, 3>> &out, int l, int r) {
        auto begin = lines.lower_bound(a), end = begin;
        while (end != lines.end() && end->first <= b) {
            out.push_back({end->first, end->second[0], end->second[1]});
            ++end;
        }
        lines.erase(begin, end);

        int m = (l + r) / 2;
        if (x <= m) {
            if (left) {
                left->query(x, a, b, out, l, m);
            }
        } else {
            if (right) {
                right->query(x, a, b, out, m + 1, r);
            }
        }
    }
};

vector<array<int, 3>> events[2];
set<array<int, 3>> visited[2];
segtree *nodes[2];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;

    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        int x1, x2, y1, y2;
        cin >> x1 >> x2 >> y1 >> y2;

        for (auto x : {x1, x2}) {
            events[0].push_back({y1, 1, x});
            events[0].push_back({y2, -1, x});

            events[1].push_back({x, 0, y1});
        }

        for (auto y : {y1, y2}) {
            events[1].push_back({x1, 1, y});
            events[1].push_back({x2, -1, y});

            events[0].push_back({y, 0, x1});
        }
    }

    events[0].push_back({ys, 0, xs});
    events[0].push_back({yt, 0, xt});
    events[1].push_back({xs, 0, ys});
    events[1].push_back({xt, 0, yt});

    for (int k = 0; k < 2; ++k) {
        sort(events[k].begin(), events[k].end());
        nodes[k] = new segtree();
        set<int> walls = {0, M};

        for (auto [a, t, b] : events[k]) {
            if (t == -1) {
                walls.erase(b);
            } else if (t == 1) {
                walls.insert(b);
            } else {
                auto it = walls.lower_bound(b);
                int r = *it, l = *(--it);
                nodes[k]->update(l, r, a, 0, M);
            }
        }
    }

    vector<array<int, 3>> init;
    nodes[0]->query(xs, ys, ys, init, 0, M);
    nodes[1]->query(ys, xs, xs, init, 0, M);

    queue<array<int, 5>> bfs;
    for (int k = 0; k < 2; ++k) {
        bfs.push({1, k, init[k][0], init[k][1], init[k][2]});
        visited[k].insert(init[k]);
    }

    while (!bfs.empty()) {
        auto [dist, o, a, b, c] = bfs.front();
        bfs.pop();

        if ((o == 0 && a == yt && b <= xt && xt <= c) ||
            (o == 1 && a == xt && b <= yt && yt <= c)) {
            cout << dist << "\n";
            exit(0);
        }

        vector<array<int, 3>> adj;
        nodes[1 - o]->query(a, b, c, adj, 0, M);

        for (auto [d, e, f] : adj) {
            if (visited[1 - o].count({d, e, f}) == 0) {
                bfs.push({dist + 1, 1 - o, d, e, f});
                visited[1 - o].insert({d, e, f});
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 12 ms 1964 KB Output is correct
6 Correct 10 ms 1840 KB Output is correct
7 Correct 12 ms 1836 KB Output is correct
8 Correct 12 ms 1900 KB Output is correct
9 Correct 12 ms 1848 KB Output is correct
10 Correct 12 ms 1868 KB Output is correct
11 Correct 12 ms 1868 KB Output is correct
12 Correct 9 ms 1868 KB Output is correct
13 Correct 13 ms 1868 KB Output is correct
14 Correct 12 ms 1924 KB Output is correct
15 Correct 4 ms 844 KB Output is correct
16 Correct 9 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 12 ms 1964 KB Output is correct
6 Correct 10 ms 1840 KB Output is correct
7 Correct 12 ms 1836 KB Output is correct
8 Correct 12 ms 1900 KB Output is correct
9 Correct 12 ms 1848 KB Output is correct
10 Correct 12 ms 1868 KB Output is correct
11 Correct 12 ms 1868 KB Output is correct
12 Correct 9 ms 1868 KB Output is correct
13 Correct 13 ms 1868 KB Output is correct
14 Correct 12 ms 1924 KB Output is correct
15 Correct 4 ms 844 KB Output is correct
16 Correct 9 ms 1484 KB Output is correct
17 Correct 27 ms 11212 KB Output is correct
18 Correct 25 ms 11076 KB Output is correct
19 Correct 25 ms 11420 KB Output is correct
20 Correct 27 ms 11420 KB Output is correct
21 Correct 24 ms 11384 KB Output is correct
22 Correct 27 ms 11340 KB Output is correct
23 Correct 24 ms 11460 KB Output is correct
24 Correct 29 ms 11300 KB Output is correct
25 Correct 20 ms 11308 KB Output is correct
26 Correct 27 ms 11340 KB Output is correct
27 Correct 11 ms 3404 KB Output is correct
28 Correct 18 ms 4848 KB Output is correct
29 Correct 22 ms 6104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 12 ms 1964 KB Output is correct
6 Correct 10 ms 1840 KB Output is correct
7 Correct 12 ms 1836 KB Output is correct
8 Correct 12 ms 1900 KB Output is correct
9 Correct 12 ms 1848 KB Output is correct
10 Correct 12 ms 1868 KB Output is correct
11 Correct 12 ms 1868 KB Output is correct
12 Correct 9 ms 1868 KB Output is correct
13 Correct 13 ms 1868 KB Output is correct
14 Correct 12 ms 1924 KB Output is correct
15 Correct 4 ms 844 KB Output is correct
16 Correct 9 ms 1484 KB Output is correct
17 Correct 27 ms 11212 KB Output is correct
18 Correct 25 ms 11076 KB Output is correct
19 Correct 25 ms 11420 KB Output is correct
20 Correct 27 ms 11420 KB Output is correct
21 Correct 24 ms 11384 KB Output is correct
22 Correct 27 ms 11340 KB Output is correct
23 Correct 24 ms 11460 KB Output is correct
24 Correct 29 ms 11300 KB Output is correct
25 Correct 20 ms 11308 KB Output is correct
26 Correct 27 ms 11340 KB Output is correct
27 Correct 11 ms 3404 KB Output is correct
28 Correct 18 ms 4848 KB Output is correct
29 Correct 22 ms 6104 KB Output is correct
30 Correct 3761 ms 892516 KB Output is correct
31 Correct 4195 ms 896732 KB Output is correct
32 Correct 5321 ms 892704 KB Output is correct
33 Correct 5882 ms 898464 KB Output is correct
34 Correct 4045 ms 916852 KB Output is correct
35 Correct 5971 ms 911544 KB Output is correct
36 Correct 3495 ms 905992 KB Output is correct
37 Correct 4907 ms 896904 KB Output is correct
38 Correct 4066 ms 912312 KB Output is correct
39 Correct 5372 ms 899296 KB Output is correct
40 Correct 516 ms 53016 KB Output is correct
41 Correct 575 ms 52896 KB Output is correct
42 Correct 521 ms 53460 KB Output is correct
43 Correct 514 ms 53432 KB Output is correct
44 Correct 501 ms 53952 KB Output is correct
45 Correct 513 ms 53984 KB Output is correct
46 Correct 509 ms 53940 KB Output is correct
47 Correct 510 ms 53928 KB Output is correct
48 Correct 505 ms 54000 KB Output is correct
49 Correct 503 ms 54056 KB Output is correct
50 Correct 25 ms 4804 KB Output is correct
51 Correct 26 ms 6036 KB Output is correct
52 Correct 21 ms 6028 KB Output is correct