Submission #400456

# Submission time Handle Problem Language Result Execution time Memory
400456 2021-05-08T05:23:38 Z KoD New Home (APIO18_new_home) C++17
100 / 100
5000 ms 214932 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;
}

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 time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 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 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 2 ms 460 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 464 KB Output is correct
14 Correct 2 ms 460 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 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 464 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 336 KB Output is correct
28 Correct 2 ms 464 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 204 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 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 2 ms 460 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 464 KB Output is correct
14 Correct 2 ms 460 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 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 464 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 336 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 678 ms 32032 KB Output is correct
32 Correct 154 ms 6084 KB Output is correct
33 Correct 585 ms 30100 KB Output is correct
34 Correct 574 ms 30288 KB Output is correct
35 Correct 674 ms 31892 KB Output is correct
36 Correct 678 ms 31928 KB Output is correct
37 Correct 423 ms 29632 KB Output is correct
38 Correct 417 ms 29712 KB Output is correct
39 Correct 346 ms 29256 KB Output is correct
40 Correct 365 ms 29368 KB Output is correct
41 Correct 398 ms 29544 KB Output is correct
42 Correct 407 ms 29384 KB Output is correct
43 Correct 73 ms 7608 KB Output is correct
44 Correct 406 ms 29588 KB Output is correct
45 Correct 382 ms 29460 KB Output is correct
46 Correct 362 ms 29192 KB Output is correct
47 Correct 244 ms 28092 KB Output is correct
48 Correct 234 ms 27984 KB Output is correct
49 Correct 276 ms 28464 KB Output is correct
50 Correct 305 ms 28708 KB Output is correct
51 Correct 282 ms 28588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2987 ms 110644 KB Output is correct
2 Correct 4066 ms 100900 KB Output is correct
3 Correct 1846 ms 141556 KB Output is correct
4 Correct 2670 ms 115520 KB Output is correct
5 Correct 3587 ms 100472 KB Output is correct
6 Correct 3770 ms 100828 KB Output is correct
7 Correct 1565 ms 141560 KB Output is correct
8 Correct 2053 ms 115136 KB Output is correct
9 Correct 2545 ms 106076 KB Output is correct
10 Correct 3095 ms 101696 KB Output is correct
11 Correct 1658 ms 100648 KB Output is correct
12 Correct 1750 ms 101508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4464 ms 131016 KB Output is correct
2 Correct 940 ms 36996 KB Output is correct
3 Correct 4588 ms 129336 KB Output is correct
4 Correct 2403 ms 171052 KB Output is correct
5 Correct 3487 ms 137020 KB Output is correct
6 Correct 3265 ms 141660 KB Output is correct
7 Correct 4277 ms 128652 KB Output is correct
8 Correct 4439 ms 128980 KB Output is correct
9 Correct 2201 ms 172032 KB Output is correct
10 Correct 2953 ms 140256 KB Output is correct
11 Correct 3573 ms 132628 KB Output is correct
12 Correct 3900 ms 129720 KB Output is correct
13 Correct 1676 ms 127056 KB Output is correct
14 Correct 1644 ms 126040 KB Output is correct
15 Correct 1867 ms 128084 KB Output is correct
16 Correct 2065 ms 129428 KB Output is correct
17 Correct 2001 ms 127632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 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 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 2 ms 460 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 464 KB Output is correct
14 Correct 2 ms 460 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 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 464 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 336 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 678 ms 32032 KB Output is correct
32 Correct 154 ms 6084 KB Output is correct
33 Correct 585 ms 30100 KB Output is correct
34 Correct 574 ms 30288 KB Output is correct
35 Correct 674 ms 31892 KB Output is correct
36 Correct 678 ms 31928 KB Output is correct
37 Correct 423 ms 29632 KB Output is correct
38 Correct 417 ms 29712 KB Output is correct
39 Correct 346 ms 29256 KB Output is correct
40 Correct 365 ms 29368 KB Output is correct
41 Correct 398 ms 29544 KB Output is correct
42 Correct 407 ms 29384 KB Output is correct
43 Correct 73 ms 7608 KB Output is correct
44 Correct 406 ms 29588 KB Output is correct
45 Correct 382 ms 29460 KB Output is correct
46 Correct 362 ms 29192 KB Output is correct
47 Correct 244 ms 28092 KB Output is correct
48 Correct 234 ms 27984 KB Output is correct
49 Correct 276 ms 28464 KB Output is correct
50 Correct 305 ms 28708 KB Output is correct
51 Correct 282 ms 28588 KB Output is correct
52 Correct 453 ms 40116 KB Output is correct
53 Correct 435 ms 38200 KB Output is correct
54 Correct 532 ms 34660 KB Output is correct
55 Correct 389 ms 33220 KB Output is correct
56 Correct 395 ms 34944 KB Output is correct
57 Correct 399 ms 30548 KB Output is correct
58 Correct 414 ms 33068 KB Output is correct
59 Correct 423 ms 34908 KB Output is correct
60 Correct 407 ms 30484 KB Output is correct
61 Correct 92 ms 16608 KB Output is correct
62 Correct 436 ms 39972 KB Output is correct
63 Correct 494 ms 35588 KB Output is correct
64 Correct 481 ms 33804 KB Output is correct
65 Correct 474 ms 30908 KB Output is correct
66 Correct 402 ms 29640 KB Output is correct
67 Correct 159 ms 7824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 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 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 2 ms 460 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 464 KB Output is correct
14 Correct 2 ms 460 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 2 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 464 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 336 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 678 ms 32032 KB Output is correct
32 Correct 154 ms 6084 KB Output is correct
33 Correct 585 ms 30100 KB Output is correct
34 Correct 574 ms 30288 KB Output is correct
35 Correct 674 ms 31892 KB Output is correct
36 Correct 678 ms 31928 KB Output is correct
37 Correct 423 ms 29632 KB Output is correct
38 Correct 417 ms 29712 KB Output is correct
39 Correct 346 ms 29256 KB Output is correct
40 Correct 365 ms 29368 KB Output is correct
41 Correct 398 ms 29544 KB Output is correct
42 Correct 407 ms 29384 KB Output is correct
43 Correct 73 ms 7608 KB Output is correct
44 Correct 406 ms 29588 KB Output is correct
45 Correct 382 ms 29460 KB Output is correct
46 Correct 362 ms 29192 KB Output is correct
47 Correct 244 ms 28092 KB Output is correct
48 Correct 234 ms 27984 KB Output is correct
49 Correct 276 ms 28464 KB Output is correct
50 Correct 305 ms 28708 KB Output is correct
51 Correct 282 ms 28588 KB Output is correct
52 Correct 2987 ms 110644 KB Output is correct
53 Correct 4066 ms 100900 KB Output is correct
54 Correct 1846 ms 141556 KB Output is correct
55 Correct 2670 ms 115520 KB Output is correct
56 Correct 3587 ms 100472 KB Output is correct
57 Correct 3770 ms 100828 KB Output is correct
58 Correct 1565 ms 141560 KB Output is correct
59 Correct 2053 ms 115136 KB Output is correct
60 Correct 2545 ms 106076 KB Output is correct
61 Correct 3095 ms 101696 KB Output is correct
62 Correct 1658 ms 100648 KB Output is correct
63 Correct 1750 ms 101508 KB Output is correct
64 Correct 4464 ms 131016 KB Output is correct
65 Correct 940 ms 36996 KB Output is correct
66 Correct 4588 ms 129336 KB Output is correct
67 Correct 2403 ms 171052 KB Output is correct
68 Correct 3487 ms 137020 KB Output is correct
69 Correct 3265 ms 141660 KB Output is correct
70 Correct 4277 ms 128652 KB Output is correct
71 Correct 4439 ms 128980 KB Output is correct
72 Correct 2201 ms 172032 KB Output is correct
73 Correct 2953 ms 140256 KB Output is correct
74 Correct 3573 ms 132628 KB Output is correct
75 Correct 3900 ms 129720 KB Output is correct
76 Correct 1676 ms 127056 KB Output is correct
77 Correct 1644 ms 126040 KB Output is correct
78 Correct 1867 ms 128084 KB Output is correct
79 Correct 2065 ms 129428 KB Output is correct
80 Correct 2001 ms 127632 KB Output is correct
81 Correct 453 ms 40116 KB Output is correct
82 Correct 435 ms 38200 KB Output is correct
83 Correct 532 ms 34660 KB Output is correct
84 Correct 389 ms 33220 KB Output is correct
85 Correct 395 ms 34944 KB Output is correct
86 Correct 399 ms 30548 KB Output is correct
87 Correct 414 ms 33068 KB Output is correct
88 Correct 423 ms 34908 KB Output is correct
89 Correct 407 ms 30484 KB Output is correct
90 Correct 92 ms 16608 KB Output is correct
91 Correct 436 ms 39972 KB Output is correct
92 Correct 494 ms 35588 KB Output is correct
93 Correct 481 ms 33804 KB Output is correct
94 Correct 474 ms 30908 KB Output is correct
95 Correct 402 ms 29640 KB Output is correct
96 Correct 159 ms 7824 KB Output is correct
97 Correct 2822 ms 198872 KB Output is correct
98 Correct 973 ms 30244 KB Output is correct
99 Correct 4818 ms 148824 KB Output is correct
100 Correct 2747 ms 190656 KB Output is correct
101 Correct 3591 ms 169744 KB Output is correct
102 Correct 5000 ms 157168 KB Output is correct
103 Correct 3200 ms 160880 KB Output is correct
104 Correct 3153 ms 160148 KB Output is correct
105 Correct 2056 ms 159276 KB Output is correct
106 Correct 2188 ms 159460 KB Output is correct
107 Correct 2671 ms 175976 KB Output is correct
108 Correct 2622 ms 184464 KB Output is correct
109 Correct 2665 ms 166064 KB Output is correct
110 Correct 2796 ms 179024 KB Output is correct
111 Correct 2821 ms 190132 KB Output is correct
112 Correct 2764 ms 165616 KB Output is correct
113 Correct 489 ms 94208 KB Output is correct
114 Correct 2814 ms 214932 KB Output is correct
115 Correct 3279 ms 193340 KB Output is correct
116 Correct 3411 ms 184648 KB Output is correct
117 Correct 3497 ms 168024 KB Output is correct
118 Correct 3056 ms 161416 KB Output is correct
119 Correct 1065 ms 44416 KB Output is correct
120 Correct 1177 ms 148672 KB Output is correct
121 Correct 1351 ms 153556 KB Output is correct
122 Correct 1301 ms 152888 KB Output is correct
123 Correct 1512 ms 155096 KB Output is correct
124 Correct 1666 ms 156908 KB Output is correct
125 Correct 1574 ms 155648 KB Output is correct
126 Correct 1689 ms 157124 KB Output is correct