Submission #792295

# Submission time Handle Problem Language Result Execution time Memory
792295 2023-07-25T00:28:33 Z skittles1412 Sky Walking (IOI19_walk) C++17
43 / 100
2976 ms 360020 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

using ll = long long;

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;

template <typename A, typename B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}

template <typename Cb>
struct CmpByKey {
    Cb cb;

    CmpByKey(const Cb& cb) : cb(cb) {}

    template <typename T>
    bool operator()(const T& a, const T& b) const {
        return cb(a) < cb(b);
    }
};

template <typename T>
struct Comp {
    map<T, int> mp;

    int operator()(const T& t) {
        auto [it, inserted] = mp.emplace(t, -1);
        if (inserted) {
            it->second = sz(mp) - 1;
        }
        return it->second;
    }
};

template <typename T, typename Cmp = less<T>>
vector<T> sorted(vector<T> arr, Cmp cmp = Cmp()) {
    sort(begin(arr), end(arr), cmp);
    return arr;
}

template <typename S>
long dijkstra(const vector<tuple<S, S, long>>& edges, S src, S dest) {
    dbg("A");
    int n = 2 * sz(edges) + 10;

    Comp<S> comp;
    vector<pair<int, long>> graph[n];

    for (auto& [u, v, w] : edges) {
        int cu = comp(u), cv = comp(v);
        graph[cu].emplace_back(cv, w);
        graph[cv].emplace_back(cu, w);
    }

    long dist[n];
    memset(dist, 0x3f, sizeof(dist));

    min_pq<pair<long, int>> pq;
    auto push = [&](int u, long d) -> void {
        if (d >= dist[u]) {
            return;
        }
        dist[u] = d;
        pq.emplace(d, u);
    };

    int c_src = comp(src), c_dest = comp(dest);

    push(c_src, 0);

    while (sz(pq)) {
        auto [d, u] = pq.top();
        pq.pop();

        if (u == c_dest) {
            return d;
        }
        if (d != dist[u]) {
            continue;
        }

        for (auto& [v, w] : graph[u]) {
            push(v, d + w);
        }
    }

    return -1;
}

namespace s1 {

long solve(vector<pair<int, int>> arr,
           vector<array<int, 3>> walk,
           int src,
           int dest) {
    map<int, vector<pair<int, int>>> evts;
    for (auto& [x, h] : arr) {
        evts[-h].emplace_back(x, -1);
    }
    for (auto& [ql, qr, h] : walk) {
        evts[-h].emplace_back(ql, qr);
    }

    vector<array<int, 3>> d_walk;
    {
        set<int> pos;
        for (auto& [nh, ev] : evts) {
            int h = -nh;

            for (auto& [x, ty] : ev) {
                if (ty == -1) {
                    dbg(h, x);
                    pos.insert(x);
                }
            }

            for (auto& [ql, qr] : ev) {
                if (qr == -1) {
                    continue;
                }
                dbg(h, ql, qr);

                auto it = pos.lower_bound(ql);
                while (true) {
                    int cl = *it;
                    ++it;
                    int cr = *it;
                    d_walk.push_back({cl, cr, h});

                    dbg("A", *it, qr);
                    assert(*it <= qr);
                    if (*it == qr) {
                        break;
                    }
                }
            }
        }
    }

    for (auto& [ql, qr, h] : d_walk) {
        dbg(ql, qr, h);
    }

    using S = pair<int, int>;

    S s_src {src, 0}, s_dest {dest, 0};
    vector<S> states {s_src, s_dest};
    for (auto& [ql, qr, h] : d_walk) {
        states.emplace_back(ql, h);
        states.emplace_back(qr, h);
    }

    sort(begin(states), end(states));
    states.erase(unique(begin(states), end(states)), states.end());

    map<int, vector<int>> buildings;
    for (auto& [x, y] : states) {
        buildings[x].push_back(y);
    }

    vector<tuple<S, S, long>> edges;

    for (auto& [x, ys] : buildings) {
        sort(begin(ys), end(ys));
        for (int i = 0; i + 1 < sz(ys); i++) {
            edges.emplace_back(S(x, ys[i]), S(x, ys[i + 1]), ys[i + 1] - ys[i]);
        }
    }

    for (auto& [ql, qr, h] : d_walk) {
        edges.emplace_back(S(ql, h), S(qr, h), qr - ql);
    }

    for (auto& [u, v, w] : edges) {
        dbg(u, v, w);
    }

    return dijkstra(edges, s_src, s_dest);
}

}  // namespace s1

namespace s2 {

template <typename Cb>
void add_edges(vector<array<int, 4>> arr, const Cb& cb) {
    for (auto& [_ql, qr, _i, _] : arr) {
        // [] -> [)
        qr++;
    }

    int n = sz(arr);

    map<pair<int, int>, int> mp;

    auto split = [&](int ind) -> void {
        auto it = mp.lower_bound({ind + 1, -1});
        if (it == mp.begin()) {
            return;
        }
        --it;
        auto [cql, cqr] = it->first;

        if (cql < ind && ind < cqr) {
            int cv = it->second;
            mp.erase(it);
            mp[{cql, ind}] = cv;
            mp[{ind, cqr}] = cv;
        }
    };

    for (auto& [ql, qr, qi, _] : arr) {
        split(ql);
        split(qr);

        for (auto it = mp.lower_bound({ql, -1}); it != mp.end();
             it = mp.erase(it)) {
            auto [cql, cqr] = it->first;
            if (qr <= cql) {
                break;
            }

            assert(ql <= cql && cqr <= qr);
            cb(qi, it->second);
        }

        mp[{ql, qr}] = qi;
    }
}

long solve(vector<pair<int, int>> arr,
           vector<array<int, 3>> walk,
           int src,
           int dest) {
    int c_src = sz(walk), c_dest = sz(walk) + 1;
    walk.push_back({src, src, 0});
    walk.push_back({dest, dest, 0});

    using S = pair<int, int>;

    vector<tuple<S, S, long>> edges;

    auto add_edge = [&](const S& s1, const S& s2) -> void {
        auto& [x1, y1] = s1;
        auto& [x2, y2] = s2;
        assert(x1 == x2 || y1 == y2);
        dbg(s1, s2);
        edges.emplace_back(s1, s2, abs(x1 - x2) + abs(y1 - y2));
    };

    vector<array<int, 4>> n_walk;
    for (int i = 0; i < sz(walk); i++) {
        auto& [ql, qr, qh] = walk[i];
        n_walk.push_back({ql, qr, i, qh});
    }

    vector<S> w_segs[sz(walk)];

    auto cb_edge = [&](int u, int v) -> void {
        auto [l1, r1, h1] = walk[u];
        auto [l2, r2, h2] = walk[v];

        array<int, 4> pos {l1, r1, l2, r2};
        sort(begin(pos), end(pos));

        for (auto& x : {pos[1], pos[2]}) {
            w_segs[u].emplace_back(x, h1);
            w_segs[v].emplace_back(x, h2);

            auto it = lower_bound(begin(arr), end(arr), S {x, -1});
            assert(it != arr.end());
            if (max(h1, h2) <= it->second) {
                add_edge(S {x, h1}, S {x, h2});
            }
        }
    };

    add_edges(
        sorted(n_walk, CmpByKey([&](const auto& a) -> int { return a[3]; })),
        cb_edge);
    add_edges(
        sorted(n_walk, CmpByKey([&](const auto& a) -> int { return -a[3]; })),
        cb_edge);

    for (auto& a : w_segs) {
        sort(begin(a), end(a));
        a.erase(unique(begin(a), end(a)), a.end());

        for (int i = 0; i + 1 < sz(a); i++) {
            add_edge(a[i], a[i + 1]);
        }
    }

    return dijkstra(edges, S {src, 0}, S {dest, 0});
}

}  // namespace s2

ll min_distance(vector<int> arr_x,
                vector<int> arr_h,
                vector<int> walk_l,
                vector<int> walk_r,
                vector<int> walk_y,
                int src,
                int dest) {
    int n = sz(arr_x), m = sz(walk_l);

    vector<pair<int, int>> arr(n);
    for (int i = 0; i < n; i++) {
        arr[i] = {arr_x[i], arr_h[i]};
    }

    vector<array<int, 3>> walk(m);
    for (int i = 0; i < m; i++) {
        walk[i] = {arr_x[walk_l[i]], arr_x[walk_r[i]], walk_y[i]};
    }

    src = arr_x[src];
    dest = arr_x[dest];

    return s2::solve(arr, walk, src, dest);
}

Compilation message

walk.cpp: In function 'int64_t s1::solve(std::vector<std::pair<int, int> >, std::vector<std::array<int, 3> >, int, int)':
walk.cpp:167:16: warning: unused structured binding declaration [-Wunused-variable]
  167 |     for (auto& [ql, qr, h] : d_walk) {
      |                ^~~~~~~~~~~
walk.cpp:201:16: warning: unused structured binding declaration [-Wunused-variable]
  201 |     for (auto& [u, v, w] : edges) {
      |                ^~~~~~~~~
walk.cpp: In function 'int64_t s2::solve(std::vector<std::pair<int, int> >, std::vector<std::array<int, 3> >, int, int)':
walk.cpp:262:9: warning: unused variable 'c_src' [-Wunused-variable]
  262 |     int c_src = sz(walk), c_dest = sz(walk) + 1;
      |         ^~~~~
walk.cpp:262:27: warning: unused variable 'c_dest' [-Wunused-variable]
  262 |     int c_src = sz(walk), c_dest = sz(walk) + 1;
      |                           ^~~~~~
walk.cpp: In instantiation of 'void s2::add_edges(std::vector<std::array<int, 4> >, const Cb&) [with Cb = s2::solve(std::vector<std::pair<int, int> >, std::vector<std::array<int, 3> >, int, int)::<lambda(int, int)>]':
walk.cpp:307:16:   required from here
walk.cpp:219:9: warning: unused variable 'n' [-Wunused-variable]
  219 |     int n = sz(arr);
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1893 ms 256160 KB Output is correct
4 Correct 2089 ms 245684 KB Output is correct
5 Correct 1238 ms 187916 KB Output is correct
6 Correct 1093 ms 164648 KB Output is correct
7 Correct 1253 ms 188148 KB Output is correct
8 Correct 2025 ms 275560 KB Output is correct
9 Correct 1801 ms 227852 KB Output is correct
10 Incorrect 2148 ms 261332 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 437 ms 92816 KB Output is correct
2 Correct 2413 ms 334132 KB Output is correct
3 Correct 2975 ms 347684 KB Output is correct
4 Correct 2952 ms 350456 KB Output is correct
5 Correct 2915 ms 360020 KB Output is correct
6 Correct 2537 ms 318828 KB Output is correct
7 Correct 987 ms 165708 KB Output is correct
8 Correct 909 ms 116000 KB Output is correct
9 Correct 2353 ms 291952 KB Output is correct
10 Correct 745 ms 140092 KB Output is correct
11 Correct 8 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 92816 KB Output is correct
2 Correct 2413 ms 334132 KB Output is correct
3 Correct 2975 ms 347684 KB Output is correct
4 Correct 2952 ms 350456 KB Output is correct
5 Correct 2915 ms 360020 KB Output is correct
6 Correct 2537 ms 318828 KB Output is correct
7 Correct 987 ms 165708 KB Output is correct
8 Correct 909 ms 116000 KB Output is correct
9 Correct 2353 ms 291952 KB Output is correct
10 Correct 745 ms 140092 KB Output is correct
11 Correct 8 ms 2260 KB Output is correct
12 Correct 2830 ms 346388 KB Output is correct
13 Correct 2765 ms 350012 KB Output is correct
14 Correct 2915 ms 359928 KB Output is correct
15 Correct 1650 ms 216556 KB Output is correct
16 Correct 1908 ms 258248 KB Output is correct
17 Correct 2246 ms 324808 KB Output is correct
18 Correct 1650 ms 216768 KB Output is correct
19 Correct 1901 ms 258976 KB Output is correct
20 Correct 1302 ms 174116 KB Output is correct
21 Correct 26 ms 6564 KB Output is correct
22 Correct 1852 ms 259288 KB Output is correct
23 Correct 1606 ms 234052 KB Output is correct
24 Correct 962 ms 142076 KB Output is correct
25 Correct 1464 ms 211616 KB Output is correct
26 Correct 675 ms 98288 KB Output is correct
27 Correct 2976 ms 358628 KB Output is correct
28 Correct 2863 ms 349116 KB Output is correct
29 Correct 2438 ms 318172 KB Output is correct
30 Correct 989 ms 164928 KB Output is correct
31 Correct 2222 ms 291576 KB Output is correct
32 Correct 816 ms 140448 KB Output is correct
33 Correct 855 ms 135184 KB Output is correct
34 Correct 751 ms 139728 KB Output is correct
35 Correct 1174 ms 176536 KB Output is correct
36 Correct 1004 ms 137240 KB Output is correct
37 Correct 1033 ms 141888 KB Output is correct
38 Correct 890 ms 131964 KB Output is correct
39 Correct 741 ms 143292 KB Output is correct
40 Correct 976 ms 135576 KB Output is correct
41 Correct 925 ms 127852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1893 ms 256160 KB Output is correct
21 Correct 2089 ms 245684 KB Output is correct
22 Correct 1238 ms 187916 KB Output is correct
23 Correct 1093 ms 164648 KB Output is correct
24 Correct 1253 ms 188148 KB Output is correct
25 Correct 2025 ms 275560 KB Output is correct
26 Correct 1801 ms 227852 KB Output is correct
27 Incorrect 2148 ms 261332 KB Output isn't correct
28 Halted 0 ms 0 KB -