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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

enum SegtreeType {
  CUSTOM,
  RAQ_RMQ,
  RAQ_RSQ,
  RUQ_RMQ,
  RUQ_RSQ,
};
template <SegtreeType ty, class T> class LazySegtree {
  int n;
  vector<T> dat, lazy;
  T INIT;
  T E0, E1;
  function<T(T, T, int)> F0;
  function<T(T, T)> F1, F2;

public:
  LazySegtree(int n_, T INIT, T E0, T E1, function<T(T, T, int)> F0,
              function<T(T, T)> F1, function<T(T, T)> F2)
      : INIT(INIT), E0(E0), E1(E1), F0(F0), F1(F1), F2(F2) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat = lazy = vector<T>(2 * n);
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = INIT;
      lazy[i] = E1;
    }
  }
  LazySegtree(int n_) {
    switch (ty) {
    case RAQ_RMQ:
      INIT = 0;
      E0 = numeric_limits<T>::max();
      E1 = 0;
      F0 = [](T a, T b, int s) { return a + b; };
      F1 = [](T a, T b) { return a + b; };
      F2 = [](T a, T b) { return min(a, b); };
      break;
    case RAQ_RSQ:
      INIT = 0;
      E0 = 0;
      E1 = 0;
      F0 = [](T a, T b, int s) { return a + b * s; };
      F1 = [](T a, T b) { return a + b; };
      F2 = [](T a, T b) { return a + b; };
      break;
    }
    n = 1;
    while (n < n_)
      n <<= 1;
    dat = lazy = vector<T>(2 * n);
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = INIT;
      lazy[i] = E1;
    }
  }
  inline void push(int k, int s) {
    if (lazy[k] == E1)
      return;
    dat[k] = F0(dat[k], lazy[k], s);
    if (k < n - 1) {
      lazy[k * 2 + 1] = F1(lazy[k * 2 + 1], lazy[k]);
      lazy[k * 2 + 2] = F1(lazy[k * 2 + 2], lazy[k]);
    }
    lazy[k] = E1;
  }
  inline void upnode(int k) { dat[k] = F2(dat[k * 2 + 1], dat[k * 2 + 2]); }
  inline void update(int a, int b, T x, int k, int l, int r) {
    push(k, r - l);
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      lazy[k] = F1(lazy[k], x);
      push(k, r - l);
      return;
    }
    update(a, b, x, k * 2 + 1, l, (l + r) / 2);
    update(a, b, x, k * 2 + 2, (l + r) / 2, r);
    upnode(k);
  }
  inline T query(int a, int b, int k, int l, int r) {
    push(k, r - l);
    if (b <= l || r <= a)
      return E0;
    if (a <= l && r <= b)
      return dat[k];
    T lb = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T rb = query(a, b, k * 2 + 2, (l + r) / 2, r);
    upnode(k);
    return F2(lb, rb);
  }
  inline void update(int a, int b, T x) { update(a, b, x, 0, 0, n); }
  inline void update(int a, T x) { update(a, a + 1, x); }
  inline T query(int a, int b) { return query(a, b, 0, 0, n); }
  inline T query(int a) { return query(a, a + 1); }
};

int main() {
  int n, q;
  cin >> n >> q;
  LazySegtree<RAQ_RSQ, ll> seg(n);
  rep(i, q) {
    int ty;
    scanf("%d", &ty);
    if (ty == 0) {
      int l, r, x;
      scanf("%d%d%d", &l, &r, &x);
      l--;
      seg.update(l, r, x);
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      l--;
      printf("%lld\\n", seg.query(l, r));
    }
  }
}


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 = [](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;
}