This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
// beats!
#include <iostream>
#include "src/DataStructure/SegmentTree.hpp"
using namespace std;
struct Mono {
static constexpr long long INF= 1ll << 62;
struct T {
long long sum, h, l, h2, l2;
int hc, lc;
};
struct E { // clamp(x,lb,ub)+ad
long long lb, ub, ad;
static E add(long long x) { return {-INF, INF, x}; }
static E chmin(long long x) { return {-INF, x, 0}; }
static E chmax(long long x) { return {x, INF, 0}; }
};
static long long min2(long long a, long long a2, long long b, long long b2) { return a == b ? min(a2, b2) : a2 <= b ? a2 : b2 <= a ? b2 : max(a, b); }
static long long max2(long long a, long long a2, long long b, long long b2) { return a == b ? max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : min(a, b); }
static T ti() { return {0, -INF, INF, -INF, INF, 0, 0}; }
static T op(const T &vl, const T &vr) { return {vl.sum + vr.sum, max(vl.h, vr.h), min(vl.l, vr.l), max2(vl.h, vl.h2, vr.h, vr.h2), min2(vl.l, vl.l2, vr.l, vr.l2), vl.hc * (vl.h >= vr.h) + vr.hc * (vl.h <= vr.h), vl.lc * (vl.l <= vr.l) + vr.lc * (vl.l >= vr.l)}; }
static bool mp(T &v, const E &f, int sz) {
if (v.h <= f.lb) {
v.sum= (v.h= v.l= f.lb + f.ad) * (v.hc= v.lc= sz);
v.h2= -INF, v.l2= INF;
return true;
}
if (v.l >= f.ub) {
v.sum= (v.h= v.l= f.ub + f.ad) * (v.hc= v.lc= sz);
v.h2= -INF, v.l2= INF;
return true;
}
if (f.lb <= v.l && v.h <= f.ub) {
v.sum+= f.ad * sz, v.h+= f.ad, v.l+= f.ad;
v.h2+= f.ad, v.l2+= f.ad;
return true;
}
if (v.h2 <= f.lb) {
v.l= v.h2= f.lb + f.ad, v.lc= sz - v.hc;
v.l2= v.h= min(f.ub, v.h) + f.ad;
v.sum= v.h * v.hc + v.l * v.lc;
return true;
}
if (v.l2 >= f.ub) {
v.h= v.l2= f.ub + f.ad, v.hc= sz - v.lc;
v.h2= v.l= max(f.lb, v.l) + f.ad;
v.sum= v.h * v.hc + v.l * v.lc;
return true;
}
return false;
}
static void cp(E &pre, const E &suf) {
if (auto tmpl= suf.lb - pre.ad; pre.ub <= tmpl) pre.ub= pre.lb= tmpl;
else if (auto tmpu= suf.ub - pre.ad; tmpu <= pre.lb) pre.ub= pre.lb= tmpu;
else pre.lb= max(pre.lb, tmpl), pre.ub= min(pre.ub, tmpu);
pre.ad+= suf.ad;
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
SegmentTree<Mono> seg(N);
for (int i= 0; i < N; i++) {
long long a;
cin >> a;
seg.unsafe_set(i, {a, a, a, -Mono::INF, Mono::INF, 1, 1});
}
seg.build();
while (Q--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 3) {
cout << seg.prod(l, r).sum << '\n';
} else {
long long b;
cin >> b;
if (op == 0) seg.apply(l, r, Mono::E::chmin(b));
if (op == 1) seg.apply(l, r, Mono::E::chmax(b));
if (op == 2) seg.apply(l, r, Mono::E::add(b));
}
}
return 0;
}
#line 1 "test/yosupo/range_chmin_chmax_add_range_sum.Beats.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
// beats!
#include <iostream>
#line 2 "src/DataStructure/SegmentTree.hpp"
#include <memory>
#include <cassert>
#include <vector>
#include <algorithm>
#line 2 "src/Internal/detection_idiom.hpp"
#include <type_traits>
#define _DETECT_BOOL(name, ...) \
template <class, class= void> struct name: std::false_type {}; \
template <class T> struct name<T, std::void_t<__VA_ARGS__>>: std::true_type {}; \
template <class T> static constexpr bool name##_v= name<T>::value
#define _DETECT_TYPE(name, type1, type2, ...) \
template <class T, class= void> struct name { \
using type= type2; \
}; \
template <class T> struct name<T, std::void_t<__VA_ARGS__>> { \
using type= type1; \
}
#line 7 "src/DataStructure/SegmentTree.hpp"
template <class M> class SegmentTree {
_DETECT_BOOL(monoid, typename T::T, decltype(&T::op), decltype(&T::ti));
_DETECT_BOOL(dual, typename T::T, typename T::E, decltype(&T::mp), decltype(&T::cp));
_DETECT_TYPE(nullptr_or_E, typename T::E, std::nullptr_t, typename T::E);
using T= typename M::T;
using E= typename nullptr_or_E<M>::type;
int n;
std::unique_ptr<T[]> dat;
std::unique_ptr<E[]> laz;
std::unique_ptr<bool[]> flg;
inline void update(int k) { dat[k]= M::op(dat[k << 1], dat[k << 1 | 1]); }
inline bool map(int k, E x, int sz) {
if constexpr (std::is_invocable_r_v<bool, decltype(M::mp), T &, E, int>) return M::mp(dat[k], x, sz);
else if constexpr (std::is_invocable_r_v<bool, decltype(M::mp), T &, E>) return M::mp(dat[k], x);
else if constexpr (std::is_invocable_r_v<void, decltype(M::mp), T &, E, int>) return M::mp(dat[k], x, sz), true;
else return M::mp(dat[k], x), true;
}
inline void prop(int k, E x, int sz) {
if (k < n) {
if (flg[k]) M::cp(laz[k], x);
else laz[k]= x;
flg[k]= true;
if constexpr (monoid_v<M>)
if (!map(k, x, sz)) push(k, sz), update(k);
} else {
if constexpr (monoid_v<M>) map(k, x, 1);
else map(k - n, x, 1);
}
}
inline void push(int k, int sz) {
if (flg[k]) prop(k << 1, laz[k], sz >> 1), prop(k << 1 | 1, laz[k], sz >> 1), flg[k]= false;
}
inline bool valid(int k) const {
int d= __builtin_clz(k) - __builtin_clz(n);
return (n >> d) != k || ((n >> d) << d) == n;
}
public:
SegmentTree() {}
SegmentTree(int n): n(n), dat(std::make_unique<T[]>(n << monoid_v<M>)) {
if constexpr (monoid_v<M>) std::fill_n(dat.get(), n << 1, M::ti());
if constexpr (dual_v<M>) laz= std::make_unique<E[]>(n), flg= std::make_unique<bool[]>(n), std::fill_n(flg.get(), n, false);
}
template <class F> SegmentTree(int n, const F &init): n(n), dat(std::make_unique<T[]>(n << monoid_v<M>)) {
auto a= dat.get() + (n & -monoid_v<M>);
for (int i= 0; i < n; ++i) a[i]= init(i);
if constexpr (monoid_v<M>) build();
if constexpr (dual_v<M>) laz= std::make_unique<E[]>(n), flg= std::make_unique<bool[]>(n), std::fill_n(flg.get(), n, false);
}
SegmentTree(int n, T x): SegmentTree(n, [x](int) { return x; }) {}
SegmentTree(const std::vector<T> &v): SegmentTree(v.size(), [&v](int i) { return v[i]; }) {}
SegmentTree(const T *bg, const T *ed): SegmentTree(ed - bg, [bg](int i) { return bg[i]; }) {}
void build() {
static_assert(monoid_v<M>, "\"build\" is not available\n");
for (int i= n; --i;) update(i);
}
inline void unsafe_set(int i, T x) {
static_assert(monoid_v<M>, "\"unsafe_set\" is not available\n");
dat[i + n]= x;
}
inline void set(int i, T x) {
get(i);
if constexpr (monoid_v<M>)
for (dat[i+= n]= x; i>>= 1;) update(i);
else dat[i]= x;
}
inline void mul(int i, T x) {
static_assert(monoid_v<M>, "\"mul\" is not available\n");
set(i, M::op(get(i), x));
}
inline T get(int i) {
i+= n;
if constexpr (dual_v<M>)
for (int j= 31 - __builtin_clz(i); j; --j) push(i >> j, 1 << j);
if constexpr (monoid_v<M>) return dat[i];
else return dat[i - n];
}
inline T operator[](int i) { return get(i); }
inline T prod(int l, int r) {
static_assert(monoid_v<M>, "\"prod\" is not available\n");
l+= n, r+= n;
if constexpr (dual_v<M>) {
for (int j= 31 - __builtin_clz(l); ((l >> j) << j) != l; --j) push(l >> j, 1 << j);
for (int j= 31 - __builtin_clz(r); ((r >> j) << j) != r; --j) push(r >> j, 1 << j);
}
T s1= M::ti(), s2= M::ti();
for (; l < r; l>>= 1, r>>= 1) {
if (l & 1) s1= M::op(s1, dat[l++]);
if (r & 1) s2= M::op(dat[--r], s2);
}
return M::op(s1, s2);
}
inline void apply(int l, int r, E x) {
static_assert(dual_v<M>, "\"apply\" is not available\n");
l+= n, r+= n;
for (int j= 31 - __builtin_clz(l); ((l >> j) << j) != l; j--) push(l >> j, 1 << j);
for (int j= 31 - __builtin_clz(r); ((r >> j) << j) != r; j--) push(r >> j, 1 << j);
for (int a= l, b= r, sz= 1; a < b; a>>= 1, b>>= 1, sz<<= 1) {
if (a & 1) prop(a++, x, sz);
if (b & 1) prop(--b, x, sz);
}
if constexpr (monoid_v<M>) {
for (int j= __builtin_ctz(l) + 1; l >> j; ++j) update(l >> j);
for (int j= __builtin_ctz(r) + 1; r >> j; ++j) update(r >> j);
}
}
template <class C> int max_right(int l, const C &check) {
static_assert(monoid_v<M>, "\"max_right\" is not available\n");
assert(check(M::ti()));
if (check(prod(l, n))) return n;
T s= M::ti(), t;
int sz= 1;
for (get(l), l+= n;; s= t, ++l) {
while (!(l & 1) && valid(l >> 1)) l>>= 1, sz<<= 1;
if (!check(t= M::op(s, dat[l]))) {
while (l < n) {
if constexpr (dual_v<M>) push(l, sz);
l<<= 1, sz>>= 1;
if (check(t= M::op(s, dat[l]))) s= t, ++l;
}
return l - n;
}
}
}
template <class C> int min_left(int r, const C &check) {
static_assert(monoid_v<M>, "\"min_left\" is not available\n");
assert(check(M::ti()));
if (check(prod(0, r))) return 0;
T s= M::ti(), t;
int sz= 1;
for (get(--r), r+= n;; s= t, --r) {
while (!valid(r)) r= r << 1 | 1, sz>>= 1;
while ((r & 1) && valid(r >> 1)) r>>= 1, sz<<= 1;
if (!check(t= M::op(dat[r], s))) {
while (r < n) {
if constexpr (dual_v<M>) push(r, sz);
r= r << 1 | 1, sz>>= 1;
if (check(t= M::op(dat[r], s))) s= t, --r;
}
return r + 1 - n;
}
}
}
};
#line 7 "test/yosupo/range_chmin_chmax_add_range_sum.Beats.test.cpp"
using namespace std;
struct Mono {
static constexpr long long INF= 1ll << 62;
struct T {
long long sum, h, l, h2, l2;
int hc, lc;
};
struct E { // clamp(x,lb,ub)+ad
long long lb, ub, ad;
static E add(long long x) { return {-INF, INF, x}; }
static E chmin(long long x) { return {-INF, x, 0}; }
static E chmax(long long x) { return {x, INF, 0}; }
};
static long long min2(long long a, long long a2, long long b, long long b2) { return a == b ? min(a2, b2) : a2 <= b ? a2 : b2 <= a ? b2 : max(a, b); }
static long long max2(long long a, long long a2, long long b, long long b2) { return a == b ? max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : min(a, b); }
static T ti() { return {0, -INF, INF, -INF, INF, 0, 0}; }
static T op(const T &vl, const T &vr) { return {vl.sum + vr.sum, max(vl.h, vr.h), min(vl.l, vr.l), max2(vl.h, vl.h2, vr.h, vr.h2), min2(vl.l, vl.l2, vr.l, vr.l2), vl.hc * (vl.h >= vr.h) + vr.hc * (vl.h <= vr.h), vl.lc * (vl.l <= vr.l) + vr.lc * (vl.l >= vr.l)}; }
static bool mp(T &v, const E &f, int sz) {
if (v.h <= f.lb) {
v.sum= (v.h= v.l= f.lb + f.ad) * (v.hc= v.lc= sz);
v.h2= -INF, v.l2= INF;
return true;
}
if (v.l >= f.ub) {
v.sum= (v.h= v.l= f.ub + f.ad) * (v.hc= v.lc= sz);
v.h2= -INF, v.l2= INF;
return true;
}
if (f.lb <= v.l && v.h <= f.ub) {
v.sum+= f.ad * sz, v.h+= f.ad, v.l+= f.ad;
v.h2+= f.ad, v.l2+= f.ad;
return true;
}
if (v.h2 <= f.lb) {
v.l= v.h2= f.lb + f.ad, v.lc= sz - v.hc;
v.l2= v.h= min(f.ub, v.h) + f.ad;
v.sum= v.h * v.hc + v.l * v.lc;
return true;
}
if (v.l2 >= f.ub) {
v.h= v.l2= f.ub + f.ad, v.hc= sz - v.lc;
v.h2= v.l= max(f.lb, v.l) + f.ad;
v.sum= v.h * v.hc + v.l * v.lc;
return true;
}
return false;
}
static void cp(E &pre, const E &suf) {
if (auto tmpl= suf.lb - pre.ad; pre.ub <= tmpl) pre.ub= pre.lb= tmpl;
else if (auto tmpu= suf.ub - pre.ad; tmpu <= pre.lb) pre.ub= pre.lb= tmpu;
else pre.lb= max(pre.lb, tmpl), pre.ub= min(pre.ub, tmpu);
pre.ad+= suf.ad;
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
SegmentTree<Mono> seg(N);
for (int i= 0; i < N; i++) {
long long a;
cin >> a;
seg.unsafe_set(i, {a, a, a, -Mono::INF, Mono::INF, 1, 1});
}
seg.build();
while (Q--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 3) {
cout << seg.prod(l, r).sum << '\n';
} else {
long long b;
cin >> b;
if (op == 0) seg.apply(l, r, Mono::E::chmin(b));
if (op == 1) seg.apply(l, r, Mono::E::chmax(b));
if (op == 2) seg.apply(l, r, Mono::E::add(b));
}
}
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | example_00 |
![]() |
5 ms | 4 MB |
g++-13 | max_random_00 |
![]() |
330 ms | 27 MB |
g++-13 | max_random_01 |
![]() |
338 ms | 27 MB |
g++-13 | max_random_02 |
![]() |
335 ms | 27 MB |
g++-13 | medium_00 |
![]() |
6 ms | 4 MB |
g++-13 | medium_01 |
![]() |
5 ms | 4 MB |
g++-13 | medium_02 |
![]() |
5 ms | 4 MB |
g++-13 | random2_00 |
![]() |
410 ms | 27 MB |
g++-13 | random2_01 |
![]() |
403 ms | 27 MB |
g++-13 | random2_02 |
![]() |
402 ms | 27 MB |
g++-13 | random3_00 |
![]() |
479 ms | 27 MB |
g++-13 | random3_01 |
![]() |
481 ms | 27 MB |
g++-13 | random3_02 |
![]() |
498 ms | 27 MB |
g++-13 | random_00 |
![]() |
231 ms | 18 MB |
g++-13 | random_01 |
![]() |
245 ms | 21 MB |
g++-13 | random_02 |
![]() |
170 ms | 10 MB |
g++-13 | small_00 |
![]() |
5 ms | 4 MB |
g++-13 | small_01 |
![]() |
4 ms | 4 MB |
g++-13 | small_02 |
![]() |
4 ms | 4 MB |
g++-13 | small_03 |
![]() |
4 ms | 4 MB |
g++-13 | small_04 |
![]() |
4 ms | 4 MB |
g++-13 | small_05 |
![]() |
4 ms | 4 MB |
g++-13 | small_06 |
![]() |
4 ms | 4 MB |
g++-13 | small_07 |
![]() |
4 ms | 4 MB |
g++-13 | small_08 |
![]() |
4 ms | 4 MB |
g++-13 | small_09 |
![]() |
4 ms | 4 MB |
g++-13 | small_absolute_values_00 |
![]() |
226 ms | 18 MB |
g++-13 | small_absolute_values_01 |
![]() |
233 ms | 21 MB |
g++-13 | small_absolute_values_02 |
![]() |
163 ms | 10 MB |
clang++-18 | example_00 |
![]() |
5 ms | 4 MB |
clang++-18 | max_random_00 |
![]() |
333 ms | 27 MB |
clang++-18 | max_random_01 |
![]() |
340 ms | 27 MB |
clang++-18 | max_random_02 |
![]() |
334 ms | 27 MB |
clang++-18 | medium_00 |
![]() |
7 ms | 4 MB |
clang++-18 | medium_01 |
![]() |
7 ms | 4 MB |
clang++-18 | medium_02 |
![]() |
6 ms | 4 MB |
clang++-18 | random2_00 |
![]() |
416 ms | 27 MB |
clang++-18 | random2_01 |
![]() |
413 ms | 27 MB |
clang++-18 | random2_02 |
![]() |
406 ms | 27 MB |
clang++-18 | random3_00 |
![]() |
482 ms | 27 MB |
clang++-18 | random3_01 |
![]() |
485 ms | 27 MB |
clang++-18 | random3_02 |
![]() |
495 ms | 27 MB |
clang++-18 | random_00 |
![]() |
234 ms | 18 MB |
clang++-18 | random_01 |
![]() |
242 ms | 21 MB |
clang++-18 | random_02 |
![]() |
172 ms | 10 MB |
clang++-18 | small_00 |
![]() |
5 ms | 4 MB |
clang++-18 | small_01 |
![]() |
4 ms | 4 MB |
clang++-18 | small_02 |
![]() |
4 ms | 4 MB |
clang++-18 | small_03 |
![]() |
4 ms | 4 MB |
clang++-18 | small_04 |
![]() |
4 ms | 4 MB |
clang++-18 | small_05 |
![]() |
4 ms | 4 MB |
clang++-18 | small_06 |
![]() |
4 ms | 4 MB |
clang++-18 | small_07 |
![]() |
4 ms | 4 MB |
clang++-18 | small_08 |
![]() |
4 ms | 4 MB |
clang++-18 | small_09 |
![]() |
4 ms | 4 MB |
clang++-18 | small_absolute_values_00 |
![]() |
229 ms | 18 MB |
clang++-18 | small_absolute_values_01 |
![]() |
232 ms | 21 MB |
clang++-18 | small_absolute_values_02 |
![]() |
164 ms | 10 MB |