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

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
template <class T, class S> inline bool minup(T &m, const S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, const S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const unsigned INF = 0xFFFFFFFF;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-10;

int r, c, q;
int _r, _c;
int g;
unsigned int _seg[11111111];
vector<unsigned int *> seg;
int sx, sy, tx, ty;

void init() {
  _r = 1;
  _c = 1;
  while (_r < r)
    _r *= 2;
  while (_c < c)
    _c *= 2;
  seg.resize(2 * _r - 1);
  rep(i, 2 * _r - 1) seg[i] = _seg + i * (2 * _c - 1);
  rep(i, 2 * _r - 1) rep(j, 2 * _c - 1) seg[i][j] = INF;
}

void update(int a, int b, unsigned int x) {
  a += _r - 1;
  b += _c - 1;
  seg[a][b] = x;
  while (a >= 0) {
    int tmp = b;
    while (b > 0) {
      b = (b - 1) / 2;
      seg[a][b] = min(seg[a][2 * b + 1], seg[a][2 * b + 2]);
    }
    b = tmp;
    if (a == 0)
      break;
    a = (a - 1) / 2;
    seg[a][b] = min(seg[2 * a + 1][b], seg[2 * a + 2][b]);
  }
}

unsigned int _query(int sy, int ty, int kk, int k = 0, int l = 0, int r = _c) {
  if (r <= sy || ty <= l)
    return INF;
  if (sy <= l && r <= ty)
    return seg[kk][k];
  return min(_query(sy, ty, kk, 2 * k + 1, l, (l + r) / 2),
             _query(sy, ty, kk, 2 * k + 2, (l + r) / 2, r));
}

unsigned int query(int sx, int sy, int tx, int ty, int k = 0, int l = 0,
                   int r = _r) {
  if (r <= sx || tx <= l)
    return INF;
  if (sx <= l && r <= tx)
    return _query(sy, ty, k);
  return min(query(sx, sy, tx, ty, 2 * k + 1, l, (l + r) / 2),
             query(sx, sy, tx, ty, 2 * k + 2, (l + r) / 2, r));
}

int main(int argc, char *argv[]) {
  while (cin >> r >> c >> q, r) {
    init();
    rep(i, r) rep(j, c) {
      cin >> g;
      update(i, j, g);
    }
    rep(i, q) {
      cin >> sx >> sy >> tx >> ty;
      cout << query(sx, sy, tx + 1, ty + 1) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
template <class T, class S> inline bool minup(T &m, const S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, const S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const unsigned INF = 0xFFFFFFFF;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-10;

int r, c, q;
int _r, _c;
int g;
unsigned int _seg[11111111];
vector<unsigned int *> seg;
int sx, sy, tx, ty;

void init() {
  _r = 1;
  _c = 1;
  while (_r < r)
    _r *= 2;
  while (_c < c)
    _c *= 2;
  seg.resize(2 * _c - 1);
  rep(i, 2 * _r - 1) seg[i] = _seg + i * (2 * _c - 1);
  rep(i, 2 * _r - 1) rep(j, 2 * _c - 1) seg[i][j] = INF;
}

void update(int a, int b, unsigned int x) {
  a += _r - 1;
  b += _c - 1;
  seg[a][b] = x;
  while (a >= 0) {
    int tmp = b;
    while (b > 0) {
      b = (b - 1) / 2;
      seg[a][b] = min(seg[a][2 * b + 1], seg[a][2 * b + 2]);
    }
    b = tmp;
    if (a == 0)
      break;
    a = (a - 1) / 2;
    seg[a][b] = min(seg[2 * a + 1][b], seg[2 * a + 2][b]);
  }
}

unsigned int _query(int sy, int ty, int kk, int k = 0, int l = 0, int r = _c) {
  if (r <= sy || ty <= l)
    return INF;
  if (sy <= l && r <= ty)
    return seg[kk][k];
  return min(_query(sy, ty, kk, 2 * k + 1, l, (l + r) / 2),
             _query(sy, ty, kk, 2 * k + 2, (l + r) / 2, r));
}

unsigned int query(int sx, int sy, int tx, int ty, int k = 0, int l = 0,
                   int r = _r) {
  if (r <= sx || tx <= l)
    return INF;
  if (sx <= l && r <= tx)
    return _query(sy, ty, k);
  return min(query(sx, sy, tx, ty, 2 * k + 1, l, (l + r) / 2),
             query(sx, sy, tx, ty, 2 * k + 2, (l + r) / 2, r));
}

int main(int argc, char *argv[]) {
  while (cin >> r >> c >> q, r) {
    init();
    rep(i, r) rep(j, c) {
      cin >> g;
      update(i, j, g);
    }
    rep(i, q) {
      cin >> sx >> sy >> tx >> ty;
      cout << query(sx, sy, tx + 1, ty + 1) << endl;
    }
  }

  return 0;
}