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

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int r, c, q;
int r2, c2;
const int INF = 2147483647;
vector<vector<int>> dc, dr;

int init(int k) {
  int sz = 1;
  while (sz < k)
    sz *= 2;
  return sz;
}

void update(vector<int> &seg, int k, int a) {
  k += (seg.size() / 2) - 1;
  seg[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]);
  }
}

int query(vector<int> &seg, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return seg[k];
  else {
    int vl = query(seg, a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(seg, a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

int main(void) {
  while (1) {
    scanf("%d%d%d", &r, &c, &q);
    if (r == 0 && c == 0 && q == 0)
      break;
    dc.resize(r);
    dr.resize(c);
    c2 = init(c) * 2;
    r2 = init(r) * 2;
    for (int i = 0; i < r; i++) {
      dc[i].resize(c2);
      fill(dc[i].begin(), dc[i].end(), INF);
    }
    for (int i = 0; i < c; i++) {
      dr[i].resize(r2);
      fill(dr[i].begin(), dr[i].end(), INF);
    }
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int a;
        scanf("%d", &a);
        update(dc[i], j, a);
        update(dr[j], i, a);
      }
    }
    for (int i = 0; i < q; i++) {
      int x, x2, y, y2;
      scanf("%d%d%d%d", &y, &x, &y2, &x2);
      int res = INF;
      if (y2 - y <= x2 - x) {
        for (int i = y; i <= y2; i++) {
          res = min(res, query(dc[i], x, x2 + 1, 0, 0, dc[i].size() / 2));
        }
      } else {
        for (int i = x; i <= x2; i++) {
          res = min(res, query(dr[i], y, y2 + 1, 0, 0, dr[i].size() / 2));
        }
      }
      printf("%d\\n", res);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int r, c, q;
int r2, c2;
const long long int INF = 1000000000000;
vector<vector<long long int>> dc, dr;

int init(int k) {
  int sz = 1;
  while (sz < k)
    sz *= 2;
  return sz;
}

void update(vector<long long int> &seg, int k, int a) {
  k += (seg.size() / 2) - 1;
  seg[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]);
  }
}

long long int query(vector<long long int> &seg, int a, int b, int k, int l,
                    int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return seg[k];
  else {
    long long int vl = query(seg, a, b, k * 2 + 1, l, (l + r) / 2);
    long long int vr = query(seg, a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

int main(void) {
  while (1) {
    scanf("%d%d%d", &r, &c, &q);
    if (r == 0 && c == 0 && q == 0)
      break;
    dc.resize(r);
    dr.resize(c);
    c2 = init(c) * 2;
    r2 = init(r) * 2;
    for (int i = 0; i < r; i++) {
      dc[i].resize(c2);
      fill(dc[i].begin(), dc[i].end(), INF);
    }
    for (int i = 0; i < c; i++) {
      dr[i].resize(r2);
      fill(dr[i].begin(), dr[i].end(), INF);
    }
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int a;
        scanf("%d", &a);
        update(dc[i], j, a);
        update(dr[j], i, a);
      }
    }
    for (int i = 0; i < q; i++) {
      int x, x2, y, y2;
      scanf("%d%d%d%d", &y, &x, &y2, &x2);
      long long int res = INF;
      if (y2 - y <= x2 - x) {
        for (int i = y; i <= y2; i++) {
          res = min(res, query(dc[i], x, x2 + 1, 0, 0, dc[i].size() / 2));
        }
      } else {
        for (int i = x; i <= x2; i++) {
          res = min(res, query(dr[i], y, y2 + 1, 0, 0, dr[i].size() / 2));
        }
      }
      printf("%lld\\n", res);
    }
  }
  return 0;
}