Problem p00653 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

const int INF = INT_MAX;

typedef long long ll;
typedef pair<int, int> P;

int *dat;
int R, C, W;

void update(int r, int c, int x) {
  r += R - 1;
  c += C - 1;
  dat[r * W + c] = x;
  int cc = c;
  while (cc > 0) {
    cc = (cc - 1) / 2;
    dat[r * W + cc] = min(dat[r * W + cc * 2 + 1], dat[r * W + cc * 2 + 2]);
  } // end while

  while (r > 0) {
    r = (r - 1) / 2;
    cc = c;
    while (true) {
      dat[r * W + cc] =
          min(dat[(r * 2 + 1) * W + cc], dat[(r * 2 + 2) * W + cc]);
      if (cc == 0)
        break;
      cc = (cc - 1) / 2;
    } // end while
  }   // end while
}

int query(int r1, int c1, int r2, int c2, int r, int rl, int rr, int c, int cl,
          int cr) {
  if (rr <= r1 || r2 <= rl || cr <= c1 || c2 <= cl)
    return INF;
  if (r1 <= rl && rr <= r2 && c1 <= cl && cr <= c2)
    return dat[r * W + c];
  if (r1 <= rl && rr <= r2) {
    int v1 = query(r1, c1, r2, c2, r, rl, rr, c * 2 + 1, cl, (cl + cr) / 2);
    int v2 = query(r1, c1, r2, c2, r, rl, rr, c * 2 + 2, (cl + cr) / 2, cr);
    return min(v1, v2);
  } else {
    int v1 = query(r1, c1, r2, c2, r * 2 + 1, rl, (rl + rr) / 2, c, cl, cr);
    int v2 = query(r1, c1, r2, c2, r * 2 + 2, (rl + rr) / 2, rr, c, cl, cr);
    return min(v1, v2);
  } // end if
}

int main() {
  int RR, CC, Q;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> RR >> CC >> Q) {
    R = 1, C = 1;
    while (R < RR)
      R *= 2;
    while (C < CC)
      C *= 2;
    W = C * 2 - 1;

    dat = new int[(R * 2 - 1) * (C * 2 - 1)];

    rep(i, RR) {
      rep(j, CC) {
        int x;
        cin >> x;
        update(i, j, x);
      } // end rep
    }   // end rep
    rep(i, Q) {
      int r1, c1, r2, c2;
      cin >> r1 >> c1 >> r2 >> c2;
      int ans = query(r1, c1, r2 + 1, c2 + 1, 0, 0, R, 0, 0, C);
      cout << ans << endl;
    } // end rep

    delete[] dat;
    dat = 0;

  } // end while

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

const int INF = INT_MAX;

typedef long long ll;
typedef pair<int, int> P;

int **dat;
int R, C;

void update(int r, int c, int x) {
  r += R - 1;
  c += C - 1;
  dat[r][c] = x;
  int cc = c;
  while (cc > 0) {
    cc = (cc - 1) / 2;
    dat[r][cc] = min(dat[r][cc * 2 + 1], dat[r][cc * 2 + 2]);
  } // end while

  while (r > 0) {
    r = (r - 1) / 2;
    cc = c;
    while (true) {
      dat[r][cc] = min(dat[r * 2 + 1][cc], dat[r * 2 + 2][cc]);
      if (cc == 0)
        break;
      cc = (cc - 1) / 2;
    } // end while
  }   // end while
}

int query(int r1, int c1, int r2, int c2, int r, int rl, int rr, int c, int cl,
          int cr) {
  if (rr <= r1 || r2 <= rl || cr <= c1 || c2 <= cl)
    return INF;
  if (r1 <= rl && rr <= r2 && c1 <= cl && cr <= c2)
    return dat[r][c];
  if (r1 <= rl && rr <= r2) {
    int v1 = query(r1, c1, r2, c2, r, rl, rr, c * 2 + 1, cl, (cl + cr) / 2);
    int v2 = query(r1, c1, r2, c2, r, rl, rr, c * 2 + 2, (cl + cr) / 2, cr);
    return min(v1, v2);
  } else {
    int v1 = query(r1, c1, r2, c2, r * 2 + 1, rl, (rl + rr) / 2, c, cl, cr);
    int v2 = query(r1, c1, r2, c2, r * 2 + 2, (rl + rr) / 2, rr, c, cl, cr);
    return min(v1, v2);
  } // end if
}

int main() {
  int RR, CC, Q;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> RR >> CC >> Q) {
    R = 1, C = 1;
    while (R < RR)
      R *= 2;
    while (C < CC)
      C *= 2;

    dat = new int *[R * 2 - 1];
    for (int i = 0; i < R * 2 - 1; ++i) {
      dat[i] = new int[C * 2 - 1];
    } // end for

    rep(i, RR) {
      rep(j, CC) {
        int x;
        cin >> x;
        update(i, j, x);
      } // end rep
    }   // end rep
    rep(i, Q) {
      int r1, c1, r2, c2;
      cin >> r1 >> c1 >> r2 >> c2;
      int ans = query(r1, c1, r2 + 1, c2 + 1, 0, 0, R, 0, 0, C);
      cout << ans << endl;
    } // end rep

    for (int i = 0; i < R * 2 - 1; ++i) {
      delete[] dat[i];
      dat[i] = 0;
    } // end for
    delete[] dat;
    dat = 0;

  } // end while

  return 0;
}