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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

vector<vector<int>> Hs, Ws;

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

  return (sz);
}

void update(vector<int> &seg, int pos, int val) {
  pos += (seg.size() / 2) - 1;
  seg[pos] = val;

  while (pos) {
    pos = (pos - 1) / 2;
    seg[pos] = min(seg[2 * pos + 1], seg[2 * pos + 2]);
  };
}

int getMin(vector<int> &seg, int a, int b, int k, int l, int r) {
  int vl, vr;

  if (b <= l || r <= a) {
    return (INT_MAX);
  }

  if (a <= l && r <= b) {
    return (seg[k]);
  } else {
    vl = getMin(seg, a, b, k * 2 + 1, l, (l + r) / 2);
    vr = getMin(seg, a, b, k * 2 + 2, (l + r) / 2, r);
  }

  return (min(vl, vr));
}

int main() {
  int H, W, Q;

  while (1) {
    scanf("%d %d %d", &H, &W, &Q);

    if (H + W + Q == 0) {
      break;
    }

    Hs.resize(H);
    Ws.resize(W);

    int Hsz = 2 * init(W);
    int Wsz = 2 * init(H);

    for (int i = 0; i < H; i++) {
      Hs[i].resize(Hsz);
      fill(Hs[i].begin(), Hs[i].end(), INT_MAX);
    }

    for (int i = 0; i < W; i++) {
      Ws[i].resize(Wsz);
      fill(Ws[i].begin(), Ws[i].end(), INT_MAX);
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        int t;
        scanf("%d", &t);
        update(Hs[i], j, t);
        update(Ws[j], i, t);
      }
    }

    for (int i = 0; i < Q; i++) {
      int h1, w1, h2, w2;
      scanf("%d %d %d %d", &h1, &w1, &h2, &w2);
      int res = INT_MAX;

      if (h2 - h1 <= w2 - w1) {
        for (int j = h1; j <= h2; j++) {
          res = min(res, getMin(Hs[j], w1, w2 + 1, 0, 0, Hs[j].size() / 2));
        }
      } else {
        for (int j = w1; j <= w2; j++) {
          res = min(res, getMin(Ws[j], h1, h2 + 1, 0, 0, Ws[j].size() / 2));
        }
      }

      printf("%d\\n", res);
    }
  }

  return (0);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

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

  return (sz);
}

void update(vector<int> &seg, int pos, int val) {
  pos += (seg.size() / 2) - 1;
  seg[pos] = val;

  while (pos) {
    pos = (pos - 1) / 2;
    seg[pos] = min(seg[2 * pos + 1], seg[2 * pos + 2]);
  };
}

int getMin(vector<int> &seg, int a, int b, int k, int l, int r) {
  int vl, vr;

  if (b <= l || r <= a) {
    return (INT_MAX);
  }

  if (a <= l && r <= b) {
    return (seg[k]);
  } else {
    vl = getMin(seg, a, b, k * 2 + 1, l, (l + r) / 2);
    vr = getMin(seg, a, b, k * 2 + 2, (l + r) / 2, r);
  }

  return (min(vl, vr));
}

int main() {
  int H, W, Q;

  while (1) {
    scanf("%d %d %d", &H, &W, &Q);

    if (H + W + Q == 0) {
      break;
    }

    vector<vector<int>> Hs(H, vector<int>(2 * init(W)));
    vector<vector<int>> Ws(W, vector<int>(2 * init(H)));

    for (int i = 0; i < H; i++) {
      fill(Hs[i].begin(), Hs[i].end(), INT_MAX);
    }

    for (int i = 0; i < W; i++) {
      fill(Ws[i].begin(), Ws[i].end(), INT_MAX);
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        int t;
        scanf("%d", &t);
        update(Hs[i], j, t);
        update(Ws[j], i, t);
      }
    }

    for (int i = 0; i < Q; i++) {
      int h1, w1, h2, w2;
      scanf("%d %d %d %d", &h1, &w1, &h2, &w2);
      int res = INT_MAX;

      if (h2 - h1 <= w2 - w1) {
        for (int j = h1; j <= h2; j++) {
          res = min(res, getMin(Hs[j], w1, w2 + 1, 0, 0, Hs[j].size() / 2));
        }
      } else {
        for (int j = w1; j <= w2; j++) {
          res = min(res, getMin(Ws[j], h1, h2 + 1, 0, 0, Ws[j].size() / 2));
        }
      }

      printf("%d\\n", res);
    }
  }

  return (0);
}