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

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

class RMQ {
  int n;
  vector<int> dat;

public:
  RMQ(int _n) { init(_n); }
  RMQ() {}

  void init(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    dat.resize(2 * n - 1, INT_MAX);
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  int size() { return n; }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INT_MAX;
    if (a <= l && r <= b)
      return dat[k];
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }

  int query(int l, int r) { return query(l, r, 0, 0, n); }
};
// RMQ R;

int main() {
  int N, Q;
  cin >> N >> Q;
  // R.init(N);
  RMQ R(N);
  while (Q--) {
    int C, X, Y;
    cin >> C >> X >> Y;

    if (C == 0) {
      R.update(X, Y);
    } else {
      cout << R.query(X, Y + 1) << endl;
    }
  }
}

Error: WA

Fixed submission:

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

class RMQ {
  int n;
  vector<int> dat;

public:
  RMQ(int _n) { init(_n); }
  RMQ() {}

  void init(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    dat.resize(2 * n - 1, INT_MAX);
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  int size() { return n; }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INT_MAX;
    if (a <= l && r <= b)
      return dat[k];
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }

  int query(int l, int r) { return query(l, r, 0, 0, n); }
};
// RMQ R;

int main() {
  int N, Q;
  cin >> N >> Q;
  // R.init(N);
  RMQ R(N);
  while (Q--) {
    int C, X, Y;
    cin >> C >> X >> Y;

    if (C == 0) {
      R.update(X, Y);
    } else {
      cerr << R.query(X, Y + 1) << endl;
    }
  }
}