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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 400005;

struct node {
  int mn = 0x7fffffff, lazy = -1;
} S[maxn];
int n, q;

void update(int id, int ql, int qr, int v, int l, int r) {
  if (ql == l && qr == r) {
    S[id].lazy = v;
    return;
  }
  if (S[id].lazy != -1) {
    S[id].mn = S[id].lazy;
    if (l != r)
      S[id << 1].lazy = S[id << 1 | 1].lazy = S[id].lazy;
    S[id].lazy = -1;
  }
  int m = l + r >> 1;
  if (qr <= m)
    update(id << 1, ql, qr, v, l, m);
  else if (ql > m)
    update(id << 1 | 1, ql, qr, v, m + 1, r);
  else
    update(id << 1, ql, m, v, l, m),
        update(id << 1 | 1, m + 1, qr, v, m + 1, r);
  int lm = S[id << 1].lazy != -1 ? S[id << 1].lazy : S[id << 1].mn;
  int rm = S[id << 1 | 1].lazy != -1 ? S[id << 1 | 1].lazy : S[id << 1 | 1].mn;
  S[id].mn = min(lm, rm);
}

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

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 400005;

struct node {
  int mn = 0x7fffffff, lazy = -1;
} S[maxn];
int n, q;

void update(int id, int ql, int qr, int v, int l, int r) {
  if (ql == l && qr == r) {
    S[id].lazy = v;
    return;
  }
  if (S[id].lazy != -1) {
    S[id].mn = S[id].lazy;
    S[id << 1].lazy = S[id << 1 | 1].lazy = S[id].lazy;
    S[id].lazy = -1;
  }
  int m = l + r >> 1;
  if (qr <= m)
    update(id << 1, ql, qr, v, l, m);
  else if (ql > m)
    update(id << 1 | 1, ql, qr, v, m + 1, r);
  else
    update(id << 1, ql, m, v, l, m),
        update(id << 1 | 1, m + 1, qr, v, m + 1, r);
  int lm = S[id << 1].lazy != -1 ? S[id << 1].lazy : S[id << 1].mn;
  int rm = S[id << 1 | 1].lazy != -1 ? S[id << 1 | 1].lazy : S[id << 1 | 1].mn;
  S[id].mn = min(lm, rm);
}

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

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