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 INF 1000000000000
using namespace std;
typedef long long ll;

int r, c, q, S;
vector<ll> v[1000];

void init(int x, int y) {
  rep(i, x) {
    rep(j, y * 4) { v[i][j] = INF; }
  }
  S = 1;
  while (S <= y)
    S *= 2;
}

void update(int y, int x, ll t) {
  x += S - 1;
  v[y][x] = t;
  while (x > 0) {
    x = (x - 1) / 2;
    v[y][x] = min(v[y][2 * x + 1], v[y][2 * x + 2]);
  }
}

ll get(int z, int a, int b, int t, int r, int l) {
  if (a >= l || b <= r)
    return INF;
  if (a <= r && l <= b)
    return v[z][t];
  return min(get(z, a, b, t * 2 + 1, r, (r + l) / 2),
             get(z, a, b, t * 2 + 2, (r + l) / 2, l));
}

int main() {
  while (scanf("%d%d%d", &r, &c, &q), r || c || q) {
    for (int i = 0; i < min(r, c); i++) {
      while (v[i].size() < max(r, c) * 4)
        v[i].push_back(0);
    }
    init(min(r, c), max(r, c));
    if (r < c) {
      ll tmp;
      rep(i, r) rep(j, c) {
        scanf("%lld", &tmp);
        update(i, j, tmp);
      }
      rep(i, q) {
        int y1, x1, y2, x2;
        ll ans = INF;
        scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
        for (int j = y1; j <= y2; j++) {
          ans = min(ans, get(j, x1, x2 + 1, 0, 0, S));
        }
        printf("%lld\\n", ans);
      }
    } else {
      ll tmp;
      rep(i, r) rep(j, c) {
        scanf("%lld", &tmp);
        update(j, i, tmp);
      }
      swap(r, c);
      rep(i, q) {
        int y1, x1, y2, x2;
        ll ans = INF;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        for (int j = y1; j <= y2; j++) {
          ans = min(ans, get(j, x1, x2 + 1, 0, 0, S));
        }
        printf("%lld\\n", ans);
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000000
using namespace std;
typedef long long ll;

int r, c, q, S;
vector<ll> v[1000];

void init(int x, int y) {
  rep(i, x) {
    rep(j, y * 2) { v[i][j] = INF; }
  }
  S = 1;
  while (S <= y)
    S *= 2;
}

void update(int y, int x, ll t) {
  x += S - 1;
  v[y][x] = t;
  while (x > 0) {
    x = (x - 1) / 2;
    v[y][x] = min(v[y][2 * x + 1], v[y][2 * x + 2]);
  }
}

ll get(int z, int a, int b, int t, int r, int l) {
  if (a >= l || b <= r)
    return INF;
  if (a <= r && l <= b)
    return v[z][t];
  return min(get(z, a, b, t * 2 + 1, r, (r + l) / 2),
             get(z, a, b, t * 2 + 2, (r + l) / 2, l));
}

int main() {
  rep(i, 1000) {
    rep(j, 1000000 / (i + 1) * 2) { v[i].push_back(0); }
  }
  while (scanf("%d%d%d", &r, &c, &q), r || c || q) {
    init(min(r, c), max(r, c));
    if (r < c) {
      ll tmp;
      rep(i, r) rep(j, c) {
        scanf("%lld", &tmp);
        update(i, j, tmp);
      }
      rep(i, q) {
        int y1, x1, y2, x2;
        ll ans = INF;
        scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
        for (int j = y1; j <= y2; j++) {
          ans = min(ans, get(j, x1, x2 + 1, 0, 0, S));
        }
        printf("%lld\\n", ans);
      }
    } else {
      ll tmp;
      rep(i, r) rep(j, c) {
        scanf("%lld", &tmp);
        update(j, i, tmp);
      }
      swap(r, c);
      rep(i, q) {
        int y1, x1, y2, x2;
        ll ans = INF;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        for (int j = y1; j <= y2; j++) {
          ans = min(ans, get(j, x1, x2 + 1, 0, 0, S));
        }
        printf("%lld\\n", ans);
      }
    }
  }
}