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: 0

Fixed submission:

#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;
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() {
  try {
    rmq = new int **[R];
    for (int i = 0; i < R; i++) {
      rmq[i] = new int *[11];
      for (int k = 0; (1 << k) - 1 < C; k++) {
        rmq[i][k] = new int[C];
        for (int j = 0; j + (1 << k) - 1 < C; j++) {
          rmq[i][k][j] =
              k == 0 ? getvalue(i, j)
                     : min(rmq[i][k - 1][j], rmq[i][k - 1][j + (1 << k - 1)]);
        }
      }
    }
  } catch (bad_alloc &ba) {
    while (true)
      ;
  }
}

void rmq_destory() {
  for (int i = 0; i < R; i++) {
    for (int k = 0; (1 << k) - 1 < C; k++) {
      delete[] rmq[i][k];
    }
    delete[] rmq[i];
  }
  delete[] rmq;
}

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);
    }
    rmq_destory();
  }
  return 0;
}