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

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

const int maxn = 100005;
typedef long long ll;
struct st {
  ll v, lazy;
} T[4 * maxn];
ll A[maxn];

void lazy(int ini, int fin, int lv) {
  if (ini == fin)
    return;
  T[lv * 2].lazy += T[lv].lazy;
  T[lv * 2 + 1].lazy += T[lv].lazy;
  int mit = (ini + fin) / 2;

  T[lv * 2].v += T[lv].lazy * (mit - ini + 1);
  T[lv * 2 + 1].v += T[lv].lazy * (fin - mit);
  T[lv].lazy = 0;
}

void update(int ini, int fin, int lv, int a, int b, ll v) {
  if (ini > b || fin < a)
    return;
  if (T[lv].lazy)
    lazy(ini, fin, lv);
  if (ini >= a && fin <= b) {
    T[lv].v += (fin - ini + 1) * v;
    T[lv].lazy = v;
    return;
  }
  int mit = (ini + fin) / 2;
  update(ini, mit, lv * 2, a, b, v);
  update(mit + 1, fin, lv * 2 + 1, a, b, v);
  T[lv].v = T[lv * 2].v + T[lv * 2 + 1].v;
}

ll query(int ini, int fin, int lv, int a, int b) {
  if (ini > b || fin < a)
    return 0;
  if (T[lv].lazy)
    lazy(ini, fin, lv);
  if (ini >= a && fin <= b)
    return T[lv].v;
  int mit = (ini + fin) / 2;
  return query(ini, mit, lv * 2, a, b) + query(mit + 1, fin, lv * 2 + 1, a, b);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm

  int N, Q;
  cin >> N >> Q;

  int q, a, b;
  ll x;
  while (Q--) {
    cin >> q;
    if (q == 0) {
      cin >> a >> b >> x;
      update(1, N, 1, a, b, x);
    } else {
      cin >> a >> b;
      cout << query(1, N, 1, a, b) << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define endl '\\n'
#define K 200010
typedef long long ll;

ll n, q;
ll tree[K];
ll lazy[K];

ll query(ll node, ll a, ll b, ll i, ll j) {
  if (a > b || a > j || b < i)
    return 0;
  if (lazy[node] != 0) {
    tree[node] += lazy[node] * (b - a + 1);
    if (a != b) {
      lazy[2 * node] += lazy[node];
      lazy[2 * node + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
  if (a >= i && b <= j)
    return tree[node];

  ll mid = (a + b) / 2;
  ll q1 = query(2 * node, a, mid, i, j);
  ll q2 = query(2 * node + 1, mid + 1, b, i, j);

  return q1 + q2;
}

void update(ll node, ll a, ll b, ll i, ll j, ll v) {
  if (a > b)
    return;
  if (lazy[node] != 0) {
    tree[node] += lazy[node] * (b - a + 1);
    if (a != b) {
      lazy[2 * node] += lazy[node];
      lazy[2 * node + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
  if (a > b || a > j || b < i)
    return;

  if (a >= i && b <= j) {
    tree[node] += v * (b - a + 1);
    if (a != b) {
      lazy[2 * node] += v;
      lazy[2 * node + 1] += v;
    }
    return;
  }

  ll mid = (a + b) / 2;
  update(2 * node, a, mid, i, j, v);
  update(2 * node + 1, mid + 1, b, i, j, v);
  tree[node] = tree[2 * node] + tree[2 * node + 1];
}

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

  cin >> n >> q;
  for (int i = 0; i < 2 * n + 5; i++) {
    lazy[i] = 0;
    tree[i] = 0;
  }
  ll r;
  while (q--) {
    cin >> r;
    int s, t;
    cin >> s >> t;
    if (r) {
      cout << query(1, 0, n - 1, s - 1, t - 1) << endl;
    } else {
      ll x;
      cin >> x;
      update(1, 0, n - 1, s - 1, t - 1, x);
    }
  }

  return 0;
}