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

// 40
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define INF ((1 << 31) ^ ~0)

vector<vector<int>> d;

int que(const vector<int> &d, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return INF;
  } else if (a <= l && r <= b) {
    return d[k];
  } else {
    return min(que(d, a, b, k * 2 + 1, l, (l + r) / 2),
               que(d, a, b, k * 2 + 2, (l + r) / 2, r));
  }
}

int main() {
  for (int r, c, q; cin >> r >> c >> q, r | c | q;) {
    vector<vector<int>> g;
    bool w = r < c;
    if (w) {
      g = vector<vector<int>>(r, vector<int>(c));
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
          cin >> g[i][j];
        }
      }
    } else {
      g = vector<vector<int>>(c, vector<int>(r));
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
          cin >> g[j][i];
        }
      }
      swap(r, c);
    }
    int cc;
    for (cc = 1; cc < c; cc *= 2)
      ;
    d = vector<vector<int>>(r, vector<int>(cc * 2 - 1, INF));
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int k = j + cc - 1;
        d[i][k] = g[i][j];
        while (k > 0) {
          k = (k - 1) / 2;
          d[i][k] = min(d[i][k * 2 + 1], d[i][k * 2 + 2]);
        }
      }
    }
    while (q--) {
      int r1, c1, r2, c2;
      if (w) {
        cin >> r1 >> c1 >> r2 >> c2;
      } else {
        cin >> c1 >> r1 >> c2 >> r2;
      }
      int m = INF;
      for (int i = r1; i <= r2; i++) {
        m = min(m, que(d[i], c1, c2 + 1, 0, 0, cc));
      }
      cout << m << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

// 40
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define INF ((1 << 31) ^ ~0)
#define N 1024

int d[1000][2 * N - 1];

int que(int *d, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return INF;
  } else if (a <= l && r <= b) {
    return d[k];
  } else {
    return min(que(d, a, b, k * 2 + 1, l, (l + r) / 2),
               que(d, a, b, k * 2 + 2, (l + r) / 2, r));
  }
}

int main() {
  for (int r, c, q; cin >> r >> c >> q, r | c | q;) {
    vector<vector<int>> g;
    bool w = r < c;
    if (w) {
      g = vector<vector<int>>(r, vector<int>(c));
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
          cin >> g[i][j];
        }
      }
    } else {
      g = vector<vector<int>>(c, vector<int>(r));
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
          cin >> g[j][i];
        }
      }
      swap(r, c);
    }
    int cc;
    for (cc = 1; cc < c; cc *= 2)
      ;
    fill(d[0], d[r + 1], INF);
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int k = j + cc - 1;
        d[i][k] = g[i][j];
        while (k > 0) {
          k = (k - 1) / 2;
          d[i][k] = min(d[i][k * 2 + 1], d[i][k * 2 + 2]);
        }
      }
    }
    while (q--) {
      int r1, c1, r2, c2;
      if (w) {
        cin >> r1 >> c1 >> r2 >> c2;
      } else {
        cin >> c1 >> r1 >> c2 >> r2;
      }
      int m = INF;
      for (int i = r1; i <= r2; i++) {
        m = min(m, que(d[i], c1, c2 + 1, 0, 0, cc));
      }
      cout << m << endl;
    }
  }
  return 0;
}