Submission #400452

# Submission time Handle Problem Language Result Execution time Memory
400452 2021-05-08T05:07:28 Z KoD New Home (APIO18_new_home) C++17
57 / 100
5000 ms 168988 KB
#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)};
    }
};

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<std::multiset<i32>> 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].insert(y);
            seg.assign(i, Monoid{*lines[i].begin()});
        };
        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].erase(lines[i].find(y));
            seg.assign(i, Monoid{*lines[i].begin()});
        };
        for (const auto i : rep(0, Xs)) {
            lines[i].insert(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 time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 516 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 452 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 3 ms 588 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 516 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 452 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 3 ms 588 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 668 ms 36628 KB Output is correct
32 Correct 272 ms 8024 KB Output is correct
33 Correct 596 ms 32892 KB Output is correct
34 Correct 578 ms 33096 KB Output is correct
35 Correct 678 ms 36588 KB Output is correct
36 Correct 674 ms 36488 KB Output is correct
37 Correct 412 ms 31204 KB Output is correct
38 Correct 419 ms 31208 KB Output is correct
39 Correct 330 ms 30820 KB Output is correct
40 Correct 355 ms 30924 KB Output is correct
41 Correct 362 ms 31080 KB Output is correct
42 Correct 377 ms 31024 KB Output is correct
43 Correct 90 ms 12560 KB Output is correct
44 Correct 380 ms 31204 KB Output is correct
45 Correct 348 ms 30948 KB Output is correct
46 Correct 354 ms 30896 KB Output is correct
47 Correct 237 ms 29924 KB Output is correct
48 Correct 222 ms 29796 KB Output is correct
49 Correct 263 ms 30240 KB Output is correct
50 Correct 295 ms 30728 KB Output is correct
51 Correct 264 ms 30180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3656 ms 134744 KB Output is correct
2 Correct 4075 ms 124004 KB Output is correct
3 Correct 2867 ms 168972 KB Output is correct
4 Correct 3474 ms 140416 KB Output is correct
5 Correct 3688 ms 123532 KB Output is correct
6 Correct 4085 ms 123996 KB Output is correct
7 Correct 2656 ms 168988 KB Output is correct
8 Correct 2568 ms 140564 KB Output is correct
9 Correct 2712 ms 130576 KB Output is correct
10 Correct 3165 ms 125080 KB Output is correct
11 Correct 1656 ms 123428 KB Output is correct
12 Correct 1757 ms 124924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5056 ms 154376 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 516 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 452 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 3 ms 588 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 668 ms 36628 KB Output is correct
32 Correct 272 ms 8024 KB Output is correct
33 Correct 596 ms 32892 KB Output is correct
34 Correct 578 ms 33096 KB Output is correct
35 Correct 678 ms 36588 KB Output is correct
36 Correct 674 ms 36488 KB Output is correct
37 Correct 412 ms 31204 KB Output is correct
38 Correct 419 ms 31208 KB Output is correct
39 Correct 330 ms 30820 KB Output is correct
40 Correct 355 ms 30924 KB Output is correct
41 Correct 362 ms 31080 KB Output is correct
42 Correct 377 ms 31024 KB Output is correct
43 Correct 90 ms 12560 KB Output is correct
44 Correct 380 ms 31204 KB Output is correct
45 Correct 348 ms 30948 KB Output is correct
46 Correct 354 ms 30896 KB Output is correct
47 Correct 237 ms 29924 KB Output is correct
48 Correct 222 ms 29796 KB Output is correct
49 Correct 263 ms 30240 KB Output is correct
50 Correct 295 ms 30728 KB Output is correct
51 Correct 264 ms 30180 KB Output is correct
52 Correct 664 ms 44896 KB Output is correct
53 Correct 618 ms 43104 KB Output is correct
54 Correct 686 ms 39192 KB Output is correct
55 Correct 467 ms 35880 KB Output is correct
56 Correct 481 ms 37976 KB Output is correct
57 Correct 419 ms 32708 KB Output is correct
58 Correct 507 ms 35884 KB Output is correct
59 Correct 536 ms 38104 KB Output is correct
60 Correct 443 ms 32440 KB Output is correct
61 Correct 150 ms 21044 KB Output is correct
62 Correct 624 ms 45088 KB Output is correct
63 Correct 651 ms 40016 KB Output is correct
64 Correct 617 ms 37560 KB Output is correct
65 Correct 549 ms 33240 KB Output is correct
66 Correct 419 ms 31284 KB Output is correct
67 Correct 271 ms 8504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 516 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 452 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 3 ms 588 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 668 ms 36628 KB Output is correct
32 Correct 272 ms 8024 KB Output is correct
33 Correct 596 ms 32892 KB Output is correct
34 Correct 578 ms 33096 KB Output is correct
35 Correct 678 ms 36588 KB Output is correct
36 Correct 674 ms 36488 KB Output is correct
37 Correct 412 ms 31204 KB Output is correct
38 Correct 419 ms 31208 KB Output is correct
39 Correct 330 ms 30820 KB Output is correct
40 Correct 355 ms 30924 KB Output is correct
41 Correct 362 ms 31080 KB Output is correct
42 Correct 377 ms 31024 KB Output is correct
43 Correct 90 ms 12560 KB Output is correct
44 Correct 380 ms 31204 KB Output is correct
45 Correct 348 ms 30948 KB Output is correct
46 Correct 354 ms 30896 KB Output is correct
47 Correct 237 ms 29924 KB Output is correct
48 Correct 222 ms 29796 KB Output is correct
49 Correct 263 ms 30240 KB Output is correct
50 Correct 295 ms 30728 KB Output is correct
51 Correct 264 ms 30180 KB Output is correct
52 Correct 3656 ms 134744 KB Output is correct
53 Correct 4075 ms 124004 KB Output is correct
54 Correct 2867 ms 168972 KB Output is correct
55 Correct 3474 ms 140416 KB Output is correct
56 Correct 3688 ms 123532 KB Output is correct
57 Correct 4085 ms 123996 KB Output is correct
58 Correct 2656 ms 168988 KB Output is correct
59 Correct 2568 ms 140564 KB Output is correct
60 Correct 2712 ms 130576 KB Output is correct
61 Correct 3165 ms 125080 KB Output is correct
62 Correct 1656 ms 123428 KB Output is correct
63 Correct 1757 ms 124924 KB Output is correct
64 Execution timed out 5056 ms 154376 KB Time limit exceeded
65 Halted 0 ms 0 KB -