競技プログラミング用 C++ ライブラリ
test/dynamic_li_chao_tree_line.test.cpp · GitHub で見る
#define PROBLEM "https://judge.yosupo.jp/problem/line_add_get_min"
#include "data_structure/dynamic_li_chao_tree.hpp"
#include "components/fast_io.hpp"
int main() {
FastInput input;
FastOutput output;
int n = 0, q = 0;
input.read(n), input.read(q);
DynamicLiChaoTree<> tree(-1000000000LL, 1000000001LL);
tree.reserve(n + q);
for (int i = 0; i < n; ++i) {
std::int64_t a = 0, b = 0;
input.read(a), input.read(b);
tree.add_line(a, b);
}
while (q--) {
int type = 0;
std::int64_t a = 0, b = 0;
input.read(type), input.read(a);
if (type == 0) input.read(b), tree.add_line(a, b);
else output.println(std::int64_t(*tree.query(a)));
}
}
#line 1 "test/dynamic_li_chao_tree_line.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/line_add_get_min"
#line 1 "data_structure/dynamic_li_chao_tree.hpp"
#line 1 "components/ordered_midpoint.hpp"
#include <type_traits>
template<class T>
constexpr T ordered_midpoint(T l, T r) {
static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool> && sizeof(T) <= 8);
using U = std::make_unsigned_t<T>;
return T(U(l) + U(U(r) - U(l)) / 2);
}
#line 5 "data_structure/dynamic_li_chao_tree.hpp"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#line 12 "data_structure/dynamic_li_chao_tree.hpp"
#include <utility>
#include <vector>
template <class T = std::int64_t, bool maximize = false, class R = std::conditional_t<(sizeof(T) < 8), std::int64_t, __int128_t>>
class DynamicLiChaoTree {
static_assert(std::is_integral_v<T> && std::is_signed_v<T> && sizeof(T) <= 8);
using B = std::conditional_t<(sizeof(T) < 8), std::int64_t, __int128_t>;
static constexpr std::uint32_t present = std::uint32_t(1) << 31, mask = present - 1;
static constexpr B first = std::numeric_limits<T>::lowest(), end = B(std::numeric_limits<T>::max()) + 1;
struct Line {
T a = 0, b = 0;
template <class V> V eval(T x) const { return V(R(a) * R(x) + R(b)); }
};
struct Node { Line line; std::uint32_t left = 0, right = 0; };
std::vector<Node> nodes_{Node{}};
std::uint32_t root_ = 0;
T low_, high_;
bool wide_ = false;
template <class V> static bool better(V a, V b) {
if constexpr (maximize) return a > b;
else return a < b;
}
std::uint32_t make_node(Line f = {}, bool has_line = false) {
assert(nodes_.size() < present);
nodes_.push_back({f, has_line ? present : 0, 0});
return std::uint32_t(nodes_.size() - 1);
}
template <class V> std::uint32_t insert(std::uint32_t k, T l, T r, Line f) {
if (!k) return make_node(f, true);
std::uint32_t result = k;
for (;;) {
auto& node = nodes_[k];
if (!(node.left & present)) {
node.line = f, node.left |= present;
return result;
}
auto& g = node.line;
if (f.a == g.a) {
if (better(f.b, g.b)) g = f;
return result;
}
T m = ordered_midpoint(l, r);
if (better(f.template eval<V>(m), g.template eval<V>(m))) std::swap(f, g);
if (l == r) return result;
bool right = better(f.a, g.a);
if (!better(f.template eval<V>(right ? r : l), g.template eval<V>(right ? r : l))) return result;
std::uint32_t child = right ? node.right : node.left & mask;
if (!child) {
child = make_node(f, true);
if (right) nodes_[k].right = child;
else nodes_[k].left = (nodes_[k].left & present) | child;
return result;
}
k = child;
if (right) l = T(m + 1);
else r = m;
}
}
template <class V> std::uint32_t insert_segment(std::uint32_t k, T l, T r, T ql, T qr, Line f) {
if (k && (nodes_[k].left & present)) {
const auto& g = nodes_[k].line;
if (f.a == g.a) {
if (!better(f.b, g.b)) return k;
} else {
T edge = better(f.a, g.a) ? std::min(r, qr) : std::max(l, ql);
if (!better(f.template eval<V>(edge), g.template eval<V>(edge))) return k;
}
}
if (ql <= l && r <= qr) return insert<V>(k, l, r, f);
if (!k) k = make_node();
T m = ordered_midpoint(l, r);
if (ql <= m) {
auto child = insert_segment<V>(nodes_[k].left & mask, l, m, ql, qr, f);
nodes_[k].left = (nodes_[k].left & present) | child;
}
if (m < qr) nodes_[k].right = insert_segment<V>(nodes_[k].right, T(m + 1), r, ql, qr, f);
return k;
}
void check(Line f, T l, T r) {
if constexpr (std::is_same_v<R, __int128_t>) if (!wide_) {
R a = f.template eval<R>(l), b = f.template eval<R>(r);
wide_ = a < std::numeric_limits<std::int64_t>::lowest() || a > std::numeric_limits<std::int64_t>::max()
|| b < std::numeric_limits<std::int64_t>::lowest() || b > std::numeric_limits<std::int64_t>::max();
}
}
template <class F> decltype(auto) dispatch(F f) const {
if constexpr (std::is_same_v<R, __int128_t>) if (!wide_) return f.template operator()<std::int64_t>();
return f.template operator()<R>();
}
template <class V> std::optional<R> query_impl(T x) const {
V result = maximize ? std::numeric_limits<V>::lowest() : std::numeric_limits<V>::max();
bool found = false;
T l = low_, r = high_;
for (std::uint32_t k = root_; k;) {
const auto& node = nodes_[k];
if (node.left & present) {
V value = node.line.template eval<V>(x);
if (better(value, result)) result = value;
found = true;
}
if (l == r) break;
T m = ordered_midpoint(l, r);
if (x <= m) k = node.left & mask, r = m;
else k = node.right, l = T(m + 1);
}
if (found) return R(result);
return std::nullopt;
}
public:
using value_type = R;
using bound_type = B;
explicit DynamicLiChaoTree(B lower = first, B upper = end) { build(lower, upper); }
void build(B lower, B upper) {
assert(first <= lower && lower < upper && upper <= end);
low_ = T(lower), high_ = T(upper - 1);
clear();
}
B lower() const { return B(low_); }
B upper() const { return B(high_) + 1; }
bool empty() const { return root_ == 0; }
std::size_t node_count() const { return nodes_.size() - 1; }
void reserve(std::size_t count) {
assert(count < present);
nodes_.reserve(count + 1);
}
void clear() { nodes_.resize(1), root_ = 0, wide_ = false; }
void add_line(T a, T b) {
Line f{a, b};
check(f, low_, high_);
dispatch([&]<class V>() { root_ = insert<V>(root_, low_, high_, f); });
}
void add_segment(B l, B r, T a, T b) {
assert(l <= r);
l = std::max(l, lower()), r = std::min(r, upper());
if (l >= r) return;
Line f{a, b};
T ql = T(l), qr = T(r - 1);
check(f, ql, qr);
dispatch([&]<class V>() { root_ = insert_segment<V>(root_, low_, high_, ql, qr, f); });
}
std::optional<R> query(T x) const {
assert(low_ <= x && x <= high_);
return dispatch([&]<class V>() { return query_impl<V>(x); });
}
};
#line 1 "components/fast_io.hpp"
#line 5 "components/fast_io.hpp"
#include <array>
#include <bit>
#line 8 "components/fast_io.hpp"
#include <charconv>
#line 11 "components/fast_io.hpp"
#include <cstdio>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#line 18 "components/fast_io.hpp"
#if defined(__unix__) || defined(__APPLE__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace fast_io_detail { struct BulkInput; struct BulkOutput; }
class FastInput {
friend struct fast_io_detail::BulkInput;
static constexpr std::size_t capacity = 1 << 16;
FILE *file = nullptr;
std::unique_ptr<char[]> buffer;
const char *current = "", *end = current;
std::string scratch;
void *mapping = nullptr;
std::size_t mapped_size = 0;
bool refill() {
if (!file) return false;
std::size_t n = std::fread(buffer.get(), 1, capacity, file);
current = buffer.get();
end = current + n;
return n;
}
static const char *scan(const char *p, const char *end) {
while (end - p >= 8) {
std::uint64_t x;
std::memcpy(&x, p, 8);
if ((x - 0x2121212121212121ULL) & ~x & 0x8080808080808080ULL) break;
p += 8;
}
while (p != end && static_cast<unsigned char>(*p) > ' ') ++p;
return p;
}
public:
explicit FastInput(FILE *input = stdin) {
#if defined(__unix__) || defined(__APPLE__)
struct stat status;
int fd = fileno(input);
auto offset = ftello(input);
if (fd >= 0 && offset >= 0 && !fstat(fd, &status) && S_ISREG(status.st_mode) && status.st_size > offset) {
mapped_size = status.st_size;
mapping = mmap(nullptr, mapped_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapping != MAP_FAILED) {
current = static_cast<const char *>(mapping) + offset;
end = static_cast<const char *>(mapping) + mapped_size;
return;
}
mapping = nullptr;
}
#endif
file = input;
buffer = std::make_unique_for_overwrite<char[]>(capacity);
}
explicit FastInput(std::string_view input) : current(input.empty() ? "" : input.data()), end(current + input.size()) {}
FastInput(const FastInput &) = delete;
FastInput &operator=(const FastInput &) = delete;
~FastInput() {
#if defined(__unix__) || defined(__APPLE__)
if (mapping) munmap(mapping, mapped_size);
#endif
}
std::string_view token() {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return {};
}
const char *begin = current;
current = scan(current, end);
if (current != end || !file) return {begin, std::size_t(current - begin)};
scratch.assign(begin, end);
while (refill()) {
current = scan(current, end);
scratch.append(buffer.get(), std::size_t(current - buffer.get()));
if (current != end) break;
}
return scratch;
}
template <class T>
bool read(T &value) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return false;
}
if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4 && std::endian::native == std::endian::little) {
if (end - current >= 11) {
std::uint64_t x;
std::memcpy(&x, current, 8);
if (!(((x + 0x4646464646464646ULL) | (x - 0x3030303030303030ULL)) & 0x8080808080808080ULL)) {
x &= 0x0f0f0f0f0f0f0f0fULL;
x = (x * 10 + (x >> 8)) & 0x00ff00ff00ff00ffULL;
x = (x * 100 + (x >> 16)) & 0x0000ffff0000ffffULL;
std::uint64_t v = std::uint32_t(x * 10000 + (x >> 32));
int n = 8;
for (; n < 10 && static_cast<unsigned char>(current[n]) - unsigned('0') < 10; ++n)
v = v * 10 + (current[n] - '0');
if (static_cast<unsigned char>(current[n]) <= ' ' && v <= 0xffffffffULL) {
current += n;
value = T(v);
return true;
}
}
}
}
T parsed{};
auto result = std::from_chars(current, end, parsed);
if (result.ec == std::errc{} && (result.ptr != end ? static_cast<unsigned char>(*result.ptr) <= ' ' : !file)) {
current = result.ptr;
value = parsed;
return true;
}
}
auto s = token();
if (s.empty()) return false;
if constexpr (std::is_same_v<T, bool>) {
assert(s == "0" || s == "1");
value = s[0] == '1';
} else if constexpr (std::is_integral_v<T>) {
auto result = std::from_chars(s.data(), s.data() + s.size(), value);
assert(result.ec == std::errc{} && result.ptr == s.data() + s.size());
(void)result;
} else value = T(s);
return true;
}
};
class FastOutput {
friend struct fast_io_detail::BulkOutput;
inline static constexpr auto quads = [] {
std::array<std::array<char, 4>, 10000> table{};
for (int i = 0; i < 10000; ++i)
table[i] = {char('0' + i / 1000), char('0' + i / 100 % 10), char('0' + i / 10 % 10), char('0' + i % 10)};
return table;
}();
FILE *file;
std::unique_ptr<char[]> buffer = std::make_unique_for_overwrite<char[]>(1 << 16);
std::size_t capacity = 1 << 16, length = 0;
char *reserve(std::size_t n) {
if (n > capacity - length) {
if (!flush()) throw std::runtime_error("output failed");
if (n > capacity) {
std::size_t next = std::max(n, 2 * capacity);
buffer = std::make_unique_for_overwrite<char[]>(next);
capacity = next;
}
}
return buffer.get() + length;
}
public:
explicit FastOutput(FILE *output = stdout) : file(output) {}
FastOutput(const FastOutput &) = delete;
FastOutput &operator=(const FastOutput &) = delete;
~FastOutput() { flush(); }
bool flush() {
std::size_t offset = 0;
while (offset < length) {
auto n = std::fwrite(buffer.get() + offset, 1, length - offset, file);
if (!n) {
std::memmove(buffer.get(), buffer.get() + offset, length - offset);
length -= offset;
return false;
}
offset += n;
}
length = 0;
return std::fflush(file) == 0;
}
void write(char c) { *reserve(1) = c; ++length; }
void write(__uint128_t value) {
char digits[40];
char *end = digits + sizeof(digits), *begin = end;
do { *--begin = char('0' + value % 10); value /= 10; } while (value);
write(std::string_view(begin, end - begin));
}
void write(__int128_t value) {
__uint128_t magnitude = __uint128_t(value);
if (value < 0) write('-'), magnitude = 0 - magnitude;
write(magnitude);
}
void write(std::string_view s) {
char *out = reserve(s.size());
if (!s.empty()) std::memcpy(out, s.data(), s.size());
length += s.size();
}
template <class T> requires std::is_integral_v<T>
void write(T value) {
char *out = reserve(32);
if constexpr (std::is_same_v<T, bool>) *out = '0' + value, ++length;
else if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4) {
char *p = out;
if (value >= 100000000) {
auto high = value / 100000000, low = value % 100000000;
p = std::to_chars(p, p + 2, high).ptr;
std::memcpy(p, quads[low / 10000].data(), 4);
std::memcpy(p + 4, quads[low % 10000].data(), 4);
p += 8;
} else if (value >= 10000) {
p = std::to_chars(p, p + 4, value / 10000).ptr;
std::memcpy(p, quads[value % 10000].data(), 4);
p += 4;
} else p = std::to_chars(p, p + 4, value).ptr;
length += p - out;
} else length += std::to_chars(out, out + 32, value).ptr - out;
}
template <class T> requires requires(const T &value, char *out) { value.decimal_capacity(); value.to_chars(out); }
void write(const T &value) {
char *out = reserve(value.decimal_capacity());
length += value.to_chars(out) - out;
}
template <class T>
void println(const T &value) { write(value); write('\n'); }
};
#line 4 "test/dynamic_li_chao_tree_line.test.cpp"
int main() {
FastInput input;
FastOutput output;
int n = 0, q = 0;
input.read(n), input.read(q);
DynamicLiChaoTree<> tree(-1000000000LL, 1000000001LL);
tree.reserve(n + q);
for (int i = 0; i < n; ++i) {
std::int64_t a = 0, b = 0;
input.read(a), input.read(b);
tree.add_line(a, b);
}
while (q--) {
int type = 0;
std::int64_t a = 0, b = 0;
input.read(type), input.read(a);
if (type == 0) input.read(b), tree.add_line(a, b);
else output.println(std::int64_t(*tree.query(a)));
}
}