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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define MAXN 1000
using namespace std;

int w, h, n;
int x1[MAXN + 5], y1[MAXN + 5], x2[MAXN + 5], y2[MAXN + 5];
int fld[2 * MAXN][2 * MAXN];
int f[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}};

int work(int *x, int *y, int t) {
  vector<int> xx;
  for (int i = 0; i < n; i++) {
    if (1 <= x[i] && x[i] <= t)
      xx.push_back(x[i]);
    if (1 <= y[i] && y[i] <= t)
      xx.push_back(y[i]);
  }
  xx.push_back(0);
  xx.push_back(t);
  sort(xx.begin(), xx.end());
  xx.erase(unique(xx.begin(), xx.end()), xx.end());
  for (int i = 0; i < n; i++) {
    x[i] = find(xx.begin(), xx.end(), x[i]) - xx.begin();
    y[i] = find(xx.begin(), xx.end(), y[i]) - xx.begin();
  }
  return xx.size() - 1;
}

int bfs() {
  int ans = 0;
  for (int j = 0; j < h; j++)
    for (int i = 0; i < w; i++) {
      if (fld[j][i])
        continue;
      ans++;
      queue<pair<int, int>> q;
      q.push(make_pair(i, j));
      while (!q.empty()) {
        pair<int, int> t;
        t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
          int nx = t.first + f[i][0];
          int ny = t.second + f[i][1];
          if (nx < 0 || ny < 0 || nx > w || ny > h)
            continue;
          if (fld[ny][nx] > 0)
            continue;
          q.push(make_pair(nx, ny));
          fld[ny][nx] = 1;
        }
      }
    }
  return ans;
}

int main() {
  while (1) {
    memset(fld, 0, sizeof fld);
    scanf("%d%d", &w, &h);
    if (!w && !h)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
    w = work(x1, x2, w);
    h = work(y1, y2, h);
    // printf("*%d %d\\n",w,h);
    for (int i = 0; i < n; i++) {
      fld[y1[i]][x1[i]]++;
      fld[y1[i]][x2[i]]--;
      fld[y2[i]][x1[i]]--;
      fld[y2[i]][x2[i]]++;
    }
    for (int i = 0; i < h; i++)
      for (int j = 1; j < w; j++)
        fld[i][j] += fld[i][j - 1];
    for (int i = 1; i < h; i++)
      for (int j = 0; j < w; j++)
        fld[i][j] += fld[i - 1][j];
    printf("%d\\n", bfs());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define MAXN 1000
using namespace std;

int w, h, n;
int x1[MAXN + 5], y1[MAXN + 5], x2[MAXN + 5], y2[MAXN + 5];
int fld[2 * MAXN][2 * MAXN];
int f[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}};

void Init() { memset(fld, 0, sizeof fld); }

int work(int x[], int y[], int t) {
  vector<int> xx;
  for (int i = 1; i <= n; i++) {
    xx.push_back(x[i]);
    xx.push_back(y[i]);
  }
  xx.push_back(0);
  xx.push_back(t);
  sort(xx.begin(), xx.end());
  xx.erase(unique(xx.begin(), xx.end()), xx.end());
  for (int i = 1; i <= n; i++) {
    x[i] = find(xx.begin(), xx.end(), x[i]) - xx.begin();
    y[i] = find(xx.begin(), xx.end(), y[i]) - xx.begin();
  }
  return xx.size() - 1;
}

int bfs() {
  int ans = 0;
  for (int i = 1; i <= w; i++)
    for (int j = 1; j <= h; j++) {
      if (fld[i][j])
        continue;
      ans++;
      queue<pair<int, int>> q;
      q.push(make_pair(i, j));
      while (!q.empty()) {
        pair<int, int> t;
        t = q.front();
        q.pop();

        fld[t.first][t.second] = 1;
        for (int i = 0; i < 4; i++) {
          int nx = t.first + f[i][0];
          int ny = t.second + f[i][1];
          if (nx < 1 || ny < 1 || nx > w || ny > h || fld[nx][ny])
            continue;
          q.push(make_pair(nx, ny));
        }
      }
    }
  return ans;
}

int main() {
  while (1) {
    Init();
    scanf("%d%d", &w, &h);
    if (!w)
      break;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
    w = work(x1, x2, w);
    h = work(y1, y2, h);
    // printf("*%d %d\\n",w,h);
    for (int i = 1; i <= n; i++) {
      fld[x1[i]][y1[i]]++;
      fld[x1[i]][y2[i]]--;
      fld[x2[i]][y1[i]]--;
      fld[x2[i]][y2[i]]++;
    }
    for (int i = 1; i <= w; i++)
      for (int j = 1; j <= h; j++) {
        fld[i][j] += f[i - 1][j];
        fld[i][j] += f[i][j - 1];
      }
    printf("%d\\n", bfs());
  }
  return 0;
}