Problem p00653 (C++)

Buggy submission (status: Runtime Error):
// AOJ 1068
#include <algorithm>
#include <cstdio>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)
#define repi(i, a, b) for (int i = (a); i != (b); ++i)
#define all(a) (a).begin(), (a).end()

const int INF = (1 << 31) - 1;

int r, c, q;
std::vector<std::vector<int>> segR, segC;

void init(std::vector<std::vector<int>> &seg, int n) {
  int sz = 1;
  while (sz < c)
    sz <<= 1;

  rep(i, seg.size()) {
    seg[i].resize(2 * sz);
    std::fill(all(seg[i]), INF);
  }

  return;
}

void update(std::vector<int> &seg, int k, int x) {
  k += (seg.size() >> 1) - 1;
  seg[k] = x;
  while (k) {
    k = (k - 1) >> 1;
    seg[k] = std::min(seg[(k << 1) + 1], seg[(k << 1) + 2]);
  }

  return;
}

int query(const std::vector<int> &seg, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  else if (a <= l && r <= b)
    return seg[k];

  return std::min(query(seg, a, b, (k << 1) + 1, l, (l + r) >> 1),
                  query(seg, a, b, (k << 1) + 2, (l + r) >> 1, r));
}

int main() {
  while (scanf("%d%d%d", &r, &c, &q), r | c | q) {
    segR.resize(r);
    segC.resize(c);
    init(segR, c);
    init(segC, r);

    rep(i, r) {
      rep(j, c) {
        int grid;
        scanf("%d", &grid);

        update(segR[i], j, grid);
        update(segC[j], i, grid);
      }
    }

    rep(i, q) {
      int r1, c1, r2, c2;
      scanf("%d%d%d%d", &r1, &c1, &r2, &c2);

      int ans = INF;

      if (r2 - r1 <= c2 - c1)
        repi(j, r1, r2 + 1) ans = std::min(
            ans, query(segR[j], c1, c2 + 1, 0, 0, segR[j].size() >> 1));
      else
        repi(j, c1, c2 + 1) ans = std::min(
            ans, query(segC[j], r1, r2 + 1, 0, 0, segC[j].size() >> 1));

      printf("%d\n", ans);
    }
  }

  return 0;
}

Fixed submission:
// AOJ 1068
#include <algorithm>
#include <cstdio>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)
#define repi(i, a, b) for (int i = (a); i != (b); ++i)
#define all(a) (a).begin(), (a).end()

const int INF = (1 << 31) - 1;

int r, c, q;
std::vector<std::vector<int>> segR, segC;

void init(std::vector<std::vector<int>> &seg, int n) {
  int sz = 1;
  while (sz < n)
    sz <<= 1;

  rep(i, seg.size()) {
    seg[i].resize(2 * sz);
    std::fill(all(seg[i]), INF);
  }

  return;
}

void update(std::vector<int> &seg, int k, int x) {
  k += (seg.size() >> 1) - 1;
  seg[k] = x;
  while (k) {
    k = (k - 1) >> 1;
    seg[k] = std::min(seg[(k << 1) + 1], seg[(k << 1) + 2]);
  }

  return;
}

int query(const std::vector<int> &seg, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  else if (a <= l && r <= b)
    return seg[k];

  return std::min(query(seg, a, b, (k << 1) + 1, l, (l + r) >> 1),
                  query(seg, a, b, (k << 1) + 2, (l + r) >> 1, r));
}

int main() {
  while (scanf("%d%d%d", &r, &c, &q), r | c | q) {
    segR.resize(r);
    segC.resize(c);
    init(segR, c);
    init(segC, r);

    rep(i, r) {
      rep(j, c) {
        int grid;
        scanf("%d", &grid);

        update(segR[i], j, grid);
        update(segC[j], i, grid);
      }
    }

    rep(i, q) {
      int r1, c1, r2, c2;
      scanf("%d%d%d%d", &r1, &c1, &r2, &c2);

      int ans = INF;

      if (r2 - r1 <= c2 - c1)
        repi(j, r1, r2 + 1) ans = std::min(
            ans, query(segR[j], c1, c2 + 1, 0, 0, segR[j].size() >> 1));
      else
        repi(j, c1, c2 + 1) ans = std::min(
            ans, query(segC[j], r1, r2 + 1, 0, 0, segC[j].size() >> 1));

      printf("%d\n", ans);
    }
  }

  return 0;
}