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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;

const int MAX_NUM = 1000;
int W, H, N;
int X, Y;
int x1[MAX_NUM], y1[MAX_NUM], x2[MAX_NUM], y2[MAX_NUM];

bool field[6 * MAX_NUM][6 * MAX_NUM] = {};

int dir[] = {0, 1, 0, -1, 0};

void paint(int i, int j) {
  field[i][j] = true;

  queue<P> que;
  que.push(P(i, j));

  while (!que.empty()) {
    P curr = que.front();
    que.pop();

    int x = curr.first, y = curr.second;

    for (int d = 0; d < 4; d++) {
      int nx = x + dir[d], ny = y + dir[d + 1];
      if (0 <= nx && nx < X && 0 <= ny && ny < Y && !field[nx][ny]) {
        field[nx][ny] = true;
        que.push(P(nx, ny));
      }
    }
  }
}

int compress(int x1[], int x2[], int W) {
  set<int> dist;

  for (int i = 0; i < N; i++) {
    for (int d = -1; d <= 1; d++) {
      int nx1 = x1[i] + d, nx2 = x2[i] + d;
      if (0 <= nx1 && nx1 < W)
        dist.insert(nx1);
      if (0 <= nx2 && nx2 < W)
        dist.insert(nx2);
    }
  }

  vector<int> nx;
  for (set<int>::iterator it = dist.begin(); it != dist.end(); it++) {
    nx.push_back(*it);
  }

  for (int i = 0; i < N; i++) {
    x1[i] = (int)(find(nx.begin(), nx.end(), x1[i]) - nx.begin());
    x2[i] = (int)(find(nx.begin(), nx.end(), x2[i]) - nx.begin());
  }

  return (int)nx.size();
}

int main() {
  //    freopen("aoj0531.in", "r", stdin);

  while (scanf("%d%d", &W, &H)) {
    if (W == 0 && H == 0)
      break;

    scanf("%d", &N);
    memset(field, false, sizeof(field));

    for (int i = 0; i < N; i++) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      x2[i]--;
      y2[i]--;
    }

    X = compress(x1, x2, W);
    Y = compress(y1, y2, H);

    for (int i = 0; i < N; i++) {
      for (int x = x1[i]; x <= x2[i]; x++) {
        for (int y = y1[i]; y <= y2[i]; y++) {
          field[x][y] = true;
        }
      }
    }

    int count = 0;
    for (int i = 0; i < X; i++) {
      for (int j = 0; j < Y; j++) {
        if (!field[i][j]) {
          paint(i, j);
          count++;
        }
      }
    }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;

const int MAX_NUM = 1000 + 1;
int W, H, N;
int X, Y;
int x1[MAX_NUM], y1[MAX_NUM], x2[MAX_NUM], y2[MAX_NUM];

int field[6 * MAX_NUM][6 * MAX_NUM] = {};

int dir[] = {0, 1, 0, -1, 0};

void paint(int i, int j) {
  field[i][j] = 1;

  queue<P> que;
  que.push(P(i, j));

  while (!que.empty()) {
    P curr = que.front();
    que.pop();

    int x = curr.first, y = curr.second;

    for (int d = 0; d < 4; d++) {
      int nx = x + dir[d], ny = y + dir[d + 1];
      if (0 <= nx && nx < X && 0 <= ny && ny < Y && field[nx][ny] == 0) {
        field[nx][ny] = 1;
        que.push(P(nx, ny));
      }
    }
  }
}

int compress(int x1[], int x2[], int W) {
  set<int> dist;

  for (int i = 0; i < N; i++) {
    for (int d = -1; d <= 1; d++) {
      int nx1 = x1[i] + d, nx2 = x2[i] + d;
      if (0 <= nx1 && nx1 < W)
        dist.insert(nx1);
      if (0 <= nx2 && nx2 < W)
        dist.insert(nx2);
    }
  }

  vector<int> nx;
  for (set<int>::iterator it = dist.begin(); it != dist.end(); it++) {
    nx.push_back(*it);
  }

  for (int i = 0; i < N; i++) {
    x1[i] = (int)(find(nx.begin(), nx.end(), x1[i]) - nx.begin());
    x2[i] = (int)(find(nx.begin(), nx.end(), x2[i]) - nx.begin());
  }

  return (int)nx.size();
}

int main() {
  //    freopen("aoj0531.in", "r", stdin);

  while (scanf("%d%d", &W, &H)) {
    if (W == 0 && H == 0)
      break;

    scanf("%d", &N);
    memset(field, 0, sizeof(field));

    for (int i = 0; i < N; i++) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      x2[i]--;
      y2[i]--;
    }

    X = compress(x1, x2, W);
    Y = compress(y1, y2, H);

    for (int i = 0; i < N; i++) {
      for (int x = x1[i]; x <= x2[i]; x++) {
        for (int y = y1[i]; y <= y2[i]; y++) {
          field[x][y] = 1;
        }
      }
    }

    int count = 0;
    for (int i = 0; i < X; i++) {
      for (int j = 0; j < Y; j++) {
        if (field[i][j] == 0) {
          paint(i, j);
          count++;
        }
      }
    }

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

  return 0;
}