lmori's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/LibraryChecker/data-structure/square-root-decomposition/RangeChminChmaxAddRangeSum.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum"
#include "../../../../data-structure/square-root-decomposition/RangeChminChmaxAddRangeSum.hpp"

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, q;
    in(n, q);
    vector<lint> a(n);
    in(a);
    RangeChminChmaxAddRangeSum<lint> rc(a);
    while (q--) {
        int tp, l, r;
        in(tp, l, r);
        if (tp == 0) {
            lint x;
            in(x);
            rc.range_chmin(l, r, x);
        } else if (tp == 1) {
            lint x;
            in(x);
            rc.range_chmax(l, r, x);
        } else if (tp == 2) {
            lint x;
            in(x);
            rc.range_add(l, r, x);
        } else {
            out(rc.range_sum(l, r));
        }
    }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/LibraryChecker/data-structure/square-root-decomposition/RangeChminChmaxAddRangeSum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum"
#line 1 "data-structure/square-root-decomposition/RangeChminChmaxAddRangeSum.hpp"

template <class T>
class RangeChminChmaxAddRangeSum {
   private:
    int n;
    vector<T> a, sum, offset;
    vector<int> divi;
    vector<bool> modulo;

    vector<deque<pair<T, int>>> deq;
    int bsize, bnum;

    inline void build(int bidx) {
        sum[bidx] = 0;
        offset[bidx] = 0;
        map<T, int> cnt;
        int l = bidx * bsize;
        int r = min(n, l + bsize);
        for (int i = l; i < r; i++) {
            cnt[a[i]]++;
            sum[bidx] += a[i];
        }
        deq[bidx].assign(begin(cnt), end(cnt));
    }

    inline void push(int bidx) {
        sum[bidx] += offset[bidx] * (min((bidx + 1) * bsize, n) - bidx * bsize);
        T lower = offset[bidx] + deq[bidx].front().first;
        T upper = offset[bidx] + deq[bidx].back().first;
        for (int i = bidx * bsize; i < min((bidx + 1) * bsize, n); i++) {
            a[i] = min(max(a[i] + offset[bidx], lower), upper);
        }
    }

   public:
    RangeChminChmaxAddRangeSum(const vector<T> &v) {
        a = v;
        n = a.size();
        divi.resize(n + 1);
        modulo.assign(n + 1, false);
        bsize = 250;
        bnum = (n + bsize - 1) / bsize;
        deq.resize(bnum);
        sum.assign(bnum, 0);
        offset.assign(bnum, 0);
        for (int i = 0; i < n + 1; i++) {
            divi[i] = i / bsize;
            modulo[i] = ((i % bsize) != 0);
        }

        for (int i = 0; i < bnum; i++) {
            build(i);
        }
    }

    void range_chmin(int l, int r, T x) {
        // 左端
        if (modulo[l]) {
            push(divi[l]);
            while (l < r and modulo[l]) {
                a[l] = min(a[l], x);
                l++;
            }
            build(divi[l - 1]);
        }

        // 右端
        if (modulo[r]) {
            push(divi[r]);
            while (l < r and modulo[r]) {
                r--;
                a[r] = min(a[r], x);
            }
            build(divi[r]);
        }

        // バケット全体
        while (l < r) {
            int i = divi[l];
            while (offset[i] + deq[i].back().first > x) {
                auto p = deq[i].back();
                deq[i].pop_back();
                if (deq[i].empty() or offset[i] + deq[i].back().first < x) {
                    deq[i].emplace_back(x - offset[i], 0);
                }
                sum[i] -= (p.first - deq[i].back().first) * p.second;
                deq[i].back().second += p.second;
            }
            l += bsize;
        }
    }

    void range_chmax(int l, int r, T x) {
        // 左端
        if (modulo[l]) {
            push(divi[l]);
            while (l < r and modulo[l]) {
                a[l] = max(a[l], x);
                l++;
            }
            build(divi[l - 1]);
        }

        // 右端
        if (modulo[r]) {
            push(divi[r]);
            while (l < r and modulo[r]) {
                r--;
                a[r] = max(a[r], x);
            }
            build(divi[r]);
        }

        // バケット全体
        while (l < r) {
            int i = divi[l];
            while (offset[i] + deq[i].front().first < x) {
                auto p = deq[i].front();
                deq[i].pop_front();
                if (deq[i].empty() or offset[i] + deq[i].front().first > x) {
                    deq[i].emplace_front(x - offset[i], 0);
                }
                sum[i] += (deq[i].front().first - p.first) * p.second;
                deq[i].front().second += p.second;
            }
            l += bsize;
        }
    }

    void range_add(int l, int r, T x) {
        // 左端
        if (modulo[l]) {
            push(divi[l]);
            while (l < r and modulo[l]) {
                a[l] += x;
                l++;
            }
            build(divi[l - 1]);
        }

        // 右端
        if (modulo[r]) {
            push(divi[r]);
            while (l < r and modulo[r]) {
                r--;
                a[r] += x;
            }
            build(divi[r]);
        }

        // バケット全体
        while (l < r) {
            int i = divi[l];
            offset[i] += x;
            l += bsize;
        }
    }

    T range_sum(int l, int r) {
        T res = 0;
        // 左端
        if (modulo[l]) {
            push(divi[l]);
            while (l < r and modulo[l]) {
                res += a[l];
                l++;
            }
            build(divi[l - 1]);
        }

        // 右端
        if (modulo[r]) {
            push(divi[r]);
            while (l < r and modulo[r]) {
                r--;
                res += a[r];
            }
            build(divi[r]);
        }

        // バケット全体
        while (l < r) {
            int i = divi[l];
            res += offset[i] * (min((i + 1) * bsize, n) - i * bsize) + sum[i];
            l += bsize;
        }
        return res;
    }
};
#line 4 "verify/LibraryChecker/data-structure/square-root-decomposition/RangeChminChmaxAddRangeSum.test.cpp"

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, q;
    in(n, q);
    vector<lint> a(n);
    in(a);
    RangeChminChmaxAddRangeSum<lint> rc(a);
    while (q--) {
        int tp, l, r;
        in(tp, l, r);
        if (tp == 0) {
            lint x;
            in(x);
            rc.range_chmin(l, r, x);
        } else if (tp == 1) {
            lint x;
            in(x);
            rc.range_chmax(l, r, x);
        } else if (tp == 2) {
            lint x;
            in(x);
            rc.range_add(l, r, x);
        } else {
            out(rc.range_sum(l, r));
        }
    }
}
Back to top page