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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef pair<int, int> pi;
const int inf = (1ll << 31) - 1;
const double INF = 1e12, EPS = 1e-9;

int h, w, q, H, W, W_;
int y1, x1, y2, x2, *g, sz;

int rec(int Y1, int X1, int Y2, int X2, int y, int x) {
  if (y2 <= Y1 || y1 >= Y2 || x2 <= X1 || x1 >= X2)
    return inf;
  if (y1 <= Y1 && Y2 <= y2 && x1 <= X1 && X2 <= x2)
    return g[y * W_ + x];
  int res = inf;

  // if(y < 0 || x < 0) assert(0);
  if (Y1 + 1 == Y2)
    res = min(res, min(rec(Y1, X1, Y2, (X1 + X2) / 2, y, x * 2 + 1),
                       rec(Y1, (X1 + X2) / 2, Y2, X2, y, x * 2 + 2)));
  else if (X1 + 1 == X2)
    res = min(res, min(rec(Y1, X1, (Y1 + Y2) / 2, X2, y * 2 + 1, x),
                       rec((Y1 + Y2) / 2, X1, Y2, X2, y * 2 + 2, x)));
  else {
    res = min(
        res,
        min(rec(Y1, X1, (Y1 + Y2) / 2, (X1 + X2) / 2, y * 2 + 1, x * 2 + 1),
            rec((Y1 + Y2) / 2, X1, Y2, (X1 + X2) / 2, y * 2 + 2, x * 2 + 1)));
    res = min(
        res,
        min(rec(Y1, (X1 + X2) / 2, (Y1 + Y2) / 2, X2, y * 2 + 1, x * 2 + 2),
            rec((Y1 + Y2) / 2, (X1 + X2) / 2, Y2, X2, y * 2 + 2, x * 2 + 2)));
  }
  return res;
}

int main() {
  g = NULL;
  while (scanf("%d%d%d", &h, &w, &q), w) {
    for (H = 1; H < h; H *= 2)
      ;
    for (W = 1; W < w; W *= 2)
      ;
    W_ = 2 * W - 1;
    if (sz < (2 * H - 1) * (2 * W - 1)) {
      sz = (2 * H - 1) * (2 * W - 1);
      g = (int *)realloc(g, sizeof(int) * sz);
    }
    rep(i, (2 * H - 1) * (2 * W - 1)) g[i] = inf;
    rep(i, h) rep(j, w) scanf("%d", g + (i + H - 1) * W_ + j + W - 1);
    for (int i = 2 * H - 2; i >= 0; i--) {
      for (int j = 2 * W - 2; j >= 0; j--) {
        if (i < H - 1 && j < W - 1)
          g[i * W_ + j] = min(min(g[(i * 2 + 1) * W_ + j * 2 + 1],
                                  g[(i * 2 + 2) * W_ + j * 2 + 2]),
                              min(g[(i * 2 + 2) * W_ + j * 2 + 1],
                                  g[(i * 2 + 1) * W_ + j * 2 + 2]));
        if (i < H - 1)
          g[i * W_ + j] = min(g[i * W_ + j], min(g[(i * 2 + 1) * W_ + j],
                                                 g[(i * 2 + 2) * W_ + j]));
        if (j < W - 1)
          g[i * W_ + j] = min(
              g[i * W_ + j], min(g[i * W_ + j * 2 + 1], g[i * W_ + j * 2 + 2]));
      }
    }
    rep(i, q) {
      scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
      y2++;
      x2++;
      printf("%d\\n", rec(0, 0, H, W, 0, 0));
    }
  }
  if (g)
    free(g);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef pair<int, int> pi;
const int inf = (1ll << 31) - 1;
const double INF = 1e12, EPS = 1e-9;

int h, w, q, H, W, W_;
int y1, x1, y2, x2, g[8000000];

int rec(int Y1, int X1, int Y2, int X2, int y, int x) {
  if (y2 <= Y1 || y1 >= Y2 || x2 <= X1 || x1 >= X2)
    return inf;
  if (y1 <= Y1 && Y2 <= y2 && x1 <= X1 && X2 <= x2)
    return g[y * W_ + x];
  int res = inf;

  // if(y < 0 || x < 0) assert(0);
  if (Y1 + 1 == Y2)
    res = min(res, min(rec(Y1, X1, Y2, (X1 + X2) / 2, y, x * 2 + 1),
                       rec(Y1, (X1 + X2) / 2, Y2, X2, y, x * 2 + 2)));
  else if (X1 + 1 == X2)
    res = min(res, min(rec(Y1, X1, (Y1 + Y2) / 2, X2, y * 2 + 1, x),
                       rec((Y1 + Y2) / 2, X1, Y2, X2, y * 2 + 2, x)));
  else {
    res = min(
        res,
        min(rec(Y1, X1, (Y1 + Y2) / 2, (X1 + X2) / 2, y * 2 + 1, x * 2 + 1),
            rec((Y1 + Y2) / 2, X1, Y2, (X1 + X2) / 2, y * 2 + 2, x * 2 + 1)));
    res = min(
        res,
        min(rec(Y1, (X1 + X2) / 2, (Y1 + Y2) / 2, X2, y * 2 + 1, x * 2 + 2),
            rec((Y1 + Y2) / 2, (X1 + X2) / 2, Y2, X2, y * 2 + 2, x * 2 + 2)));
  }
  return res;
}

int main() {
  while (scanf("%d%d%d", &h, &w, &q), w) {
    for (H = 1; H < h; H *= 2)
      ;
    for (W = 1; W < w; W *= 2)
      ;
    W_ = 2 * W - 1;
    rep(i, (2 * H - 1) * (2 * W - 1)) g[i] = inf;
    rep(i, h) rep(j, w) scanf("%d", g + (i + H - 1) * W_ + j + W - 1);

    for (int i = 2 * H - 2; i >= 0; i--) {
      for (int j = 2 * W - 2; j >= 0; j--) {
        if (i < H - 1 && j < W - 1)
          g[i * W_ + j] = min(min(g[(i * 2 + 1) * W_ + j * 2 + 1],
                                  g[(i * 2 + 2) * W_ + j * 2 + 2]),
                              min(g[(i * 2 + 2) * W_ + j * 2 + 1],
                                  g[(i * 2 + 1) * W_ + j * 2 + 2]));
        if (i < H - 1)
          g[i * W_ + j] = min(g[i * W_ + j], min(g[(i * 2 + 1) * W_ + j],
                                                 g[(i * 2 + 2) * W_ + j]));
        if (j < W - 1)
          g[i * W_ + j] = min(
              g[i * W_ + j], min(g[i * W_ + j * 2 + 1], g[i * W_ + j * 2 + 2]));
      }
    }
    rep(i, q) {
      scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
      y2++;
      x2++;
      printf("%d\\n", rec(0, 0, H, W, 0, 0));
    }
  }
  return 0;
}