Submission #400453

#TimeUsernameProblemLanguageResultExecution timeMemory
400453KoDNew Home (APIO18_new_home)C++17
80 / 100
5040 ms216300 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;
}

class revrep {
    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 revrep(const usize first, const usize last) noexcept
        : first(last - 1), last(std::min(first, last) - 1) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr u64 ceil_log2(const u64 x) {
    u64 e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
}

template <class Monoid> class SegmentTree {
    using M = Monoid;
    usize internal_size, seg_size;
    std::vector<M> data;

    void fetch(const usize k) { data[k] = data[2 * k] + data[2 * k + 1]; }

  public:
    explicit SegmentTree(const usize size = 0, const M& value = M::zero())
        : SegmentTree(std::vector<M>(size, value)) {}
    explicit SegmentTree(const std::vector<M>& vec)
        : internal_size(vec.size()) {
        seg_size = 1 << ceil_log2(internal_size);
        data = std::vector<M>(2 * seg_size, M::zero());
        for (const usize i : rep(0, internal_size)) data[seg_size + i] = vec[i];
        for (const usize i : revrep(1, seg_size)) fetch(i);
    }

    usize size() const { return internal_size; }

    void assign(usize i, const M& value) {
        assert(i < internal_size);
        i += seg_size;
        data[i] = value;
        while (i > 1) {
            i >>= 1;
            fetch(i);
        }
    }

    M fold() const { return data[1]; }
    M fold(usize l, usize r) const {
        assert(l <= r and r <= internal_size);
        l += seg_size;
        r += seg_size;
        M ret_l = M::zero(), ret_r = M::zero();
        while (l < r) {
            if (l & 1) ret_l = ret_l + data[l++];
            if (r & 1) ret_r = data[--r] + ret_r;
            l >>= 1;
            r >>= 1;
        }
        return ret_l + ret_r;
    }

    template <class F> usize max_right(usize l, const F& f) const {
        assert(l <= internal_size);
        assert(f(M::zero()));
        if (l == internal_size) return internal_size;
        l += seg_size;
        M sum = M::zero();
        do {
            while (!(l & 1)) l >>= 1;
            if (!f(sum + data[l])) {
                while (l < seg_size) {
                    l = 2 * l;
                    if (f(sum + data[l])) sum = sum + data[l++];
                }
                return l - seg_size;
            }
            sum = sum + data[l++];
        } while ((l & -l) != l);
        return internal_size;
    }

    template <class F> usize min_left(usize r, const F& f) const {
        assert(r <= internal_size);
        assert(f(M::zero()));
        if (r == 0) return 0;
        r += seg_size;
        M sum = M::zero();
        do {
            r -= 1;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!f(data[r] + sum)) {
                while (r < seg_size) {
                    r = 2 * r + 1;
                    if (f(data[r] + sum)) sum = data[r--] + sum;
                }
                return r + 1 - seg_size;
            }
            sum = data[r] + sum;
        } while ((r & -r) != r);
        return 0;
    }
};

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

constexpr i32 MAX = 200000000;

struct Monoid {
    i32 min;
    static Monoid zero() { return Monoid{MAX}; }
    Monoid operator+(const Monoid& other) const {
        return Monoid{std::min(min, other.min)};
    }
};

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<Monoid> 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, Monoid{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, Monoid{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)
                                  .min);
            }
            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...