cpp-library

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

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: test/ds/disjoint_sparse_table.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/static_range_sum

#include "ds/disjoint_sparse_table.hpp"

int n, q, a[500000];

int main() {
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d", a+i);
  disjoint_sparse_table<addition<ll>> dst(a, a+n);
  while (q--) {
    int l, r; scanf("%d%d", &l, &r);
    printf("%lld\n", dst.prod(l, r));
  }
}
#line 1 "test/ds/disjoint_sparse_table.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/static_range_sum

#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "algebra.hpp"

#define CONST(val) [=] { return val; }
#define WRAP_FN(func) \
  [](auto&&... args) { return func(forward<decltype(args)>(args)...); }

template <class Unit, class Op>
struct monoid : private Unit, private Op {
  using type = decltype(declval<Unit>()());
  monoid(Unit unit, Op op) : Unit(unit), Op(op) {}
  type unit() const { return Unit::operator()(); }
  type op(type a, type b) const { return Op::operator()(a, b); }
};

template <class Unit, class Op, class Inv>
struct group : monoid<Unit, Op>, private Inv {
  using type = typename monoid<Unit, Op>::type;
  group(Unit unit, Op op, Inv inv) : monoid<Unit, Op>(unit, op), Inv(inv) {}
  type inv(type a) const { return Inv::operator()(a); }
};

template <class T>
struct addition {
  using type = T;
  type unit() const { return 0; }
  type op(type a, type b) const { return a + b; }
  type inv(type a) const { return -a; }
};

template <class T>
struct maximum {
  using type = T;
  type unit() const { return numeric_limits<T>::min(); }
  type op(type a, type b) const { return a > b ? a : b; }
};

template <class T>
struct minimum {
  using type = T;
  type unit() const { return numeric_limits<T>::max(); }
  type op(type a, type b) const { return a > b ? b : a; }
};

template <class T, T nul = -1>
struct assign {
  using type = T;
  type unit() const { return nul; }
  type op(type a, type b) const { return b == nul ? a : b; }
};
#line 3 "bit/clz.hpp"

#pragma GCC target("lzcnt")

template <class T>
int clz(T x) {
    if (!x) return sizeof(T) * 8;
    if constexpr (sizeof(T) <= sizeof(unsigned)) {
        return __builtin_clz((unsigned)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
        return __builtin_clzll((unsigned long long)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
        int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
        return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
    }
}
#line 4 "ds/disjoint_sparse_table.hpp"

template <class M>
class disjoint_sparse_table {
 public:
  using value_type = typename M::type;
  disjoint_sparse_table() = default;
  template <class It>
  disjoint_sparse_table(It a, It last, M m = M()) : m(m) {
    int n = last - a;
    data.emplace_back(a, last);
    int w = 2;
    while (w < n) {
      vector<value_type> row(n, m.unit());
      int j = 0;
      for (; j + w * 2 < n; j += w * 2) {
        value_type prod = m.unit();
        repr2(i, j, j + w) row[i] = prod = m.op(a[i], prod);
        prod = m.unit();
        rep2(i, j + w, j + w * 2) row[i] = prod = m.op(prod, a[i]);
      }
      if (j + w < n) {
        value_type prod = m.unit();
        repr2(i, j, j + w) row[i] = prod = m.op(a[i], prod);
        prod = m.unit();
        rep2(i, j + w, n) row[i] = prod = m.op(prod, a[i]);
      }
      data.push_back(move(row));
      w *= 2;
    }
  }

  value_type prod(int l, int r) {
    if (l == r) return m.unit();
    if (l + 1 == r) return data[0][l];
    int i = 31 - clz(l ^ (r - 1));
    return m.op(data[i][l], data[i][r - 1]);
  }

 private:
  M m;
  vector<vector<value_type>> data;
};
#line 4 "test/ds/disjoint_sparse_table.test.cpp"

int n, q, a[500000];

int main() {
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d", a+i);
  disjoint_sparse_table<addition<ll>> dst(a, a+n);
  while (q--) {
    int l, r; scanf("%d%d", &l, &r);
    printf("%lld\n", dst.prod(l, r));
  }
}

Test cases

Env Name Status Elapsed Memory
g++-12 example_00 :heavy_check_mark: AC 145 ms 8 MB
g++-12 max_random_00 :heavy_check_mark: AC 321 ms 94 MB
g++-12 max_random_01 :heavy_check_mark: AC 327 ms 94 MB
g++-12 max_random_02 :heavy_check_mark: AC 338 ms 94 MB
g++-12 max_random_03 :heavy_check_mark: AC 315 ms 94 MB
g++-12 max_random_04 :heavy_check_mark: AC 332 ms 94 MB
g++-12 random_00 :heavy_check_mark: AC 264 ms 75 MB
g++-12 random_01 :heavy_check_mark: AC 273 ms 87 MB
g++-12 random_02 :heavy_check_mark: AC 138 ms 16 MB
g++-12 random_03 :heavy_check_mark: AC 132 ms 81 MB
g++-12 random_04 :heavy_check_mark: AC 119 ms 56 MB
Back to top page