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/fenwick.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_E

#include "prelude.hpp"
#include "ds/fenwick.hpp"

int n, q, c, s, t, x, i;

int main() {
  scanf("%d%d", &n, &q);
  fenwick_tree<addition<int>> fwk(n+1);
  while (q--) {
    scanf("%d", &c);
    if (c == 0) {
      scanf("%d%d%d", &s, &t, &x);
      fwk.add(s-1, x);
      fwk.sub(t, x);
    } else {
      scanf("%d", &i);
      printf("%d\n", fwk.sum(i));
    }
  }
}
#line 1 "test/ds/fenwick.test.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_E

#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 "bit/ilog2.hpp"

template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }

template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 4 "ds/fenwick.hpp"

template <class M>
class fenwick_tree {
 public:
  using value_type = typename M::type;
  fenwick_tree() = default;
  fenwick_tree(vector<value_type> v, M m = M()) : m(m), data(move(v)) {
    data.insert(data.cbegin(), m.unit());
    for (int i = 1; i < data.size(); i++) {
      if (i + lsb(i) < data.size())
        data[i + lsb(i)] = m.op(data[i + lsb(i)], data[i]);
    }
  }
  template <class Iter>
  fenwick_tree(Iter f, Iter l, M m = M())
      : fenwick_tree(vector<value_type>(f, l), m) {}
  fenwick_tree(int n, M m = M()) : m(m), data(n + 1, m.unit()) {}
  int size() const { return data.size() - 1; }
  void clear() { fill(data.begin(), data.end(), m.unit()); }
  void add(int i, value_type v) {
    for (i++; i < data.size(); i += lsb(i)) data[i] = m.op(data[i], v);
  }
  void sub(int i, value_type v) { add(i, m.inv(v)); }
  void assign(int i, value_type v) { add(i, m.op(v, m.inv(sum(i, i + 1)))); }
  value_type sum(int r) const {
    value_type res = m.unit();
    for (; r; r -= lsb(r)) res = m.op(res, data[r]);
    return res;
  }
  value_type sum(int l, int r) const { return m.op(m.inv(sum(l)), sum(r)); }
  template <class F>
  int partition_point(F pred = F()) const {
    int i = 0;
    value_type s = m.unit();
    if (!pred(s)) return i;
    for (int w = bit_floor(data.size()); w; w >>= 1) {
      if (i + w < data.size()) {
        value_type s2 = m.op(s, data[i + w]);
        if (pred(s2)) i += w, s = s2;
      }
    }
    return i + 1;
  }
  // min i s.t. sum(i) >= x
  template <class Comp = less<value_type>>
  int lower_bound(value_type x, Comp comp = Comp()) const {
    return partition_point([&](value_type s) { return comp(s, x); });
  }

 private:
  M m;
  vector<value_type> data;
  static int lsb(int a) { return a & -a; }
};
#line 5 "test/ds/fenwick.test.cpp"

int n, q, c, s, t, x, i;

int main() {
  scanf("%d%d", &n, &q);
  fenwick_tree<addition<int>> fwk(n+1);
  while (q--) {
    scanf("%d", &c);
    if (c == 0) {
      scanf("%d%d%d", &s, &t, &x);
      fwk.add(s-1, x);
      fwk.sub(t, x);
    } else {
      scanf("%d", &i);
      printf("%d\n", fwk.sum(i));
    }
  }
}

Test cases

Env Name Status Elapsed Memory
g++-12 00_sample_00.in :heavy_check_mark: AC 247 ms 8 MB
g++-12 00_sample_01.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 01_rand_00.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 01_rand_01.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 01_rand_02.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 01_rand_03.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 01_rand_04.in :heavy_check_mark: AC 13 ms 8 MB
g++-12 01_rand_05.in :heavy_check_mark: AC 15 ms 8 MB
g++-12 02_corner_00.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 02_corner_01.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 03_large_00.in :heavy_check_mark: AC 15 ms 8 MB
g++-12 03_large_01.in :heavy_check_mark: AC 15 ms 8 MB
g++-12 03_large_02.in :heavy_check_mark: AC 16 ms 8 MB
g++-12 03_large_03.in :heavy_check_mark: AC 16 ms 8 MB
g++-12 04_maximum_00.in :heavy_check_mark: AC 49 ms 8 MB
g++-12 04_maximum_01.in :heavy_check_mark: AC 49 ms 8 MB
g++-12 04_maximum_02.in :heavy_check_mark: AC 50 ms 8 MB
g++-12 04_maximum_03.in :heavy_check_mark: AC 48 ms 8 MB
g++-12 05_critical_00.in :heavy_check_mark: AC 49 ms 8 MB
g++-12 05_critical_01.in :heavy_check_mark: AC 44 ms 8 MB
g++-12 05_critical_02.in :heavy_check_mark: AC 51 ms 8 MB
g++-12 05_critical_03.in :heavy_check_mark: AC 49 ms 8 MB
Back to top page