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

#include <bits/stdc++.h>
using namespace std;
using PP = pair<int, int>;

int n, m;
map<long, int> mx;
map<long, int> my;
int h, w;
long a[1000];
long b[1000];
long c[1000];
long d[1000];
long e[1000];
long f[1000];
long vx[6001];
long vy[6001];
bool field[6001][6001];
bool used[6001][6001];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

long rec(int x, int y) {
  long sum = 0;
  stack<PP> st;
  st.push(PP(x, y));

  while (!st.empty()) {
    auto [x, y] = st.top();
    st.pop();
    if (field[x][y])
      continue;
    if (used[x][y])
      continue;
    used[x][y] = true;
    if (x == 0 || x == h - 1 || y == 0 || y == w - 1)
      return -1;

    if (x % 2 == 0 && y % 2 == 0) {
      sum += (vx[x + 1] - vx[x - 1]) * (vy[y + 1] - vy[y - 1]);
    }

    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      st.push(PP(nx, ny));
    }
  }
  return sum;
}

string solve() {
  for (int i = 0; i < n; ++i) {
    int x1 = mx[a[i]];
    int x2 = mx[b[i]];
    int y = my[c[i]];
    for (int k = x1; k <= x2; ++k) {
      field[k][y] = true;
    }
  }
  for (int i = 0; i < m; ++i) {
    int x = mx[d[i]];
    int y1 = my[e[i]];
    int y2 = my[f[i]];
    for (int k = y1; k <= y2; ++k) {
      field[x][k] = true;
    }
  }

  h = 2 * mx.size() + 1;
  w = 2 * my.size() + 1;
  auto itx = mx.lower_bound(0);
  auto ity = my.lower_bound(0);
  int sx = (itx == mx.end() ? h : itx->second) - 1;
  int sy = (ity == my.end() ? w : ity->second) - 1;
  long res = rec(sx, sy);
  return res < 0 ? "INF" : to_string(res);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    mx[a[i]];
    mx[b[i]];
    my[c[i]];
  }
  for (int i = 0; i < m; ++i) {
    cin >> d[i] >> e[i] >> f[i];
    mx[d[i]];
    my[e[i]];
    my[f[i]];
  }

  int cnt = 1;
  for (auto &&[p, q] : mx) {
    q = cnt;
    vx[cnt] = p;
    cnt += 2;
  }
  cnt = 1;
  for (auto &&[p, q] : my) {
    q = cnt;
    vy[cnt] = p;
    cnt += 2;
  }

  cout << solve() << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using PP = pair<long, long>;

int n, m;
map<long, int> mx;
map<long, int> my;
int h, w;
long a[1000];
long b[1000];
long c[1000];
long d[1000];
long e[1000];
long f[1000];
long vx[12001];
long vy[12001];
bool field[12001][12001];
bool used[12001][12001];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

void MLE() {
  std::vector<int> big_array(1 << 29);
  std::cerr << big_array.back() << std::endl;
  exit(0);
}

long rec(int x, int y) {
  if (x == 0 || x == h - 1 || y == 0 || y == w - 1)
    return -1;
  if (field[x][y])
    return 0;
  if (used[x][y])
    return 0;
  used[x][y] = true;

  long sum = 0;
  if (x % 2 == 1 || y % 2 == 1) {
    sum = 0;
  } else {
    sum = (vx[x + 1] - vx[x - 1]) * (vy[y + 1] - vy[y - 1]);
  }

  for (int k = 0; k < 4; ++k) {
    int nx = x + dx[k];
    int ny = y + dy[k];
    long res = rec(nx, ny);
    if (res < 0)
      return -1;
    sum += res;
  }
  return sum;
}

string solve() {
  for (int i = 0; i < n; ++i) {
    int x1 = mx[a[i]];
    int x2 = mx[b[i]];
    int y = my[c[i]];
    for (int k = x1; k <= x2; ++k) {
      field[k][y] = true;
    }
  }
  for (int i = 0; i < m; ++i) {
    int x = mx[d[i]];
    int y1 = my[e[i]];
    int y2 = my[f[i]];
    for (int k = y1; k <= y2; ++k) {
      field[x][k] = true;
    }
  }

  h = 2 * mx.size() + 1;
  w = 2 * my.size() + 1;
  auto itx = mx.lower_bound(0);
  auto ity = my.lower_bound(0);
  int sx = (itx == mx.end() ? h : itx->second) - 1;
  int sy = (ity == my.end() ? w : ity->second) - 1;
  MLE();
  long res = rec(sx, sy);
  return res < 0 ? "INF" : to_string(res);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    mx[a[i]];
    mx[b[i]];
    my[c[i]];
  }
  for (int i = 0; i < m; ++i) {
    cin >> d[i] >> e[i] >> f[i];
    mx[d[i]];
    my[e[i]];
    my[f[i]];
  }

  int cnt = 1;
  for (auto &&[p, q] : mx) {
    q = cnt;
    vx[cnt] = p;
    cnt += 2;
  }
  cnt = 1;
  for (auto &&[p, q] : my) {
    q = cnt;
    vy[cnt] = p;
    cnt += 2;
  }

  cout << solve() << endl;
}