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

#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <nmmintrin.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> struct Lazy_BIT {
  vector<T> x, lazy;
#define SZ(a) (a & -a)
#define LEFT(a) (a - SZ(a))
#define UP(a) (a + SZ(a))

  Lazy_BIT(int n) : x(n + 1), lazy(n + 1) {}
  T sum(int r) {
    T ret = 0;
    for (int k = r; 0 < k; k = LEFT(k)) {
      ret += lazy[k] * SZ(k) + x[k];
      for (int cp = UP(k); LEFT(k) == LEFT(cp) && cp < x.size(); cp = UP(cp)) {
        ret += lazy[cp] * (r - LEFT(cp));
      }
    }
    return ret;
  }
  void add(int r, int v) {
    for (int k = r; 0 < k; k = LEFT(k)) {
      lazy[k] += v;
      for (int cp = UP(k); LEFT(k) == LEFT(cp) && cp < x.size(); cp = UP(cp)) {
        x[cp] += v * (r - LEFT(cp));
      }
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  Lazy_BIT<ll> fen(n);

  REP(i, q) {
    int m;
    scanf("%d", &m);
    if (m) {
      int s, t;
      scanf("%d %d", &s, &t);

      printf("%lld\\n", fen.sum(t) - fen.sum(s - 1));
    } else {
      int s, t, x;
      scanf("%d %d %d", &s, &t, &x);

      fen.add(t, x);
      fen.add(s - 1, -x);
    }
  }

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <nmmintrin.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> struct Lazy_BIT {
  vector<T> x, lazy;
#define SZ(a) (a & -a)
#define LEFT(a) (a - SZ(a))
#define UP(a) (a + SZ(a))

  Lazy_BIT(int n) : x(n + 1), lazy(n + 1) {}
  T sum(int r) {
    T ret = 0;
    for (int k = r; 0 < k; k = LEFT(k)) {
      ret += lazy[k] * (r - LEFT(k)) + x[k];
      for (int cp = UP(k); LEFT(k) == LEFT(cp) && k < x.size(); cp = UP(cp)) {
        ret += lazy[cp] * (r - LEFT(cp));
      }
    }
    return ret;
  }
  void add(int r, int v) {
    for (int k = r; 0 < k; k = LEFT(k)) {
      lazy[k] += v;
      for (int cp = UP(k); LEFT(k) == LEFT(cp) && k < x.size(); cp = UP(cp)) {
        x[cp] += v * (r - LEFT(cp));
      }
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  Lazy_BIT<ll> fen(n);

  REP(i, q) {
    int m;
    scanf("%d", &m);
    if (m) {
      int s, t;
      scanf("%d %d", &s, &t);

      printf("%lld\\n", fen.sum(t) - fen.sum(s - 1));
    } else {
      int s, t, x;
      scanf("%d %d %d", &s, &t, &x);

      fen.add(t, x);
      fen.add(s - 1, -x);
    }
  }

  return 0;
}