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

/*
     DoS
 */

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

#define INF 0xffffffff

int r;
int c;
int q;
long long int sizex;
long long int sizey;
long long int const2d;
unsigned int grid[10000000];

void update(long long int x, long long int y, unsigned int t) {
  x += sizex - 1;
  y += sizey - 1;

  grid[const2d * y + x] = t;
  while (y >= 0) {
    long long int x_ = x;

    while (x_ > 0) {
      x_ = (x_ - 1) / 2;
      grid[const2d * y + x_] =
          min(grid[const2d * y + x_ * 2 + 1], grid[const2d * y + x_ * 2 + 2]);
    }
    if (y == 0)
      break;

    y = (y - 1) / 2;
    grid[const2d * y + x] =
        min(grid[const2d * (y * 2 + 1) + x], grid[const2d * (y * 2 + 2) + x]);
  }
}

unsigned int _get_min(unsigned int *vec, int wishl, int wishr, long long int k,
                      int watchl, int watchr) {
  int mid;
  unsigned int lval, rval;

  if (wishr <= watchl || watchr <= wishl)
    return INF;
  if (wishl <= watchl && watchr <= wishr) {
    return vec[k];
  }

  mid = (watchl + watchr) / 2;
  lval = _get_min(vec, wishl, wishr, k * 2 + 1, watchl, mid);
  rval = _get_min(vec, wishl, wishr, k * 2 + 2, mid, watchr);
  return min(lval, rval);
}

unsigned int get_min(int wishl, int wishr, int wishu, int wishd,
                     long long int k, int watchu, int watchd) {
  int mid;
  unsigned int uval, dval;

  if (wishd <= watchu || watchd <= wishu)
    return INF;
  if (wishu <= watchu && watchd <= wishd) {
    return _get_min(&grid[const2d * k], wishl, wishr, 0, 0, sizex);
  }

  mid = (watchu + watchd) / 2;
  uval = get_min(wishl, wishr, wishu, wishd, k * 2 + 1, watchu, mid);
  dval = get_min(wishl, wishr, wishu, wishd, k * 2 + 2, mid, watchd);
  return min(uval, dval);
}

int main() {
  while (1) {
    scanf("%d %d %d", &r, &c, &q);
    if (r == 0 && c == 0 && q == 0)
      return 0;

    sizex = 1;
    while (sizex < c)
      sizex *= 2;
    sizey = 1;
    while (sizey < r)
      sizey *= 2;
    const2d = sizex * 2 - 1;

    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        unsigned int g;

        scanf("%u", &g);
        update(j, i, g);
      }
    }

    for (int i = 0; i < q; i++) {
      int r1, r2;
      int c1, c2;

      scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
      printf("%u\\n", get_min(c1, c2 + 1, r1, r2 + 1, 0, 0, sizey));
    }
  }
}

Error: 0

Fixed submission:

/*
    生まれ変わって出直します
 */

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

#define INF 0xffffffff

int r;
int c;
int q;
long long int sizex;
long long int sizey;
long long int const2d;
unsigned int grid[4194304];

void update(long long int x, long long int y, unsigned int t) {
  x += sizex - 1;
  y += sizey - 1;

  grid[const2d * y + x] = t;
  while (y >= 0) {
    long long int x_ = x;

    while (x_ > 0) {
      x_ = (x_ - 1) / 2;
      grid[const2d * y + x_] =
          min(grid[const2d * y + x_ * 2 + 1], grid[const2d * y + x_ * 2 + 2]);
    }
    if (y == 0)
      break;

    y = (y - 1) / 2;
    grid[const2d * y + x] =
        min(grid[const2d * (y * 2 + 1) + x], grid[const2d * (y * 2 + 2) + x]);
  }
}

unsigned int _get_min(unsigned int *vec, int wishl, int wishr, long long int k,
                      int watchl, int watchr) {
  int mid;
  unsigned int lval, rval;

  if (wishr <= watchl || watchr <= wishl)
    return INF;
  if (wishl <= watchl && watchr <= wishr) {
    return vec[k];
  }

  mid = (watchl + watchr) / 2;
  lval = _get_min(vec, wishl, wishr, k * 2 + 1, watchl, mid);
  rval = _get_min(vec, wishl, wishr, k * 2 + 2, mid, watchr);
  return min(lval, rval);
}

unsigned int get_min(int wishl, int wishr, int wishu, int wishd,
                     long long int k, int watchu, int watchd) {
  int mid;
  unsigned int uval, dval;

  if (wishd <= watchu || watchd <= wishu)
    return INF;
  if (wishu <= watchu && watchd <= wishd) {
    return _get_min(&grid[const2d * k], wishl, wishr, 0, 0, sizex);
  }

  mid = (watchu + watchd) / 2;
  uval = get_min(wishl, wishr, wishu, wishd, k * 2 + 1, watchu, mid);
  dval = get_min(wishl, wishr, wishu, wishd, k * 2 + 2, mid, watchd);
  return min(uval, dval);
}

int main() {
  while (1) {
    scanf("%d %d %d", &r, &c, &q);
    if (r == 0 && c == 0 && q == 0)
      return 0;

    sizex = 1;
    while (sizex < c)
      sizex *= 2;
    sizey = 1;
    while (sizey < r)
      sizey *= 2;
    const2d = sizex * 2 - 1;

    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        unsigned int g;

        scanf("%u", &g);
        update(j, i, g);
      }
    }

    for (int i = 0; i < q; i++) {
      int r1, r2;
      int c1, c2;

      scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
      printf("%u\\n", get_min(c1, c2 + 1, r1, r2 + 1, 0, 0, sizey));
    }
  }
}