Problem p00653 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

struct SegTree {
  vi data;
  int size;
  SegTree(int _size) {
    for (int i = 1;; i <<= 1)
      if (i >= _size) {
        size = i;
        break;
      }
    data.assign(2 * size, INT_MAX);
  }
  void Update(int i, int n) {
    data[size + i] = n;
    for (int j = size + i; j; j >>= 1)
      data[j] = min(data[j], n);
  }
  int Query(int a, int b, int i, int l, int r) {
    if (b <= l || r <= a)
      return INT_MAX;
    if (a <= l && r <= b)
      return data[i];
    int res = INT_MAX, m = (l + r) / 2;
    if (a < m)
      res = min(res, Query(a, b, i * 2 + 0, l, m));
    if (m < b)
      res = min(res, Query(a, b, i * 2 + 1, m, r));
    return res;
  }
  int Query(int a, int b) { return Query(a, b, 1, 0, size); }
};

int main() {
  for (int r, c, q; scanf("%d%d%d", &r, &c, &q), r | c | q;) {
    bool flg = r > c; // グリッドが縦長
    if (r > c)
      swap(r, c);
    vvi grid(r, vi(c));
    rep(i, r) rep(j, c) scanf("%d", &(flg ? grid[j][i] : grid[i][j]));
    vi r1(q), c1(q), r2(q), c2(q);
    rep(i, q) scanf("%d%d%d%d", &r1[i], &c1[i], &r2[i], &c2[i]);
    if (flg) {
      swap(r1, c1);
      swap(r2, c2);
    }

    vector<SegTree> st(r, SegTree(c));
    rep(i, r) rep(j, c) st[i].Update(j, grid[i][j]);

    rep(i, q) {
      int mn = INT_MAX;
      repi(j, r1[i], r2[i] + 1) mn = min(mn, st[j].Query(c1[i], c2[i] + 1));
      printf("%d\n", mn);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

struct SegTree {
  vi data;
  int size;
  SegTree(int _size) {
    for (int i = 1;; i <<= 1)
      if (i >= _size) {
        size = i;
        break;
      }
    data.assign(2 * size, INT_MAX);
  }
  void Update(int i, int n) {
    data[size + i] = n;
    for (int j = size + i; j; j >>= 1)
      data[j] = min(data[j], n);
  }
  int Query(int a, int b, int i, int l, int r) {
    if (b <= l || r <= a)
      return INT_MAX;
    if (a <= l && r <= b)
      return data[i];
    int res = INT_MAX, m = (l + r) / 2;
    if (a < m)
      res = min(res, Query(a, b, i * 2 + 0, l, m));
    if (m < b)
      res = min(res, Query(a, b, i * 2 + 1, m, r));
    return res;
  }
  int Query(int a, int b) { return Query(a, b, 1, 0, size); }
};

int main() {
  for (int r, c, q; scanf("%d%d%d", &r, &c, &q), r | c | q;) {
    bool flg = r > c; // グリッドが縦長
    if (r > c)
      swap(r, c);
    vvi grid(r, vi(c));
    if (flg)
      rep(j, c) rep(i, r) scanf("%d", &grid[i][j]);
    else
      rep(i, r) rep(j, c) scanf("%d", &grid[i][j]);
    vi r1(q), c1(q), r2(q), c2(q);
    rep(i, q) scanf("%d%d%d%d", &r1[i], &c1[i], &r2[i], &c2[i]);
    if (flg) {
      swap(r1, c1);
      swap(r2, c2);
    }

    vector<SegTree> st(r, SegTree(c));
    rep(i, r) rep(j, c) st[i].Update(j, grid[i][j]);

    rep(i, q) {
      int mn = INT_MAX;
      repi(j, r1[i], r2[i] + 1) mn = min(mn, st[j].Query(c1[i], c2[i] + 1));
      printf("%d\n", mn);
    }
  }

  return 0;
}