답안 #1068561

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1068561 2024-08-21T10:38:25 Z juicy Golf (JOI17_golf) C++17
100 / 100
3447 ms 326316 KB
// https://oj.uz/submission/404188

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

struct Segtree {
    int n;
    vector<set<array<int, 3>>> s;

    Segtree() {};

    Segtree(int n): n(n), s(4 << __lg(n)) {};

    void add(int i, int u, int v, int id, int l, int r) {
        if (u <= l && r <= v) {
            s[id].insert({i, u, v});
            return;
        }
        int md = (l + r) / 2;
        if (u <= md) {
            add(i, u, v, id * 2, l, md);
        }
        if (md < v) {
            add(i, u, v, id * 2 + 1, md + 1, r);
        }
    }

    void add(int i, int l, int r) {
        add(i, l, r, 1, 1, n);
    }

    void gather(int i, int u, int v, vector<array<int, 3>> &graph, int id, int l, int r) {
        for (auto it = s[id].lower_bound(array<int, 3>{u}); it != s[id].end() && (*it)[0] <= v; it = s[id].erase(it)) {
            graph.push_back(*it);
        }
        if (l == r) {
            return;
        }
        int md = (l + r) / 2;
        if (i <= md) {
            gather(i, u, v, graph, id * 2, l, md);
        } else {
            gather(i, u, v, graph, id * 2 + 1, md + 1, r);
        }
    }

    vector<array<int, 3>> qry(int i, int u, int v) {
        vector<array<int, 3>> graph;
        gather(i, u, v, graph, 1, 1, n);
        return graph;
    }
} tree[2];

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    
    const int inf = 1e9 + 7;
    
    array<vector<int>, 2> comp;
    for (auto it : {0, 1}) {
        comp[it] = vector{0, inf};
    }
    auto add = [&](array<int, 2> pt) {
        for (auto it : {0, 1}) {
            comp[it].push_back(pt[it]);
        }
    };
    auto get = [&](array<int, 2> x) {
        for (auto it : {0, 1}) {
            x[it] = lower_bound(comp[it].begin(), comp[it].end(), x[it]) - comp[it].begin() + 1;
        }
        return x;
    };
    array<int, 2> s, t;
    cin >> s[0] >> s[1] >> t[0] >> t[1];
    add(s), add(t);
    int n; cin >> n;
    array<vector<array<int, 3>>, 2> events;
    while (n--) {
        array<int, 2> a, b;
        cin >> a[0] >> b[0] >> a[1] >> b[1];
        add(a); add(b);
        for (auto it : {0, 1}) {
            for (auto x : {a[it], b[it]}) {
                events[it ^ 1].push_back({a[it ^ 1], 1, x});
                events[it ^ 1].push_back({b[it ^ 1], -1, x});
                events[it].push_back({x, 0, a[it ^ 1]});
            }
        }
    }
    for (auto it : {0, 1}) {
        sort(comp[it].begin(), comp[it].end());
        comp[it].erase(unique(comp[it].begin(), comp[it].end()), comp[it].end());
    }
    for (auto it : {0, 1}) {
        for (auto &x : events[it]) {
            x[0] = lower_bound(comp[it].begin(), comp[it].end(), x[0]) - comp[it].begin() + 1;
            x[2] = lower_bound(comp[it ^ 1].begin(), comp[it ^ 1].end(), x[2]) - comp[it ^ 1].begin() + 1;
        }
    }
    s = get(s), t = get(t);
    for (int i = 0; i < 2; ++i) {
        events[i].push_back({s[i], 0, s[i ^ 1]});
        events[i].push_back({t[i], 0, t[i ^ 1]});
        tree[i] = Segtree(comp[i].size());
    }
    for (auto it : {0, 1}) {
        sort(events[it].begin(), events[it].end());
        set<int> st{1, comp[it ^ 1].size()};
        for (auto [a, t, b] : events[it]) {
            if (t == 1) {
                st.insert(b);
            } else if (t == -1) {
                st.erase(b);
            } else {
                auto i = st.lower_bound(b);
                int r = *i, l = *--i;
                tree[it ^ 1].add(a, l, r);
            }
        }
    }
    queue<array<int, 4>> q;
    map<array<int, 4>, int> mp;
    for (auto it : {0, 1}) {
        auto cands = tree[it ^ 1].qry(s[it ^ 1], s[it], s[it]);
        for (auto [x, u, v] : cands) {
            mp[{it, x, u, v}] = 1;
            q.push({it, x, u, v});
        }
    }
    while (q.size()) {
        auto [it, x, u, v] = q.front(); q.pop();
        int d = mp[{it, x, u, v}];
        if (t[it] == x && u <= t[it ^ 1] && t[it ^ 1] <= v) {
            cout << d;
            exit(0);
        }
        auto cands = tree[it].qry(x, u, v);
        for (auto [a, b, c] : cands) {
            if (!mp.count({it ^ 1, a, b, c})) {
                mp[{it ^ 1, a, b, c}] = d + 1;
                q.push({it ^ 1, a, b, c}); 
            }
        }
    }
    return 0;
}

Compilation message

golf.cpp: In function 'int main()':
golf.cpp:116:41: warning: narrowing conversion of '(& comp.std::array<std::vector<int>, 2>::operator[](((std::array<std::vector<int>, 2>::size_type)(it ^ 1))))->std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
  116 |         set<int> st{1, comp[it ^ 1].size()};
      |                        ~~~~~~~~~~~~~~~~~^~
golf.cpp:116:41: warning: narrowing conversion of '(& comp.std::array<std::vector<int>, 2>::operator[](((std::array<std::vector<int>, 2>::size_type)(it ^ 1))))->std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 8 ms 1704 KB Output is correct
6 Correct 7 ms 1932 KB Output is correct
7 Correct 8 ms 1628 KB Output is correct
8 Correct 9 ms 1884 KB Output is correct
9 Correct 10 ms 1884 KB Output is correct
10 Correct 10 ms 1936 KB Output is correct
11 Correct 10 ms 1880 KB Output is correct
12 Correct 7 ms 1788 KB Output is correct
13 Correct 12 ms 1884 KB Output is correct
14 Correct 9 ms 2004 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 7 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 8 ms 1704 KB Output is correct
6 Correct 7 ms 1932 KB Output is correct
7 Correct 8 ms 1628 KB Output is correct
8 Correct 9 ms 1884 KB Output is correct
9 Correct 10 ms 1884 KB Output is correct
10 Correct 10 ms 1936 KB Output is correct
11 Correct 10 ms 1880 KB Output is correct
12 Correct 7 ms 1788 KB Output is correct
13 Correct 12 ms 1884 KB Output is correct
14 Correct 9 ms 2004 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 7 ms 1628 KB Output is correct
17 Correct 13 ms 2652 KB Output is correct
18 Correct 9 ms 2652 KB Output is correct
19 Correct 9 ms 2516 KB Output is correct
20 Correct 11 ms 2652 KB Output is correct
21 Correct 10 ms 2908 KB Output is correct
22 Correct 8 ms 2724 KB Output is correct
23 Correct 8 ms 2652 KB Output is correct
24 Correct 11 ms 2728 KB Output is correct
25 Correct 7 ms 2652 KB Output is correct
26 Correct 11 ms 2532 KB Output is correct
27 Correct 3 ms 1116 KB Output is correct
28 Correct 6 ms 1616 KB Output is correct
29 Correct 6 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 8 ms 1704 KB Output is correct
6 Correct 7 ms 1932 KB Output is correct
7 Correct 8 ms 1628 KB Output is correct
8 Correct 9 ms 1884 KB Output is correct
9 Correct 10 ms 1884 KB Output is correct
10 Correct 10 ms 1936 KB Output is correct
11 Correct 10 ms 1880 KB Output is correct
12 Correct 7 ms 1788 KB Output is correct
13 Correct 12 ms 1884 KB Output is correct
14 Correct 9 ms 2004 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 7 ms 1628 KB Output is correct
17 Correct 13 ms 2652 KB Output is correct
18 Correct 9 ms 2652 KB Output is correct
19 Correct 9 ms 2516 KB Output is correct
20 Correct 11 ms 2652 KB Output is correct
21 Correct 10 ms 2908 KB Output is correct
22 Correct 8 ms 2724 KB Output is correct
23 Correct 8 ms 2652 KB Output is correct
24 Correct 11 ms 2728 KB Output is correct
25 Correct 7 ms 2652 KB Output is correct
26 Correct 11 ms 2532 KB Output is correct
27 Correct 3 ms 1116 KB Output is correct
28 Correct 6 ms 1616 KB Output is correct
29 Correct 6 ms 1628 KB Output is correct
30 Correct 2061 ms 318340 KB Output is correct
31 Correct 2276 ms 319072 KB Output is correct
32 Correct 2962 ms 315888 KB Output is correct
33 Correct 3447 ms 320728 KB Output is correct
34 Correct 2195 ms 326316 KB Output is correct
35 Correct 3122 ms 323408 KB Output is correct
36 Correct 1956 ms 320684 KB Output is correct
37 Correct 2664 ms 318608 KB Output is correct
38 Correct 2379 ms 324692 KB Output is correct
39 Correct 2879 ms 319980 KB Output is correct
40 Correct 532 ms 103408 KB Output is correct
41 Correct 493 ms 103360 KB Output is correct
42 Correct 505 ms 103396 KB Output is correct
43 Correct 532 ms 104368 KB Output is correct
44 Correct 500 ms 106824 KB Output is correct
45 Correct 547 ms 105308 KB Output is correct
46 Correct 577 ms 105460 KB Output is correct
47 Correct 547 ms 107592 KB Output is correct
48 Correct 555 ms 104052 KB Output is correct
49 Correct 577 ms 106828 KB Output is correct
50 Correct 6 ms 1628 KB Output is correct
51 Correct 6 ms 1628 KB Output is correct
52 Correct 6 ms 1708 KB Output is correct