Submission #400456

#TimeUsernameProblemLanguageResultExecution timeMemory
400456KoD새 집 (APIO18_new_home)C++17
100 / 100
5000 ms214932 KiB
#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept {
            return itr != other.itr;
        }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept
        : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T> bool setmax(T& lhs, const T& rhs) {
    if (lhs < rhs) {
        lhs = rhs;
        return true;
    }
    return false;
}

template <class T> bool setmin(T& lhs, const T& rhs) {
    if (lhs > rhs) {
        lhs = rhs;
        return true;
    }
    return false;
}

template <class T> using Vec = std::vector<T>;

constexpr i32 MAX = 200000000;

struct SegmentTree {
    Vec<i32> data;
    SegmentTree(const usize size) : data(2 * size, MAX) {}
    void assign(usize i, const i32 x) {
        i += data.size() >> 1;
        data[i] = x;
        while (i > 1) {
            i >>= 1;
            data[i] = std::min(data[2 * i], data[2 * i + 1]);
        }
    }
    i32 fold(usize l, usize r) {
        l += data.size() >> 1;
        r += data.size() >> 1;
        i32 ret = MAX;
        while (l < r) {
            if (l & 1) setmin(ret, data[l++]);
            if (r & 1) setmin(ret, data[--r]);
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }
};

struct GetMinimum {
    std::priority_queue<i32, Vec<i32>, std::greater<i32>> base, other;
    GetMinimum() : base(), other() {}
    void add(const i32 x) { base.push(x); }
    void remove(const i32 x) { other.push(x); }
    i32 min() {
        while (!other.empty() and base.top() == other.top()) {
            base.pop();
            other.pop();
        }
        return base.top();
    }
};

void main_() {
    usize N, K, Q;
    std::cin >> N >> K >> Q;
    Vec<i32> X(N), A(N), B(N);
    Vec<usize> T(N);
    Vec<i32> L(Q), Y(Q);
    Vec<i32> time;
    time.reserve(2 * N + Q);
    for (const auto i : rep(0, N)) {
        std::cin >> X[i] >> T[i] >> A[i] >> B[i];
        X[i] *= 2;
        T[i] -= 1;
        time.push_back(A[i]);
        time.push_back(B[i]);
    }
    for (const auto i : rep(0, Q)) {
        std::cin >> L[i] >> Y[i];
        L[i] *= 2;
        time.push_back(Y[i]);
    }
    std::sort(time.begin(), time.end());
    time.erase(std::unique(time.begin(), time.end()), time.end());
    const auto Ts = time.size();
    Vec<Vec<usize>> add(Ts), remove(Ts), query(Ts);
    for (const auto i : rep(0, N)) {
        add[std::lower_bound(time.begin(), time.end(), A[i]) - time.begin()]
            .push_back(i);
        remove[std::lower_bound(time.begin(), time.end(), B[i]) - time.begin()]
            .push_back(i);
    }
    for (const auto i : rep(0, Q)) {
        query[std::lower_bound(time.begin(), time.end(), Y[i]) - time.begin()]
            .push_back(i);
    }
    Vec<i32> ans(Q);
    const auto solve = [&] {
        Vec<i32> coord = L;
        std::sort(coord.begin(), coord.end());
        coord.erase(std::unique(coord.begin(), coord.end()), coord.end());
        const auto Xs = coord.size();
        SegmentTree seg(Xs);
        Vec<GetMinimum> lines(Xs);
        Vec<std::multiset<i32>> pts(K);
        const auto add_line = [&](const i32 x, const i32 y) {
            usize i =
                std::upper_bound(coord.begin(), coord.end(), x) - coord.begin();
            if (i == 0) return;
            i -= 1;
            lines[i].add(y);
            seg.assign(i, lines[i].min());
        };
        const auto remove_line = [&](const i32 x, const i32 y) {
            usize i =
                std::upper_bound(coord.begin(), coord.end(), x) - coord.begin();
            if (i == 0) return;
            i -= 1;
            lines[i].remove(y);
            seg.assign(i, lines[i].min());
        };
        for (const auto i : rep(0, Xs)) {
            lines[i].add(MAX);
        }
        for (const auto i : rep(0, K)) {
            pts[i].insert(2 * MAX);
            pts[i].insert(-MAX);
            add_line(MAX / 2, -MAX);
        }
        for (const auto t : rep(0, Ts)) {
            for (const auto i : add[t]) {
                const auto itr = pts[T[i]].lower_bound(X[i]);
                const auto l = *std::prev(itr);
                const auto r = *itr;
                remove_line((l + r) / 2, l);
                add_line((l + X[i]) / 2, l);
                add_line((X[i] + r) / 2, X[i]);
                pts[T[i]].insert(X[i]);
            }
            for (const auto i : query[t]) {
                setmax(ans[i],
                       L[i] - seg.fold(std::lower_bound(coord.begin(),
                                                        coord.end(), L[i]) -
                                           coord.begin(),
                                       Xs));
            }
            for (const auto i : remove[t]) {
                const auto itr = pts[T[i]].find(X[i]);
                const auto l = *std::prev(itr);
                const auto r = *std::next(itr);
                remove_line((l + X[i]) / 2, l);
                remove_line((X[i] + r) / 2, X[i]);
                add_line((l + r) / 2, l);
                pts[T[i]].erase(itr);
            }
        }
    };
    solve();
    for (auto& x : X) {
        x = MAX - x;
    }
    for (auto& x : L) {
        x = MAX - x;
    }
    solve();
    for (const auto x : ans) {
        std::cout << (x >= MAX ? -1 : x / 2) << '\n';
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    main_();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...