Problem p02351 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <math.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
// typedef pair<int,int> P;
// typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

template <typename T, typename L = T> struct LazySegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, L)>;
  using H = function<L(L, L)>;
  using P = function<L(L, int)>;

  int sz;
  vector<T> node;
  vector<L> lazy;
  const F f;
  const G g;
  const H h;
  const P p;
  const T NODE_INIT;
  const L LAZY_INIT;

  LazySegmentTree(int n, const F f, const G g, const H h, const P p,
                  const T &NODE_INIT, const L &LAZY_INIT)
      : f(f), g(g), h(h), p(p), NODE_INIT(NODE_INIT), LAZY_INIT(LAZY_INIT) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    node.assign(2 * sz - 1, NODE_INIT);
    lazy.assign(2 * sz - 1, LAZY_INIT);
  }

  void set(int k, const T &x) { node[k + sz - 1] = x; }

  void build() {
    for (int i = sz - 2; i >= 0; i--) {
      node[i] = f(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }

  void eval(int k, int len) {
    if (lazy[k] != LAZY_INIT) {
      if (k < sz - 1) {
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
        lazy[2 * k + 2] = h(lazy[2 * k + 2], lazy[k]);
      }
      node[k] = g(node[k], p(lazy[k], len));
      lazy[k] = LAZY_INIT;
    }
  }

  T update(int a, int b, const T &x, int k, int l, int r) {
    eval(k, r - l);
    if (r <= a || b <= l)
      return node[k];
    else if (a <= l && r <= b) {
      lazy[k] = h(lazy[k], x);
      eval(k, r - l);
      return node[k];
    } else {
      eval(k, r - l);
      node[k] = f(update(a, b, x, 2 * k + 1, l, (l + r) / 2),
                  update(a, b, x, 2 * k + 2, (l + r) / 2, r));
      return node[k];
    }
  }

  T update(int a, int b, const T &x) { return update(a, b, x, 0, 0, sz); }

  T query(int a, int b, int k, int l, int r) {
    eval(k, r - l);
    if (r <= a || b <= l)
      return NODE_INIT;
    else if (a <= l && r <= b)
      return node[k];
    else
      return f(query(a, b, 2 * k + 1, l, (l + r) / 2),
               query(a, b, 2 * k + 2, (l + r) / 2, r));
  }

  T query(int a, int b) { return query(a, b, 0, 0, sz); }

  void show() {
    for (int i = 0; i < sz * 2 - 1; i++)
      cout << node[i] << ' ';
    cout << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  auto add = [](ll a, ll b) { return a + b; };
  auto multiply = [](ll a, ll b) { return a * b; };

  int n, q;
  cin >> n >> q;
  LazySegmentTree<ll> tree(n, add, add, add, multiply, 0, 0);
  while (q--) {
    int type, s, t;
    ll x;
    cin >> type >> s >> t;
    s--, t--;
    if (type == 0) {
      cin >> x;
      tree.update(s, t + 1, x);
      // tree.show();
    } else {
      cout << tree.query(s, t + 1) << endl;
      // tree.show();
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <math.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
// typedef pair<int,int> P;
// typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

template <typename T, typename L = T> struct LazySegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, L)>;
  using H = function<L(L, L)>;
  using P = function<L(L, int)>;

  int sz;
  vector<T> node;
  vector<L> lazy;
  const F f;
  const G g;
  const H h;
  const P p;
  const T NODE_INIT;
  const L LAZY_INIT;

  LazySegmentTree(int n, const F f, const G g, const H h, const P p,
                  const T &NODE_INIT, const L &LAZY_INIT)
      : f(f), g(g), h(h), p(p), NODE_INIT(NODE_INIT), LAZY_INIT(LAZY_INIT) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    node.assign(2 * sz - 1, NODE_INIT);
    lazy.assign(2 * sz - 1, LAZY_INIT);
  }

  void set(int k, const T &x) { node[k + sz - 1] = x; }

  void build() {
    for (int i = sz - 2; i >= 0; i--) {
      node[i] = f(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }

  void eval(int k, int len) {
    if (lazy[k] != LAZY_INIT) {
      if (k < sz) {
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
        lazy[2 * k + 2] = h(lazy[2 * k + 2], lazy[k]);
      }
      node[k] = g(node[k], p(lazy[k], len));
      lazy[k] = LAZY_INIT;
    }
  }

  T update(int a, int b, const T &x, int k, int l, int r) {
    eval(k, r - l);
    if (r <= a || b <= l)
      return node[k];
    else if (a <= l && r <= b) {
      lazy[k] = h(lazy[k], x);
      eval(k, r - l);
      return node[k];
    } else {
      eval(k, r - l);
      node[k] = f(update(a, b, x, 2 * k + 1, l, (l + r) / 2),
                  update(a, b, x, 2 * k + 2, (l + r) / 2, r));
      return node[k];
    }
  }

  T update(int a, int b, const T &x) { return update(a, b, x, 0, 0, sz); }

  T query(int a, int b, int k, int l, int r) {
    eval(k, r - l);
    if (r <= a || b <= l)
      return NODE_INIT;
    else if (a <= l && r <= b)
      return node[k];
    else
      return f(query(a, b, 2 * k + 1, l, (l + r) / 2),
               query(a, b, 2 * k + 2, (l + r) / 2, r));
  }

  T query(int a, int b) { return query(a, b, 0, 0, sz); }

  void show() {
    for (int i = 0; i < sz * 2 - 1; i++)
      cout << node[i] << ' ';
    cout << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  auto add = [](int a, int b) { return a + b; };
  auto multiply = [](int a, int b) { return a * b; };

  int n, q;
  cin >> n >> q;
  LazySegmentTree<ll> tree(n, add, add, add, multiply, 0, 0);
  while (q--) {
    int type, s, t;
    ll x;
    cin >> type >> s >> t;
    s--, t--;
    if (type == 0) {
      cin >> x;
      tree.update(s, t + 1, x);
      // tree.show();
    } else {
      cout << tree.query(s, t + 1) << endl;
      // tree.show();
    }
  }

  return 0;
}