Problem p02352 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;

int n, q;
bool tag[maxn];
ll S[4 * maxn], lazy[4 * maxn];

void upd(int id, int l, int r, int ql, int qr, int v) {
  if (ql == l && qr == r) {
    tag[id] = 1;
    lazy[id] += v;
    return;
  }
  if (tag[id]) {
    S[id] += lazy[id];
    if (l != r) {
      tag[id << 1] = 1;
      tag[id << 1 | 1] = 1;
      lazy[id << 1] += lazy[id];
      lazy[id << 1 | 1] += lazy[id];
    }
    tag[id] = 0;
    lazy[id] = 0;
  }
  int m = l + r >> 1;
  if (qr <= m)
    upd(id << 1, l, m, ql, qr, v);
  else if (ql > m)
    upd(id << 1 | 1, m + 1, r, ql, qr, v);
  else
    upd(id << 1, l, m, ql, m, v), upd(id << 1 | 1, m + 1, r, m + 1, qr, v);
  ll lm = S[id << 1];
  if (tag[id << 1])
    lm += lazy[id << 1];
  ll rm = S[id << 1 | 1];
  if (tag[id << 1 | 1])
    rm += lazy[id << 1 | 1];
  S[id] = min(lm, rm);
}

ll query(int id, int l, int r, int ql, int qr) {
  if (tag[id]) {
    S[id] += lazy[id];
    if (l != r) {
      tag[id << 1] = 1;
      tag[id << 1 | 1] = 1;
      lazy[id << 1] += lazy[id];
      lazy[id << 1 | 1] += lazy[id];
    }
    tag[id] = 0;
    lazy[id] = 0;
  }
  if (ql == l && qr == r)
    return S[id];
  int m = l + r >> 1;
  if (qr <= m)
    return query(id << 1, l, m, ql, qr);
  else if (ql > m)
    return query(id << 1 | 1, m + 1, r, ql, qr);
  else
    return min(query(id << 1, l, m, ql, m),
               query(id << 1 | 1, m + 1, r, m + 1, qr));
}

int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int f, s, t, x;
    cin >> f >> s >> t;
    if (f)
      cout << query(1, 0, n - 1, s, t) << '\n';
    else {
      cin >> x;
      upd(1, 0, n - 1, s, t, x);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;

int n, q;
bool tag[4 * maxn];
ll S[4 * maxn], lazy[4 * maxn];

void upd(int id, int l, int r, int ql, int qr, int v) {
  if (ql == l && qr == r) {
    tag[id] = 1;
    lazy[id] += v;
    return;
  }
  if (tag[id]) {
    S[id] += lazy[id];
    if (l != r) {
      tag[id << 1] = 1;
      tag[id << 1 | 1] = 1;
      lazy[id << 1] += lazy[id];
      lazy[id << 1 | 1] += lazy[id];
    }
    tag[id] = 0;
    lazy[id] = 0;
  }
  int m = l + r >> 1;
  if (qr <= m)
    upd(id << 1, l, m, ql, qr, v);
  else if (ql > m)
    upd(id << 1 | 1, m + 1, r, ql, qr, v);
  else
    upd(id << 1, l, m, ql, m, v), upd(id << 1 | 1, m + 1, r, m + 1, qr, v);
  ll lm = S[id << 1];
  if (tag[id << 1])
    lm += lazy[id << 1];
  ll rm = S[id << 1 | 1];
  if (tag[id << 1 | 1])
    rm += lazy[id << 1 | 1];
  S[id] = min(lm, rm);
}

ll query(int id, int l, int r, int ql, int qr) {
  if (tag[id]) {
    S[id] += lazy[id];
    if (l != r) {
      tag[id << 1] = 1;
      tag[id << 1 | 1] = 1;
      lazy[id << 1] += lazy[id];
      lazy[id << 1 | 1] += lazy[id];
    }
    tag[id] = 0;
    lazy[id] = 0;
  }
  if (ql == l && qr == r)
    return S[id];
  int m = l + r >> 1;
  if (qr <= m)
    return query(id << 1, l, m, ql, qr);
  else if (ql > m)
    return query(id << 1 | 1, m + 1, r, ql, qr);
  else
    return min(query(id << 1, l, m, ql, m),
               query(id << 1 | 1, m + 1, r, m + 1, qr));
}

int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int f, s, t, x;
    cin >> f >> s >> t;
    if (f)
      cout << query(1, 0, n - 1, s, t) << '\n';
    else {
      cin >> x;
      upd(1, 0, n - 1, s, t, x);
    }
  }
}