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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>

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

using namespace std;

int rc[1000 * 1000];
int w, h, q;
int mm;
bool sw;

int buff[2 * 1024 * 1024 + 10];

void gen() {
  const int n = w * h;
  int m = 1;
  while (m < n)
    m *= 2;
  mm = m;

  REP(i, m + m) buff[i] = INT_MAX;
  REP(i, n) buff[i + m - 1] = rc[i];

  while (m - 1 >= 0) {
    m /= 2;
    /*
    for(int i = 1; i <= mm; i *= 2){
      for(int j = i - 1; j < i - 1 + i; j++){
        printf("%d ", buff[j]);
      }
      puts("");
    }
    */

    for (int i = m - 1; i < m - 1 + m; i++) {
      buff[i] = min(buff[i * 2 + 1], buff[i * 2 + 2]);
    }
  }
}

int iter(int l, int r, int pos, int width) {
  // printf("iter: %d %d %d %d\\n", l, r, pos, width);
  int w2 = width / 2;
  if (l + width == r)
    return buff[pos];
  if (w2 <= l)
    return iter(l - w2, r - w2, pos * 2 + 2, w2);
  if (w2 >= r)
    return iter(l, r, pos * 2 + 1, w2);
  return min(iter(l, w2, pos * 2 + 1, w2), iter(0, r - w2, pos * 2 + 2, w2));
}

int query(int l, int r) { return iter(l, r, 0, mm); }

int main() {
  while (scanf("%d%d%d", &h, &w, &q), w + h + q) {
    sw = h > w;

    REP(i, h) REP(j, w) {
      if (!sw) {
        scanf("%d", rc + i * w + j);
      } else {
        scanf("%d", rc + j * h + i);
      }
    }

    if (sw)
      swap(h, w);

    gen();

    REP(qq, q) {
      int h1, h2, w1, w2;
      int ans = INT_MAX;
      scanf("%d%d%d%d", &h1, &w1, &h2, &w2);

      if (sw) {
        swap(h1, w1);
        swap(h2, w2);
      }

      for (int hh = h1; hh < h2 + 1; hh++) {
        int p1 = hh * w + w1;
        int p2 = hh * w + w2;

        // REP(i, h*w) printf("%d ", rc[i]); puts("");
        // printf("query(%d, %d) = %d\\n", p1, p2 + 1, query(p1, p2 + 1));

        ans = min(ans, query(p1, p2 + 1));
      }

      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>

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

using namespace std;

int rc[1000 * 1000];
int w, h, q;
int mm;
bool sw;

int buff[1024 * 1024];

void gen() {
  const int n = w * h;
  int m = 1;
  while (m < n)
    m *= 2;
  mm = m;

  REP(i, m + m) buff[i] = INT_MAX;
  REP(i, n) buff[i + m - 1] = rc[i];

  while (m - 1 >= 0) {
    m /= 2;
    /*
    for(int i = 1; i <= mm; i *= 2){
      for(int j = i - 1; j < i - 1 + i; j++){
        printf("%d ", buff[j]);
      }
      puts("");
    }
    */

    for (int i = m - 1; i < m - 1 + m; i++) {
      buff[i] = min(buff[i * 2 + 1], buff[i * 2 + 2]);
    }
  }
}

int iter(int l, int r, int pos, int width) {
  // printf("iter: %d %d %d %d\\n", l, r, pos, width);
  int w2 = width / 2;
  if (l + width == r)
    return buff[pos];
  if (w2 <= l)
    return iter(l - w2, r - w2, pos * 2 + 2, w2);
  if (w2 >= r)
    return iter(l, r, pos * 2 + 1, w2);
  return min(iter(l, w2, pos * 2 + 1, w2), iter(0, r - w2, pos * 2 + 2, w2));
}

int query(int l, int r) { return iter(l, r, 0, mm); }

int main() {
  while (scanf("%d%d%d", &h, &w, &q), w + h + q) {
    sw = h > w;

    REP(i, h) REP(j, w) {
      if (!sw) {
        scanf("%d", rc + i * w + j);
      } else {
        scanf("%d", rc + j * h + i);
      }
    }

    if (sw)
      swap(h, w);

    gen();

    REP(qq, q) {
      int h1, h2, w1, w2;
      int ans = INT_MAX;
      scanf("%d%d%d%d", &h1, &w1, &h2, &w2);

      if (sw) {
        swap(h1, w1);
        swap(h2, w2);
      }

      for (int hh = h1; hh < h2 + 1; hh++) {
        int p1 = hh * w + w1;
        int p2 = hh * w + w2;

        // REP(i, h*w) printf("%d ", rc[i]); puts("");
        // printf("query(%d, %d) = %d\\n", p1, p2 + 1, query(p1, p2 + 1));

        ans = min(ans, query(p1, p2 + 1));
      }

      cout << ans << endl;
    }
  }
  return 0;
}