競技プログラミング用 C++ ライブラリ
test/segtree_beats.test.cpp · GitHub で見る
#define PROBLEM "https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum"
#include "fixtures/beats.hpp"
#include "../components/fast_io.hpp"
int main() {
FastInput in;
FastOutput out;
int n = 0, q = 0;
in.read(n), in.read(q);
std::vector<clamp_beats::S> a(n);
for (auto &node : a) {
long long value = 0;
in.read(value);
node = clamp_beats::leaf(value);
}
clamp_beats::Tree seg(a);
while (q--) {
int t = 0, l = 0, r = 0;
in.read(t), in.read(l), in.read(r);
if (t == 3) out.println(seg.prod(l, r).sum);
else {
long long x = 0;
in.read(x);
auto f = clamp_beats::id();
if (t == 0) f.hi = x;
if (t == 1) f.lo = x;
if (t == 2) f.add = x;
seg.apply(l, r, f);
}
}
}
#line 1 "test/segtree_beats.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum"
#line 1 "test/fixtures/beats.hpp"
#line 1 "data_structure/segtree_beats.hpp"
#include <algorithm>
#include <bit>
#include <cassert>
#include <cstddef>
#include <limits>
#include <utility>
#include <vector>
template <class S, auto op, auto e, class F, auto mapping, auto composition, auto id>
class SegtreeBeats {
int n_ = 0, base_ = 1, log_ = 0;
std::vector<S> data_;
std::vector<F> lazy_;
std::vector<unsigned char> dirty_;
void check([[maybe_unused]] int l, [[maybe_unused]] int r) const {
assert(0 <= l && l <= r && r <= n_);
}
void pull(int k) { data_[k] = op(data_[2 * k], data_[2 * k + 1]); }
void all_apply(int k, const F &f) {
if (k < base_) {
lazy_[k] = dirty_[k] ? composition(f, lazy_[k]) : f;
dirty_[k] = 1;
}
if (mapping(f, data_[k])) return;
assert(k < base_);
push(k);
pull(k);
}
void push(int k) {
if (!dirty_[k]) return;
all_apply(2 * k, lazy_[k]);
all_apply(2 * k + 1, lazy_[k]);
dirty_[k] = 0;
}
void push_path(int k) {
for (int h = log_; h; --h) push(k >> h);
}
void push_range(int l, int r) {
for (int h = log_; h; --h) {
if (((l >> h) << h) != l) push(l >> h);
if (((r >> h) << h) != r) push((r - 1) >> h);
}
}
void pull_range(int l, int r) {
for (int h = 1; h <= log_; ++h) {
if (((l >> h) << h) != l) pull(l >> h);
if (((r >> h) << h) != r) pull((r - 1) >> h);
}
}
public:
SegtreeBeats() : SegtreeBeats(0) {}
explicit SegtreeBeats(int n, const S &value = e()) {
assert(0 <= n && n <= std::numeric_limits<int>::max() / 4);
build(std::vector<S>(n, value));
}
explicit SegtreeBeats(const std::vector<S> &a) { build(a); }
void build(const std::vector<S> &a) {
assert(a.size() <= std::size_t(std::numeric_limits<int>::max() / 4));
n_ = static_cast<int>(a.size());
base_ = static_cast<int>(std::bit_ceil(static_cast<unsigned>(std::max(1, n_))));
log_ = std::countr_zero(static_cast<unsigned>(base_));
data_.assign(2 * base_, e());
lazy_.assign(base_, id());
dirty_.assign(base_, 0);
std::copy(a.begin(), a.end(), data_.begin() + base_);
for (int k = base_ - 1; k; --k) pull(k);
}
int size() const { return n_; }
bool empty() const { return !n_; }
S all_prod() const { return data_[1]; }
S get(int p) {
assert(0 <= p && p < n_);
int k = p + base_;
push_path(k);
return data_[k];
}
void set(int p, const S &value) {
assert(0 <= p && p < n_);
int k = p + base_;
push_path(k);
data_[k] = value;
while (k >>= 1) pull(k);
}
S prod(int l, int r) {
check(l, r);
if (l == r) return e();
l += base_, r += base_;
push_range(l, r);
S left = e(), right = e();
while (l < r) {
if (l & 1) left = op(std::move(left), data_[l++]);
if (r & 1) right = op(data_[--r], std::move(right));
l >>= 1, r >>= 1;
}
return op(std::move(left), std::move(right));
}
void apply(const F &f) { if (n_) all_apply(1, f); }
void apply(int p, const F &f) {
assert(0 <= p && p < n_);
int k = p + base_;
push_path(k);
all_apply(k, f);
while (k >>= 1) pull(k);
}
void apply(int l, int r, const F &f) {
check(l, r);
if (l == r) return;
l += base_, r += base_;
push_range(l, r);
int first = l, last = r;
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1, r >>= 1;
}
pull_range(first, last);
}
template <class Predicate>
int max_right(int l, Predicate predicate) {
assert(0 <= l && l <= n_ && predicate(e()));
if (l == n_) return n_;
int k = l + base_;
push_path(k);
S value = e();
do {
while (!(k & 1)) k >>= 1;
S next = op(value, data_[k]);
if (!predicate(next)) {
while (k < base_) {
push(k);
k *= 2;
next = op(value, data_[k]);
if (predicate(next)) value = std::move(next), ++k;
}
return std::min(k - base_, n_);
}
value = std::move(next);
++k;
} while ((k & -k) != k);
return n_;
}
template <class Predicate>
int min_left(int r, Predicate predicate) {
assert(0 <= r && r <= n_ && predicate(e()));
if (!r) return 0;
int k = r + base_;
push_path(k - 1);
S value = e();
do {
--k;
while (k > 1 && (k & 1)) k >>= 1;
S next = op(data_[k], value);
if (!predicate(next)) {
while (k < base_) {
push(k);
k = 2 * k + 1;
next = op(data_[k], value);
if (predicate(next)) value = std::move(next), --k;
}
return k + 1 - base_;
}
value = std::move(next);
} while ((k & -k) != k);
return 0;
}
std::vector<S> values() {
for (int k = 1; k < base_; ++k) push(k);
return std::vector<S>(data_.begin() + base_, data_.begin() + base_ + n_);
}
};
#line 5 "test/fixtures/beats.hpp"
#include <cstdint>
namespace division_beats {
struct S { long long sum, lo, hi; int len; };
struct F { long long value; unsigned long long divisor; };
inline S e() { return {0, 0, 0, 0}; }
inline S leaf(long long value) { return {value, value, value, 1}; }
inline S op(const S &a, const S &b) {
if (!a.len) return b;
if (!b.len) return a;
return {a.sum + b.sum, std::min(a.lo, b.lo), std::max(a.hi, b.hi), a.len + b.len};
}
inline F id() { return {-1, 1}; }
inline F composition(const F &f, const F &g) {
if (f.value >= 0) return f;
if (g.value >= 0) return {static_cast<long long>(g.value / f.divisor), 1};
__uint128_t d = __uint128_t(f.divisor) * g.divisor;
return {-1, d > ~0ULL ? ~0ULL : static_cast<unsigned long long>(d)};
}
inline bool mapping(const F &f, S &a) {
if (!a.len) return true;
if (f.value < 0 && f.divisor == 1) return true;
long long lo = f.value < 0 ? a.lo / f.divisor : f.value;
long long hi = f.value < 0 ? a.hi / f.divisor : f.value;
if (lo != hi) return false;
a.sum = lo * a.len;
a.lo = a.hi = lo;
return true;
}
using Tree = SegtreeBeats<S, op, e, F, mapping, composition, id>;
}
namespace bitwise_beats {
struct S { unsigned long long sum; unsigned lo, hi, bit_and, bit_or; int len; };
struct F { unsigned bit_and, bit_or; };
inline S e() { return {0, ~0U, 0, ~0U, 0, 0}; }
inline S leaf(unsigned value) { return {value, value, value, value, value, 1}; }
inline S op(const S &a, const S &b) {
return {a.sum + b.sum, std::min(a.lo, b.lo), std::max(a.hi, b.hi),
a.bit_and & b.bit_and, a.bit_or | b.bit_or, a.len + b.len};
}
inline F id() { return {~0U, 0}; }
inline F composition(const F &f, const F &g) {
return {g.bit_and & f.bit_and, (g.bit_or & f.bit_and) | f.bit_or};
}
inline bool mapping(const F &f, S &a) {
if (!a.len) return true;
if ((a.bit_and ^ a.bit_or) & (~f.bit_and | f.bit_or)) return false;
a.sum -= static_cast<unsigned long long>(a.bit_and) * a.len;
a.bit_and = (a.bit_and & f.bit_and) | f.bit_or;
a.sum += static_cast<unsigned long long>(a.bit_and) * a.len;
a.bit_or = (a.bit_or & f.bit_and) | f.bit_or;
a.lo = (a.lo & f.bit_and) | f.bit_or;
a.hi = (a.hi & f.bit_and) | f.bit_or;
return true;
}
using Tree = SegtreeBeats<S, op, e, F, mapping, composition, id>;
}
namespace clamp_beats {
inline constexpr long long inf = 1LL << 60;
struct S {
long long sum, lo, hi, lo2, hi2;
int nlo, nhi, len;
};
struct F { long long add, lo, hi; };
inline S e() { return {0, inf, -inf, inf, -inf, 0, 0, 0}; }
inline S leaf(long long value) { return {value, value, value, inf, -inf, 1, 1, 1}; }
inline S op(const S &a, const S &b) {
if (!a.len) return b;
if (!b.len) return a;
S c;
c.sum = a.sum + b.sum;
c.len = a.len + b.len;
if (a.lo < b.lo) c.lo = a.lo, c.lo2 = std::min(a.lo2, b.lo), c.nlo = a.nlo;
else if (a.lo > b.lo) c.lo = b.lo, c.lo2 = std::min(a.lo, b.lo2), c.nlo = b.nlo;
else c.lo = a.lo, c.lo2 = std::min(a.lo2, b.lo2), c.nlo = a.nlo + b.nlo;
if (a.hi > b.hi) c.hi = a.hi, c.hi2 = std::max(a.hi2, b.hi), c.nhi = a.nhi;
else if (a.hi < b.hi) c.hi = b.hi, c.hi2 = std::max(a.hi, b.hi2), c.nhi = b.nhi;
else c.hi = a.hi, c.hi2 = std::max(a.hi2, b.hi2), c.nhi = a.nhi + b.nhi;
return c;
}
inline F id() { return {0, -inf, inf}; }
inline F composition(const F &f, const F &g) {
auto shift = [&](long long x) { return x == -inf || x == inf ? x : x + f.add; };
return {g.add + f.add, std::clamp(shift(g.lo), f.lo, f.hi), std::clamp(shift(g.hi), f.lo, f.hi)};
}
inline bool mapping(const F &f, S &a) {
if (!a.len) return true;
long long lo = std::clamp(a.lo + f.add, f.lo, f.hi), hi = std::clamp(a.hi + f.add, f.lo, f.hi);
if (lo == hi) {
a = {lo * a.len, lo, lo, inf, -inf, a.len, a.len, a.len};
return true;
}
if ((f.lo > a.lo + f.add && f.lo >= a.lo2 + f.add) ||
(f.hi < a.hi + f.add && f.hi <= a.hi2 + f.add)) return false;
a.sum += f.add * a.len;
a.lo += f.add, a.hi += f.add;
if (a.lo2 != inf) a.lo2 += f.add;
if (a.hi2 != -inf) a.hi2 += f.add;
if (a.hi > f.hi) {
a.sum += (f.hi - a.hi) * a.nhi;
if (a.lo2 == a.hi) a.lo2 = f.hi;
a.hi = f.hi;
}
if (a.lo < f.lo) {
a.sum += (f.lo - a.lo) * a.nlo;
if (a.hi2 == a.lo) a.hi2 = f.lo;
a.lo = f.lo;
}
return true;
}
using Tree = SegtreeBeats<S, op, e, F, mapping, composition, id>;
}
#line 1 "components/fast_io.hpp"
#line 5 "components/fast_io.hpp"
#include <array>
#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>
#include <type_traits>
#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/segtree_beats.test.cpp"
int main() {
FastInput in;
FastOutput out;
int n = 0, q = 0;
in.read(n), in.read(q);
std::vector<clamp_beats::S> a(n);
for (auto &node : a) {
long long value = 0;
in.read(value);
node = clamp_beats::leaf(value);
}
clamp_beats::Tree seg(a);
while (q--) {
int t = 0, l = 0, r = 0;
in.read(t), in.read(l), in.read(r);
if (t == 3) out.println(seg.prod(l, r).sum);
else {
long long x = 0;
in.read(x);
auto f = clamp_beats::id();
if (t == 0) f.hi = x;
if (t == 1) f.lo = x;
if (t == 2) f.add = x;
seg.apply(l, r, f);
}
}
}