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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int vlog[1000010];
int R, C, Q;
vector<vector<vector<int>>> rmq;
int grid[1000010];

inline void setvalue(int r, int c, int v) { grid[r * C + c] = v; }
inline int getvalue(int r, int c) { return grid[r * C + c]; }

void rmq_build() {
  rmq.clear();
  for (int i = 0; i < R; i++) {
    rmq.push_back(vector<vector<int>>());
    for (int k = 0; (1 << k) - 1 < C; k++) {
      rmq[i].push_back(vector<int>());
      for (int j = 0; j + (1 << k) - 1 < C; j++) {
        rmq[i][k].push_back(
            k == 0 ? getvalue(i, j)
                   : min(rmq[i][k - 1][j], rmq[i][k - 1][j + (1 << k - 1)]));
      }
    }
  }
}

inline int rmq_query(int r, int c1, int c2) {
  int k = vlog[c2 - c1 + 1];
  return min(rmq[r][k][c1], rmq[r][k][c2 - (1 << k) + 1]);
}

int main() {
  //    freopen("SchoolOfKillifish.in","r",stdin);
  int r1, c1, r2, c2, v;
  for (int i = 1; i <= 1000000; i++)
    vlog[i] = (int)log2((double)i);
  while (~scanf("%d%d%d", &R, &C, &Q)) {
    bool rot = (R > C);
    int rr = R, cc = C;
    if (rot)
      swap(R, C);
    for (int i = 0; i < rr; i++) {
      for (int j = 0; j < cc; j++) {
        scanf("%d", &v);
        if (rot)
          setvalue(j, i, v);
        else
          setvalue(i, j, v);
      }
    }
    rmq_build();
    for (int i = 0; i < Q; i++) {
      scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
      if (rot)
        swap(r1, c1), swap(r2, c2);
      int v = 0x7fffffff;
      for (int j = r1; j <= r2; j++)
        v = min(v, rmq_query(j, c1, c2));
      printf("%d\\n", v);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <vector>
#define la (rt * 4)
#define lb (rt * 4 + 1)
#define lc (rt * 4 + 2)
#define ld (rt * 4 + 3)
#define MIN(A, B) ((A) > (B) ? (B) : (A))
using namespace std;

int r, c, q;

struct seg {
  int x1, y1, x2, y2;
  int v;
  int midx() { return (x1 + x2) >> 1; }
  int midy() { return (y1 + y2) >> 1; }
} tr[4000010];
int map[1000010];
void build(int rt, int x1, int y1, int x2, int y2) {
  tr[rt].x1 = x1;
  tr[rt].y1 = y1;
  tr[rt].x2 = x2;
  tr[rt].y2 = y2;
  if (x1 == x2 && y1 == y2) {
    tr[rt].v = map[x1 * c + y1];
    return;
  }
  int mx = tr[rt].midx();
  int my = tr[rt].midy();
  if (x1 == x2) {
    build(la, x1, y1, mx, my);
    build(lb, x1, my + 1, mx, y2);
    tr[rt].v = MIN(tr[la].v, tr[lb].v);
  } else if (y1 == y2) {
    build(la, x1, y1, mx, my);
    build(lc, mx + 1, y1, x2, my);
    tr[rt].v = MIN(tr[la].v, tr[lc].v);
  } else {
    build(la, x1, y1, mx, my);
    build(lb, x1, my + 1, mx, y2);
    build(lc, mx + 1, y1, x2, my);
    build(ld, mx + 1, my + 1, x2, y2);
    tr[rt].v = MIN(tr[la].v, tr[lb].v);
    tr[rt].v = MIN(tr[rt].v, tr[lc].v);
    tr[rt].v = MIN(tr[rt].v, tr[ld].v);
  }
  return;
}
int query(int rt, int x1, int y1, int x2, int y2) {
  if (x1 == tr[rt].x1 && x2 == tr[rt].x2 && y2 == tr[rt].y2 &&
      y1 == tr[rt].y1) {
    return tr[rt].v;
  }
  int mx = tr[rt].midx();
  int my = tr[rt].midy();
  // 1
  if (x2 <= mx && y2 <= my)
    return query(la, x1, y1, x2, y2);
  if (y1 > my && x2 <= mx)
    return query(lb, x1, y1, x2, y2);
  if (x1 > mx && y2 <= my)
    return query(lc, x1, y1, x2, y2);
  if (mx < x1 && my < y1)
    return query(ld, x1, y1, x2, y2);
  // 2
  if (x2 <= mx) {
    int a = query(la, x1, y1, x2, my);
    int b = query(lb, x1, my + 1, x2, y2);
    return MIN(a, b);
  }
  if (y2 <= my) {
    int a = query(la, x1, y1, mx, y2);
    int b = query(lc, mx + 1, y1, x2, y2);
    return MIN(a, b);
  }
  if (x1 > mx) {
    int a = query(lc, x1, y1, x2, my);
    int b = query(ld, x1, my + 1, x2, y2);
    return MIN(a, b);
  }
  if (y1 > my) {
    int a = query(lb, x1, y1, mx, y2);
    int b = query(ld, mx + 1, y1, x2, y2);
    return MIN(a, b);
  }
  // 3
  int a = query(la, x1, y1, mx, my);
  int b = query(lb, x1, my + 1, mx, y2);
  int c = query(lc, mx + 1, y1, x2, my);
  int d = query(ld, mx + 1, my + 1, x2, y2);
  int e = MIN(a, b);
  e = MIN(e, c);
  e = MIN(e, d);
  return e;
}
int main() {
  while (scanf("%d%d%d", &r, &c, &q), r || c || q) {
    for (int i = 0; i < r; ++i)
      for (int j = 0; j < c; ++j)
        scanf("%d", &map[i * c + j]);

    build(1, 0, 0, r - 1, c - 1);
    for (int i = 0; i < q; ++i) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      printf("%d\\n", query(0, a, b, c, d));
    }
  }
  return 0;
}