Skip to the content.

:heavy_check_mark: verify/segment-tree/LC_point_add_range_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"

#include "template/template.hpp"
#include "segment-tree/segment-tree.hpp"

ll op(ll x, ll y) { return x + y; }
ll e() { return 0; }

int main() {
  int n, q;
  in(n, q);
  vector<ll> a(n);
  in(a);
  SegmentTree<ll, op, e> seg(a);
  while (q--) {
    int t;
    in(t);
    if (t == 0) {
      int p, x;
      in(p, x);
      seg.apply(p, x);
    } else {
      int l, r;
      in(l, r);
      out(seg.prod(l, r));
    }
  }
}
#line 1 "verify/segment-tree/LC_point_add_range_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"

#line 2 "template/template.hpp"
#include <bits/stdc++.h>
using namespace std;

#line 2 "template/macro.hpp"
#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())
#define YN(b) cout << ((b) ? "YES" : "NO") << "\n";
#define Yn(b) cout << ((b) ? "Yes" : "No") << "\n";
#define yn(b) cout << ((b) ? "yes" : "no") << "\n";
#line 6 "template/template.hpp"

#line 2 "template/util.hpp"
using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;

template <class T, class S = T>
S SUM(const vector<T> &a) {
  return accumulate(ALL(a), S(0));
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T>
int popcnt(T x) {
  return __builtin_popcountll(x);
}
template <class T>
int topbit(T x) {
  return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <class T>
int lowbit(T x) {
  return (x == 0 ? -1 : __builtin_ctzll(x));
}
#line 8 "template/template.hpp"

#line 2 "template/inout.hpp"
struct Fast {
  Fast() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} fast;

template <class T1, class T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << " " << p.second;
}
template <class T>
istream &operator>>(istream &is, vector<T> &a) {
  for (auto &v : a) is >> v;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) {
  for (auto it = a.begin(); it != a.end();) {
    os << *it;
    if (++it != a.end()) os << " ";
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &st) {
  os << "{";
  for (auto it = st.begin(); it != st.end();) {
    os << *it;
    if (++it != st.end()) os << ",";
  }
  os << "}";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  os << "{";
  for (auto it = mp.begin(); it != mp.end();) {
    os << it->first << ":" << it->second;
    if (++it != mp.end()) os << ",";
  }
  os << "}";
  return os;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u)) cout << sep;
  out(u...);
}
#line 10 "template/template.hpp"

#line 2 "template/debug.hpp"
#ifdef LOCAL
#define debug 1
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define debug 0
#define show(...) true
#endif
template <class T>
void _show(int i, T name) {
  cerr << '\n';
}
template <class T1, class T2, class... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
  for (; a[i] != ',' && a[i] != '\0'; i++) cerr << a[i];
  cerr << ":" << b << " ";
  _show(i + 1, a, c...);
}
#line 2 "segment-tree/segment-tree.hpp"

template <class T, T (*op)(T, T), T (*e)()>
struct SegmentTree {
 private:
  int n;
  vector<T> d;
  void update(int p) { d[p] = op(d[2 * p], d[2 * p + 1]); }

 public:
  SegmentTree() : SegmentTree(0) {}
  explicit SegmentTree(int sz) : SegmentTree(vector<T>(sz, e())) {}
  explicit SegmentTree(const vector<T>& v) : n(v.size()) {
    d.assign(2 * n, e());
    for (int i = 0; i < v.size(); i++) d[n + i] = v[i];
    for (int i = n - 1; i > 0; i--) update(i);
  }
  void clear() { fill(d.begin(), d.end(), e()); }

  void set_without_update(int p, T v) { d[n + p] = v; }
  void all_update() {
    for (int i = n - 1; i > 0; i--) update(i);
  }
  T get(int p) {
    assert(0 <= p && p <= n);
    return d[p + n];
  }
  void set(int p, T v) {
    assert(0 <= p && p <= n);
    d[p += n] = v;
    for (p >>= 1; p > 0; p >>= 1) update(p);
  }
  void apply(int p, T v) {
    assert(0 <= p && p <= n);
    p += n;
    d[p] = op(d[p], v);
    for (p >>= 1; p > 0; p >>= 1) update(p);
  }
  T prod(int l, int r) {
    if (l >= r) return e();
    assert(0 <= l && l <= r && r <= n);
    T sl = e(), sr = e();
    l += n, r += n;
    while (l < r) {
      if ((l & 1) != 0) sl = op(sl, d[l++]);
      if ((r & 1) != 0) sr = op(d[--r], sr);
      l >>= 1, r >>= 1;
    }
    return op(sl, sr);
  }
  T all_prod() { return prod(0, n); }

  template <bool (*f)(T)>
  int max_right(int l) const {
    return max_right(l, [](T x) { return f(x); });
  }
  template <class F>
  int max_right(int l, F f) const {
    assert(0 <= l && l <= n);
    assert(f(e()));
    if (l == n) return n;
    int x = n + l, w = 1;
    T s = e();
    for (; l + w <= n; x >>= 1, w <<= 1)
      if (x & 1) {
        if (!f(op(s, d[x]))) break;
        s = op(s, d[x++]);
        l += w;
      }
    while (x <<= 1, w >>= 1) {
      if (l + w <= n && f(op(s, d[x]))) {
        s = op(s, d[l++]);
        l += w;
      }
    }
    return l;
  }

  template <bool (*f)(T)>
  int min_left(int r) const {
    return min_left(r, [](T x) { return f(x); });
  }
  template <class F>
  int min_left(int r, F f) const {
    assert(0 <= r && r <= n);
    assert(f(e()));
    if (r == 0) return 0;
    int x = n + r, w = 1;
    T s = e();
    for (; r - w >= 0; x >>= 1, w <<= 1)
      if (x & 1) {
        if (!f(op(d[x - 1], s))) break;
        s = op(d[--x], s);
        r -= w;
      }
    while (x <<= 1, w >>= 1) {
      if (r - w >= 0 && f(op(d[x - 1], s))) {
        s = op(d[--x], s);
        r -= w;
      }
    }
    return r;
  }
};

/**
 * @brief Segment Tree
 * @docs docs/segment-tree/segment-tree.md
 */
#line 5 "verify/segment-tree/LC_point_add_range_sum.test.cpp"

ll op(ll x, ll y) { return x + y; }
ll e() { return 0; }

int main() {
  int n, q;
  in(n, q);
  vector<ll> a(n);
  in(a);
  SegmentTree<ll, op, e> seg(a);
  while (q--) {
    int t;
    in(t);
    if (t == 0) {
      int p, x;
      in(p, x);
      seg.apply(p, x);
    } else {
      int l, r;
      in(l, r);
      out(seg.prod(l, r));
    }
  }
}
Back to top page