Problem p00653 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1 << 24;
const int INF = 1 << 28;

int r, c;
int w, h;
int seg[8 * MAX_N];

void init() {
  for (w = 1; w < r; w <<= 1)
    ;
  for (h = 1; h < c; h <<= 1)
    ;
  fill_n(seg, 8 * MAX_N, INF);
}

void update(int x, int y, int v, int k = 0, int ax = 0, int ay = 0, int bx = w,
            int by = h, int nxt = 0) {
  if (x < ax || bx <= x)
    return;
  if (y < ay || by <= y)
    return;

  if (bx - ax == 1 && by - ay == 1) {
    seg[k] = v;
    return;
  }

  if (nxt == 0 && bx - ax == 1)
    nxt = 1;
  else if (nxt == 1 && by - ay == 1)
    nxt = 0;

  if (nxt == 0) {
    int m = (ax + bx) / 2;
    update(x, y, v, k * 2 + 1, ax, ay, m, by, nxt ^ 1);
    update(x, y, v, k * 2 + 2, m, ay, bx, by, nxt ^ 1);
  } else if (nxt == 1) {
    int m = (ay + by) / 2;
    update(x, y, v, k * 2 + 1, ax, ay, bx, m, nxt ^ 1);
    update(x, y, v, k * 2 + 2, ax, m, bx, by, nxt ^ 1);
  }

  seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]);

  // printf("seg[%d] = %d\n", k, seg[k]);
  // printf("%d %d %d %d %d\n", k, ax, ay, bx, by);
}

int getMin(int lx, int ly, int rx, int ry, int k = 0, int ax = 0, int ay = 0,
           int bx = w, int by = h, int nxt = 0) {
  if (rx <= ax || bx <= lx)
    return INF;
  if (ry <= ay || by <= ly)
    return INF;
  if (lx <= ax && bx <= rx && ly <= ay && by <= ry)
    return seg[k];

  // printf("%d %d %d %d %d\n", k, ax, ay, bx, by);

  if (nxt == 0 && bx - ax == 1)
    nxt = 1;
  else if (nxt == 1 && by - ay == 1)
    nxt = 0;

  int vl, vr;
  if (nxt == 0) {
    int m = (ax + bx) / 2;
    vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, m, by, nxt ^ 1);
    vr = getMin(lx, ly, rx, ry, k * 2 + 2, m, ay, bx, by, nxt ^ 1);
  } else if (nxt == 1) {
    int m = (ay + by) / 2;
    vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, bx, m, nxt ^ 1);
    vr = getMin(lx, ly, rx, ry, k * 2 + 2, ax, m, bx, by, nxt ^ 1);
  }

  return min(vl, vr);
}

int main() {
  int q;
  while (scanf("%d %d %d", &r, &c, &q), r | c | q) {
    init();
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int v;
        scanf("%d", &v);
        update(i, j, v);
      }
    }
    while (q--) {
      int r1, c1, r2, c2;
      scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
      printf("%d\n", getMin(r1, c1, ++r2, ++c2));
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1 << 20;
const int INF = INT_MAX;

int r, c;
int w, h;
int seg[8 * MAX_N];

void init() {
  for (w = 1; w < r; w <<= 1)
    ;
  for (h = 1; h < c; h <<= 1)
    ;
  fill_n(seg, 8 * MAX_N, INF);
}

void update(int x, int y, int v, int k = 0, int ax = 0, int ay = 0, int bx = w,
            int by = h, int nxt = 0) {
  if (x < ax || bx <= x)
    return;
  if (y < ay || by <= y)
    return;

  if (bx - ax == 1 && by - ay == 1) {
    seg[k] = v;
    return;
  }

  if (nxt == 0 && bx - ax == 1)
    nxt = 1;
  else if (nxt == 1 && by - ay == 1)
    nxt = 0;

  if (nxt == 0) {
    int m = (ax + bx) / 2;
    update(x, y, v, k * 2 + 1, ax, ay, m, by, nxt ^ 1);
    update(x, y, v, k * 2 + 2, m, ay, bx, by, nxt ^ 1);
  } else if (nxt == 1) {
    int m = (ay + by) / 2;
    update(x, y, v, k * 2 + 1, ax, ay, bx, m, nxt ^ 1);
    update(x, y, v, k * 2 + 2, ax, m, bx, by, nxt ^ 1);
  }

  seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]);

  // printf("seg[%d] = %d\n", k, seg[k]);
  // printf("%d %d %d %d %d\n", k, ax, ay, bx, by);
}

int getMin(int lx, int ly, int rx, int ry, int k = 0, int ax = 0, int ay = 0,
           int bx = w, int by = h, int nxt = 0) {
  if (rx <= ax || bx <= lx)
    return INF;
  if (ry <= ay || by <= ly)
    return INF;
  if (lx <= ax && bx <= rx && ly <= ay && by <= ry)
    return seg[k];

  // printf("%d %d %d %d %d\n", k, ax, ay, bx, by);

  if (nxt == 0 && bx - ax == 1)
    nxt = 1;
  else if (nxt == 1 && by - ay == 1)
    nxt = 0;

  int vl, vr;
  if (nxt == 0) {
    int m = (ax + bx) / 2;
    vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, m, by, nxt ^ 1);
    vr = getMin(lx, ly, rx, ry, k * 2 + 2, m, ay, bx, by, nxt ^ 1);
  } else if (nxt == 1) {
    int m = (ay + by) / 2;
    vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, bx, m, nxt ^ 1);
    vr = getMin(lx, ly, rx, ry, k * 2 + 2, ax, m, bx, by, nxt ^ 1);
  }

  return min(vl, vr);
}

int main() {
  int q;
  while (scanf("%d %d %d", &r, &c, &q), r | c | q) {
    init();
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int v;
        scanf("%d", &v);
        update(i, j, v);
      }
    }
    while (q--) {
      int r1, c1, r2, c2;
      scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
      printf("%d\n", getMin(r1, c1, ++r2, ++c2));
    }
  }
  return 0;
}