:heavy_check_mark: test/library_checker/data_structure/range_chmin_chmax_add_range_sum.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum
#include "../../../data-structure/range-chminmaxaddsum-segtree-beats.hpp"
void solve() {
    LL(n, q);
    vector<S> a(n);
    rep(i, n) {
        LL(ai);
        a[i] = S(ai, 1);
    }
    segtree seg(a);
    rep(_, q) {
        LL(flag, l, r);
        if(flag == 0) {
            LL(b);
            seg.apply(l, r, F(b, -inf, 0));
        } else if(flag == 1) {
            LL(b);
            seg.apply(l, r, F(inf, b, 0));
        } else if(flag == 2) {
            LL(b);
            seg.apply(l, r, F(inf, -inf, b));
        } else {
            print(seg.prod(l, r).sum);
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    solve();
}
#line 1 "test/library_checker/data_structure/range_chmin_chmax_add_range_sum.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum
#line 2 "other/fastio.hpp"
// ref: https://maspypy.com/library-checker-many-a-b , Nyaanさん
#line 2 "other/type-utils.hpp"
#include <bits/stdc++.h>
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using vi = std::vector<int>;
using vii = std::vector<std::vector<int>>;
using pii = std::pair<int, int>;
using vl = std::vector<ll>;
using vll = std::vector<vl>;
using pll = std::pair<ll, ll>;

template <class T>
concept extended_integral =
    std::integral<T> || std::same_as<std::remove_cv_t<T>, i128> ||
    std::same_as<std::remove_cv_t<T>, u128>;
template <class T>
concept extended_signed_integral =
    std::signed_integral<T> || std::same_as<std::remove_cv_t<T>, i128>;
template <class T>
concept extended_unsigned_integral =
    std::unsigned_integral<T> || std::same_as<std::remove_cv_t<T>, u128>;

template <class T>
concept Streamable =
    requires(std::ostream &os, T &x) { os << x; } || extended_integral<T>;
template <class mint>
concept is_modint = requires(mint &x) {
    { x.val() } -> std::convertible_to<int>;
};
#line 4 "other/fastio.hpp"
namespace fastio {
constexpr int SZ = 1 << 17;
constexpr int offset = 64;
constexpr int mod = 10000;
char in_buf[SZ];
int in_left{}, in_right{};
char out_buf[SZ];
char out_tmp[offset];
int out_right{};
struct Pre {
    char num[4 * mod]{};
    constexpr Pre() {
        for(int i = 0; i < mod; ++i) {
            for(int n = i, j = 3; j >= 0; --j, n /= 10)
                num[4 * i + j] = '0' + n % 10;
        }
    }
    constexpr const char *operator[](int i) const { return &num[4 * i]; }
} constexpr pre;
void load() {
    memmove(in_buf, in_buf + in_left, in_right - in_left);
    in_right += -in_left + std::fread(in_buf + in_right - in_left, 1,
                                      SZ - (in_right - in_left), stdin);
    in_left = 0;
    if(in_right < SZ)
        in_buf[in_right++] = '\n';
}
void read(char &c) {
    do {
        if(in_left == in_right)
            load();
        c = in_buf[in_left++];
    } while(isspace(c));
}
void read(std::string &s) {
    s.clear();
    char c;
    do {
        if(in_left == in_right)
            load();
        c = in_buf[in_left++];
    } while(isspace(c));
    do {
        s += c;
        if(in_left == in_right)
            load();
        c = in_buf[in_left++];
    } while(!isspace(c));
}
template <extended_integral T> void read(T &x) {
    if(in_right - in_left < offset)
        load();
    char c;
    do
        c = in_buf[in_left++];
    while(c < '-'); // \n:10 space:32 -:45 '0':48
    bool minus{};
    if constexpr(extended_signed_integral<T>) {
        if(c == '-') {
            c = in_buf[in_left++];
            minus = true;
        }
    }
    x = 0;
    while(c >= '0') {
        x = 10 * x + (c & 15);
        c = in_buf[in_left++];
    }
    if constexpr(extended_signed_integral<T>) {
        if(minus)
            x = -x;
    }
}
void flush() { fwrite(out_buf, 1, std::exchange(out_right, 0), stdout); }
void write_range(const char *c, int n) {
    int pos{};
    while(pos < n) {
        if(out_right == SZ)
            flush();
        int len = std::min(n - pos, SZ - out_right);
        memcpy(out_buf + out_right, c + pos, len);
        out_right += len;
        pos += len;
    }
}

void write(char c) {
    if(SZ == out_right)
        flush();
    out_buf[out_right++] = c;
}
void write(const char *c) { write_range(c, strlen(c)); }
void write(const std::string &s) { write_range(s.data(), s.size()); }
template <std::floating_point T> void write(T x) {
    int n = std::snprintf(out_tmp, sizeof(out_tmp), "%.16g", x);
    write_range(out_tmp, n);
}
void write(bool x) { write(x ? '1' : '0'); }
template <extended_integral T> void write(T x) {
    if(x == 0) {
        write('0');
    }
    if constexpr(extended_signed_integral<T>) {
        if(x < 0) {
            write('-');
            x = -x;
        }
    }
    if(SZ - out_right < offset)
        flush();
    int cur = offset;
    for(; x >= 1000; x /= mod) {
        cur -= 4;
        memcpy(out_tmp + cur, pre[x % mod], 4);
    }
    if(x >= 100) {
        cur -= 3;
        memcpy(out_tmp + cur, pre[x % mod] + 1, 3);
    } else if(x >= 10) {
        cur -= 2;
        memcpy(out_tmp + cur, pre[x % mod] + 2, 2);
    } else if(x >= 1) {
        cur -= 1;
        memcpy(out_tmp + cur, pre[x % mod] + 3, 1);
    }
    write_range(out_tmp + cur, offset - cur);
}
struct Dummy {
    // プログラム終了時に出力
    ~Dummy() { flush(); }
} dummy;
} // namespace fastio
using fastio::write;
#line 4 "template.hpp"
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#line 8 "template.hpp"
using namespace std;
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif
template <Streamable T> void print_one(const T &value) { fastio::write(value); }
template <is_modint T> void print_one(const T &value) {
    print_one(value.val());
}
void print() { print_one('\n'); }
template <class T, class... Ts> void print(const T &a, const Ts &...b) {
    print_one(a);
    ((print_one(' '), print_one(b)), ...);
    print();
}
template <ranges::range Iterable>
    requires(!Streamable<Iterable>)
void print(const Iterable &v) {
    for(auto it = v.begin(); it != v.end(); ++it) {
        if(it != v.begin())
            print_one(' ');
        print_one(*it);
    }
    print();
}
#define all(v) begin(v), end(v)
template <class T> void UNIQUE(T &v) {
    ranges::sort(v);
    v.erase(unique(all(v)), end(v));
}
template <typename T> inline bool chmax(T &a, T b) {
    return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
    return ((a > b) ? (a = b, true) : (false));
}
// https://trap.jp/post/1224/
template <class... T> constexpr auto min(T... a) {
    return min(initializer_list<common_type_t<T...>>{a...});
}
template <class... T> constexpr auto max(T... a) {
    return max(initializer_list<common_type_t<T...>>{a...});
}
void input() {}
template <class Head, class... Tail> void input(Head &head, Tail &...tail) {
#ifdef LOCAL
    cin >> head;
#else
    fastio::read(head);
#endif
    input(tail...);
}
template <class T> void input(vector<T> &a) {
    for(T &x : a)
        input(x);
}
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    long long __VA_ARGS__;                                                     \
    input(__VA_ARGS__)
#define STR(...)                                                               \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define REP1_0(n, c) REP1_1(n, c)
#define REP1_1(n, c)                                                           \
    for(ll REP_COUNTER_##c = 0; REP_COUNTER_##c < (ll)(n); REP_COUNTER_##c++)
#define REP1(n) REP1_0(n, __COUNTER__)
#define REP2(i, a) for(ll i = 0; i < (ll)(a); i++)
#define REP3(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define REP4(i, a, b, c) for(ll i = (ll)(a); i < (ll)(b); i += (ll)(c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
ll inf = 3e18;
vl dx = {1, -1, 0, 0};
vl dy = {0, 0, 1, -1};
template <class T> constexpr T floor(T x, T y) noexcept {
    return x / y - ((x ^ y) < 0 and x % y);
}
template <class T> constexpr T ceil(T x, T y) noexcept {
    return x / y + ((x ^ y) >= 0 and x % y);
}
// yの符号に関わらず非負で定義 \bmod:texコマンド
template <class T> constexpr T bmod(T x, T y) noexcept {
    T m = x % y;
    return (m < 0) ? m + (y > 0 ? y : -y) : m;
}
template <std::signed_integral T> constexpr int bit_width(T x) noexcept {
    return std::bit_width((uint64_t)x);
}
template <std::signed_integral T> constexpr int popcount(T x) noexcept {
    return std::popcount((uint64_t)x);
}
constexpr bool kth_bit(auto n, auto k) { return (n >> k) & 1; }
#line 3 "data-structure/segtree-beats.hpp"
// https://rsm9.hatenablog.com/entry/2021/02/01/220408

template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S),
          F (*composition)(F, F), F (*id)()>
//   composition(f,g)(x) = f∘g(x) = f(g(x))
// aclと同じ、maspyさん記事と逆
struct segtree_beats {
    vector<S> v;
    vector<F> vf;
    ll n;
    segtree_beats(ll n)
        : n(n), v(vector<S>(2 * n, e())), vf(vector<F>(2 * n, id())) {};
    segtree_beats(vector<S> v_) : n(v_.size()) {
        vf = vector<F>(2 * n, id());
        v = vector<S>(2 * n, e());
        rep(i, n) { v[i + n] = v_[i]; }
        for(ll i = n - 1; i > 0; i--) {
            v[i] = op(v[i << 1], v[i << 1 | 1]);
        }
    }
    void apply(ll l, ll r, F f) {
        l += n;
        r += n;
        ll l0 = l / (l & -l);
        ll r0 = r / (r & -r) - 1;
        propagate_above(l0);
        propagate_above(r0);
        while(l < r) {
            if(l & 1) {
                apply_at(l, f);
                l++;
            }
            if(r & 1) {
                r--;
                apply_at(r, f);
            }
            l >>= 1;
            r >>= 1;
        }
        recul_above(l0);
        recul_above(r0);
    }
    S get(ll x) {
        x += n;
        ll maxi = bit_length(x) - 1;
        for(ll i = maxi; i > 0; i--) {
            propagate_at(x >> i);
        }
        return v[x];
    }
    void set(ll x, S s) {
        x += n;
        propagate_above(x);
        v[x] = s;
        recul_above(x);
    }
    S prod(ll l, ll r) {
        l += n;
        r += n;
        ll l0 = l / (l & -l);
        ll r0 = r / (r & -r) - 1;
        propagate_above(l0);
        propagate_above(r0);
        S sl = e(), sr = e();
        while(l < r) {
            if(l & 1) {
                sl = op(sl, v[l]);
                l++;
            }
            if(r & 1) {
                r--;
                sr = op(v[r], sr);
            }
            l >>= 1;
            r >>= 1;
        }
        return op(sl, sr);
    }

  private:
    void apply_at(ll x, F f) {
        v[x] = mapping(f, v[x]);
        if(x < n) {
            vf[x] = composition(f, vf[x]);
            // Added for Segment Tree Beats implementation.
            if(v[x].fail) {
                propagate_at(x);
                v[x] = op(v[x << 1], v[x << 1 | 1]);
            }
        }
    }
    void propagate_at(ll x) {
        apply_at(x << 1, vf[x]);
        apply_at(x << 1 | 1, vf[x]);
        vf[x] = id();
    }
    ll bit_length(unsigned long long x) { return 64 - countl_zero(x); }
    void propagate_above(ll x) {
        ll maxi = bit_length(x) - 1;
        for(ll i = maxi; i > 0; i--) {
            propagate_at(x >> i);
        }
        return;
    }
    void recul_above(ll x) {
        while(x > 1) {
            x >>= 1;
            v[x] = op(v[x << 1], v[x << 1 | 1]);
        }
    }
};
#line 3 "data-structure/range-chminmaxaddsum-segtree-beats.hpp"
namespace RangeChMinMaxAddSum {
struct S {
    ll min, min2, max, max2;
    // min2,max2は2番目の最小最大,但し値が1種類のときは全部一致させる
    // 値が2種類のときmin == max2 and max == min2でありmappingの際は注意が必要
    ll sz, szmin, szmax, sum;
    bool fail;
    S(ll x, ll sz = 1)
        : min(x), min2(x), max(x), max2(x), sz(sz), szmin(sz), szmax(sz),
          sum(x * sz), fail(false) {};
    S()
        : min(inf), min2(inf), max(-inf), max2(-inf), sz(0), szmin(0), szmax(0),
          sum(0), fail(false) {}

    bool operator==(const S &other) const {
        return min == other.min and min2 == other.min2 and max == other.max and
               max2 == other.max2 and sz == other.sz and
               szmin == other.szmin and szmax == other.szmax and
               sum == other.sum;
    }
};
S e() {
    S res(0, 0);
    res.min = res.min2 = inf, res.max = res.max2 = -inf;
    res.fail = false;
    return res;
};
void chmin2(ll &m, ll &m2, ll val) {
    if(val < m) {
        m2 = m, m = val;
    } else if(m < val and val < m2) {
        m2 = val;
    }
}
ll second_lowest(S &a, S &b) {
    ll m = inf, m2 = inf;
    chmin2(m, m2, a.min), chmin2(m, m2, a.min2), chmin2(m, m2, b.min),
        chmin2(m, m2, b.min2);
    return (m2 == inf ? m : m2);
}
ll second_heighest(S &a, S &b) {
    ll m = inf, m2 = inf;
    chmin2(m, m2, -a.max), chmin2(m, m2, -a.max2), chmin2(m, m2, -b.max),
        chmin2(m, m2, -b.max2);
    return (m2 == inf ? -m : -m2);
}
S op(S a, S b) {
    if(a.fail)
        return a;
    if(b.fail)
        return b;
    S res;
    res.min = min(a.min, b.min);
    res.max = max(a.max, b.max);
    res.min2 = second_lowest(a, b);
    res.max2 = second_heighest(a, b);
    res.sum = a.sum + b.sum;
    res.sz = a.sz + b.sz;
    res.szmin = a.szmin * (a.min == res.min) + b.szmin * (b.min == res.min);
    res.szmax = a.szmax * (a.max == res.max) + b.szmax * (b.max == res.max);
    return res;
}
struct F {
    // min -> max -> add の順
    ll min, max, add;
    bool operator==(const F &other) const {
        return min == other.min && max == other.max && add == other.add;
    }
};
F id() { return F(inf, -inf, 0); }
S mapping(F f, S s) {
    if(s.fail or f == id())
        return s;
    if(s.sz == 1) {
        ll x = s.min;
        chmin(x, f.min);
        chmax(x, f.max);
        x += f.add;
        return S(x, s.sz);
    }
    // f.min
    if(f.min > s.max2) {
        // f.minを処理できる
        ll dif = min(0LL, f.min - s.max);
        s.sum += dif * s.szmax;
        s.max += dif;
        if(s.min2 + dif == s.max) {
            s.min2 = s.max;
        }
    } else if(f.min <= s.min) {
        // 全部x
        ll x = max(f.max, f.min) + f.add;
        return S(x, s.sz);
    } else {
        s.fail = true;
        return s;
    }
    // f.max
    if(f.max < s.min2) {
        ll dif = max(0LL, f.max - s.min);
        s.sum += dif * s.szmin;
        s.min += dif;
        if(s.max2 + dif == s.min) {
            s.max2 = s.min;
        }
    } else if(f.max >= s.max) {
        return S(f.max + f.add, s.sz);
    } else {
        s.fail = true;
        return s;
    }
    s.min += f.add, s.min2 += f.add, s.max += f.add, s.max2 += f.add;
    s.sum += f.add * s.sz;
    return s;
}
F composition(F f, F g) {
    F res;
    if(f == id())
        return g;
    if(g == id())
        return f;
    res.min = min(g.min, f.min - g.add);
    res.max = max(g.max, f.max - g.add);
    if(g.max >= f.min - g.add) {
        res.min = res.max = f.min - g.add;
    }
    if(f.max - g.add >= g.min) {
        res.min = res.max = f.max - g.add;
    }
    res.add = f.add + g.add;
    return res;
}
using segtree = segtree_beats<S, op, e, F, mapping, composition, id>;
} // namespace RangeChMinMaxAddSum
using RangeChMinMaxAddSum::F;
using RangeChMinMaxAddSum::S;
using RangeChMinMaxAddSum::segtree;
#line 3 "test/library_checker/data_structure/range_chmin_chmax_add_range_sum.test.cpp"
void solve() {
    LL(n, q);
    vector<S> a(n);
    rep(i, n) {
        LL(ai);
        a[i] = S(ai, 1);
    }
    segtree seg(a);
    rep(_, q) {
        LL(flag, l, r);
        if(flag == 0) {
            LL(b);
            seg.apply(l, r, F(b, -inf, 0));
        } else if(flag == 1) {
            LL(b);
            seg.apply(l, r, F(inf, b, 0));
        } else if(flag == 2) {
            LL(b);
            seg.apply(l, r, F(inf, -inf, b));
        } else {
            print(seg.prod(l, r).sum);
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    solve();
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 4 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 722 ms 69 MB
g++ max_random_01 :heavy_check_mark: AC 711 ms 69 MB
g++ max_random_02 :heavy_check_mark: AC 712 ms 69 MB
g++ medium_00 :heavy_check_mark: AC 7 ms 4 MB
g++ medium_01 :heavy_check_mark: AC 6 ms 4 MB
g++ medium_02 :heavy_check_mark: AC 5 ms 4 MB
g++ random2_00 :heavy_check_mark: AC 896 ms 69 MB
g++ random2_01 :heavy_check_mark: AC 890 ms 69 MB
g++ random2_02 :heavy_check_mark: AC 902 ms 69 MB
g++ random3_00 :heavy_check_mark: AC 1071 ms 69 MB
g++ random3_01 :heavy_check_mark: AC 1084 ms 69 MB
g++ random3_02 :heavy_check_mark: AC 1067 ms 69 MB
g++ random_00 :heavy_check_mark: AC 498 ms 45 MB
g++ random_01 :heavy_check_mark: AC 504 ms 53 MB
g++ random_02 :heavy_check_mark: AC 340 ms 21 MB
g++ small_00 :heavy_check_mark: AC 5 ms 4 MB
g++ small_01 :heavy_check_mark: AC 4 ms 4 MB
g++ small_02 :heavy_check_mark: AC 4 ms 4 MB
g++ small_03 :heavy_check_mark: AC 4 ms 4 MB
g++ small_04 :heavy_check_mark: AC 4 ms 4 MB
g++ small_05 :heavy_check_mark: AC 4 ms 4 MB
g++ small_06 :heavy_check_mark: AC 4 ms 4 MB
g++ small_07 :heavy_check_mark: AC 4 ms 4 MB
g++ small_08 :heavy_check_mark: AC 4 ms 4 MB
g++ small_09 :heavy_check_mark: AC 4 ms 4 MB
g++ small_absolute_values_00 :heavy_check_mark: AC 483 ms 45 MB
g++ small_absolute_values_01 :heavy_check_mark: AC 499 ms 53 MB
g++ small_absolute_values_02 :heavy_check_mark: AC 334 ms 21 MB
Back to top page