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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1 << 20)

struct RMQ {
  int N;
  ll gt;
  ll a[N_SIZE];
  ll seg[N_SIZE * 2];
  void init(int n) {
    N = n;
    gt = 1;
    while (gt < n)
      gt *= 2;
    REP(i, N) a[i] = (1LL << 31) - 1;
    REP(i, N_SIZE * 2) seg[i] = (1LL << 31) - 1;
    REP(i, N) seg[i + gt] = a[i];
  }
  void update(int k, int x) {
    k += gt - 1;
    seg[k] = x;
    while (k) {
      k = (k - 1) / 2; //???????????????
      seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]);
    }
  }
  ll query(int a, int b) { return query(a, b, 0, 0, gt); }
  ll query(int a, int b, int k, int l,
           int r) { //[a,b)????????????????°????,k=0,l=0,r=gt
    if (r <= a || b <= l)
      return (1LL << 31) - 1;

    if (a <= l && r <= b)
      return seg[k];
    else {
      return min(query(a, b, k * 2 + 1, l, (l + r) / 2),
                 query(a, b, k * 2 + 2, (l + r) / 2, r));
    }
  }
  void print() { REP(i, 2 * gt) cout << "!" << i << " " << seg[i] << endl; }
};

RMQ rmq;

int n, q;

int main() {
  cin >> n >> q;
  rmq.init(n);
  REP(i, q) {
    int x, y;
    bool f;
    cin >> f >> x >> y;
    if (f) {
      cout << rmq.query(x, y + 1) << endl;
    } else {
      rmq.update(x, y);
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1 << 20)

struct RMQ {
  int N;
  ll gt;
  ll a[N_SIZE];
  ll seg[N_SIZE * 2];
  void init(int n) {
    N = n;
    REP(i, 30) {
      if ((1 << i) >= N) {
        gt = (1 << i) - 1;
        break;
      }
    }
    REP(i, N) a[i] = (1LL << 31) - 1;
    REP(i, N_SIZE * 2) seg[i] = (1LL << 31) - 1;
    REP(i, N) seg[i + gt] = a[i];
  }
  void update(int k, int x) {
    k += gt;
    seg[k] = x;
    while (k) {
      k = (k - 1) / 2; //???????????????
      seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]);
    }
  }
  ll query(int a, int b, int k, int l,
           int r) { //[a,b)????????????????°????,k=0,l=0,r=gt
    if (a != b && (r <= a || b <= l))
      return (1LL << 31) - 1;

    if (a <= l && r <= b)
      return seg[k];
    else {
      return min(query(a, b, k * 2 + 1, l, (l + r) / 2),
                 query(a, b, k * 2 + 2, (l + r) / 2, r));
    }
  }
  void print() {
    REP(i, 2 * (gt + 1)) cout << "!" << i << " " << seg[i] << endl;
  }
};

RMQ rmq;

int n, q;

int main() {
  cin >> n >> q;
  rmq.init(n);
  REP(i, q) {
    int x, y;
    bool f;
    cin >> f >> x >> y;
    if (f) {
      cout << rmq.query(x, y, 0, 0, n) << endl;
      ;
    } else {
      rmq.update(x, y);
    }
    rmq.print();
  }
}