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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
#define Seg_Max_N (1 << 18)

using Value = int;
const Value ini = 1e9;
vector<vector<int>> vs;
struct segtree {
  segtree *a;
  segtree *b;
  int minnum = 2147483647;

  segtree() {}
  segtree(vector<vector<int>> &vs, const int l, const int r, const int u,
          const int d) {
    if (l + 21 >= r && u + 21 >= d) {
      for (int x = l; x < r; ++x) {
        for (int y = u; y < d; ++y) {
          minnum = min(minnum, vs[y][x]);
        }
      }
    } else {
      if (r - l < d - u) {
        // tatenibunnkatu
        a = new segtree(vs, l, r, u, (u + d) / 2);
        b = new segtree(vs, l, r, (u + d) / 2, d);
      } else {
        a = new segtree(vs, l, (l + r) / 2, u, d);
        b = new segtree(vs, (l + r) / 2, r, u, d);
      }
      minnum = min(a->minnum, b->minnum);
    }
  }

  // min [a, b)
  int query(const int &ql, const int &qr, const int &qu, const int &qd,
            const int &nl, const int &nr, const int &nu, const int &nd) {
    if (nr <= ql || qr <= nl)
      return 2147483647;
    else if (nd <= qu || qd <= nu)
      return 2147483647;
    if (nl + 21 >= nr && nu + 21 >= nd) {
      int ans = 2147483647;
      for (int x = max(ql, nl); x < min(qr, nr); ++x) {
        for (int y = max(qu, nu); y < min(qd, nd); ++y) {
          ans = min(ans, vs[y][x]);
        }
      }
      return ans;
    }
    if (ql <= nl && nr <= qr && qu <= nu && nd <= qd) {

      return minnum;
    }
    int aa, ab;
    if (nr - nl < nd - nu) {

      aa = a->query(ql, qr, qu, qd, nl, nr, nu, (nu + nd) / 2);
      ab = b->query(ql, qr, qu, qd, nl, nr, (nu + nd) / 2, nd);
    } else {

      aa = a->query(ql, qr, qu, qd, nl, (nl + nr) / 2, nu, nd);
      ab = b->query(ql, qr, qu, qd, (nl + nr) / 2, nr, nu, nd);
    }
    int ans = min(aa, ab);
    return ans;
  }
};
int main() {
  while (1) {
    int ar, ac, q;
    cin >> ar >> ac >> q;
    if (!ar)
      break;
    vs.clear();
    vs = vector<vector<int>>(ar, vector<int>(ac));
    for (int i = 0; i < ar; ++i) {
      for (int j = 0; j < ac; ++j) {
        cin >> vs[i][j];
      }
    }
    segtree seg(vs, 0, ac, 0, ar);
    for (int i = 0; i < q; ++i) {
      int u, l, d, r;
      cin >> u >> l >> d >> r;
      d++;
      r++;
      int ans = seg.query(l, r, u, d, 0, ac, 0, ar);
      cout << ans << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
#define Seg_Max_N (1 << 18)

using Value = int;
const Value ini = 1e9;
vector<vector<int>> vs;
struct segtree {
  segtree *a;
  segtree *b;
  int minnum = 2147483647;

  segtree() {}
  segtree(vector<vector<int>> &vs, const int l, const int r, const int u,
          const int d) {
    if (l + 11 >= r && u + 11 >= d) {
      for (int x = l; x < r; ++x) {
        for (int y = u; y < d; ++y) {
          minnum = min(minnum, vs[y][x]);
        }
      }
    } else {
      if (r - l < d - u) {
        // tatenibunnkatu
        a = new segtree(vs, l, r, u, (u + d) / 2);
        b = new segtree(vs, l, r, (u + d) / 2, d);
      } else {
        a = new segtree(vs, l, (l + r) / 2, u, d);
        b = new segtree(vs, (l + r) / 2, r, u, d);
      }
      minnum = min(a->minnum, b->minnum);
    }
  }

  // min [a, b)
  int query(const int &ql, const int &qr, const int &qu, const int &qd,
            const int &nl, const int &nr, const int &nu, const int &nd) {
    if (nr <= ql || qr <= nl)
      return 2147483647;
    else if (nd <= qu || qd <= nu)
      return 2147483647;
    if (nl + 11 >= nr && nu + 11 >= nd) {
      int ans = 2147483647;
      for (int x = max(ql, nl); x < min(qr, nr); ++x) {
        for (int y = max(qu, nu); y < min(qd, nd); ++y) {
          ans = min(ans, vs[y][x]);
        }
      }
      return ans;
    }
    if (ql <= nl && nr <= qr && qu <= nu && nd <= qd) {

      return minnum;
    }
    int aa, ab;
    if (nr - nl < nd - nu) {

      aa = a->query(ql, qr, qu, qd, nl, nr, nu, (nu + nd) / 2);
      ab = b->query(ql, qr, qu, qd, nl, nr, (nu + nd) / 2, nd);
    } else {

      aa = a->query(ql, qr, qu, qd, nl, (nl + nr) / 2, nu, nd);
      ab = b->query(ql, qr, qu, qd, (nl + nr) / 2, nr, nu, nd);
    }
    int ans = min(aa, ab);
    return ans;
  }
};
int main() {
  while (1) {
    int ar, ac, q;
    cin >> ar >> ac >> q;
    if (!ar)
      break;
    vs.clear();
    vs = vector<vector<int>>(ar, vector<int>(ac));
    for (int i = 0; i < ar; ++i) {
      for (int j = 0; j < ac; ++j) {
        cin >> vs[i][j];
      }
    }
    segtree seg(vs, 0, ac, 0, ar);
    for (int i = 0; i < q; ++i) {
      int u, l, d, r;
      cin >> u >> l >> d >> r;
      d++;
      r++;
      int ans = seg.query(l, r, u, d, 0, ac, 0, ar);
      cout << ans << endl;
    }
  }

  return 0;
}