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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int mo[5] = {0, 1, 0, -1, 0};
int ans, n, X1[1000], X2[1000], Y1[1000], Y2[1000], mm[2010][2010] = {0};

void calc(int w, int h) {

  for (int i = 0; i < 2010; i++)
    for (int j = 0; j < 2010; j++)
      mm[i][j] = 0;

  ans = 0;

  vector<int> x, y;
  map<int, int> mx, my;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);

    x.push_back(X1[i]);
    x.push_back(X2[i]);
    y.push_back(Y1[i]);
    y.push_back(Y2[i]);
  }

  x.push_back(0);
  x.push_back(w);
  y.push_back(0);
  y.push_back(h);

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  for (int i = 0; i < x.size(); i++) {
    mx[x[i]] = i;
  }
  for (int i = 0; i < y.size(); i++) {
    my[y[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    mm[my[Y1[i]]][mx[X1[i]]]++;
    mm[my[Y1[i]]][mx[X2[i]]]--;
    mm[my[Y2[i]]][mx[X1[i]]]--;
    mm[my[Y2[i]]][mx[X2[i]]]++;
  }

  for (int i = 0; i < y.size(); i++) {
    for (int j = 0; j < x.size(); j++) {
      mm[i][j + 1] += mm[i][j];
    }
  }

  for (int i = 0; i < x.size(); i++) {
    for (int j = 0; j < y.size(); j++) {
      mm[j + 1][i] += mm[j][i];
    }
  }

  for (int i = 0; i < y.size() - 1; i++) {
    for (int j = 0; j < x.size() - 1; j++) {
      if (mm[i][j] > 0)
        continue;

      queue<pair<int, int>> q;
      q.push(make_pair(i, j));
      mm[i][j] = 1;
      ans++;

      while (!q.empty()) {
        pair<int, int> p = q.front();
        q.pop();

        for (int k = 0; k < 4; k++) {
          if (0 <= p.first + mo[k] && p.first + mo[k] < y.size() - 1 &&
              0 <= p.second + mo[k + 1] &&
              p.second + mo[k + 1] < x.size() - 1) {
            if (mm[p.first + mo[k]][p.second + mo[k + 1]] == 0) {
              q.push(make_pair(p.first + mo[k], p.second + mo[k + 1]));
              mm[p.first + mo[k]][p.second + mo[k + 1]] = 1;
            }
          }
        }
      }
    }
  }

  printf("%d\\n", ans);

  return;
}

int main() {
  int w, h;

  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0)
      break;
    calc(w, h);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int mo[5] = {0, 1, 0, -1, 0};
int ans, n, X1[1000], X2[1000], Y1[1000], Y2[1000], mm[4010][4010] = {0};

void calc(int w, int h) {

  for (int i = 0; i < 4010; i++)
    for (int j = 0; j < 4010; j++)
      mm[i][j] = 0;

  ans = 0;

  vector<int> x, y;
  map<int, int> mx, my;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);

    x.push_back(X1[i]);
    x.push_back(X2[i]);
    x.push_back(X1[i] + 1);
    y.push_back(Y1[i]);
    y.push_back(Y2[i]);
    y.push_back(Y1[i] + 1);
  }

  x.push_back(0);
  x.push_back(w);
  y.push_back(0);
  y.push_back(h);

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  for (int i = 0; i < x.size(); i++) {
    mx[x[i]] = i;
  }
  for (int i = 0; i < y.size(); i++) {
    my[y[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    mm[my[Y1[i]]][mx[X1[i]]]++;
    mm[my[Y1[i]]][mx[X2[i]]]--;
    mm[my[Y2[i]]][mx[X1[i]]]--;
    mm[my[Y2[i]]][mx[X2[i]]]++;
  }

  for (int i = 0; i < y.size(); i++) {
    for (int j = 0; j < x.size(); j++) {
      mm[i][j + 1] += mm[i][j];
    }
  }

  for (int i = 0; i < x.size(); i++) {
    for (int j = 0; j < y.size(); j++) {
      mm[j + 1][i] += mm[j][i];
    }
  }

  for (int i = 0; i < y.size() - 1; i++) {
    for (int j = 0; j < x.size() - 1; j++) {
      if (mm[i][j] > 0)
        continue;

      queue<pair<int, int>> q;
      q.push(make_pair(i, j));
      mm[i][j] = 1;
      ans++;

      while (!q.empty()) {
        pair<int, int> p = q.front();
        q.pop();

        for (int k = 0; k < 4; k++) {
          if (0 <= p.first + mo[k] && p.first + mo[k] < y.size() - 1 &&
              0 <= p.second + mo[k + 1] &&
              p.second + mo[k + 1] < x.size() - 1) {
            if (mm[p.first + mo[k]][p.second + mo[k + 1]] == 0) {
              q.push(make_pair(p.first + mo[k], p.second + mo[k + 1]));
              mm[p.first + mo[k]][p.second + mo[k + 1]] = 1;
            }
          }
        }
      }
    }
  }

  printf("%d\\n", ans);

  return;
}

int main() {
  int w, h;

  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0)
      break;
    calc(w, h);
  }

  return 0;
}