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

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

int h, w, n;
int x[1000][2], y[1000][2];
vector<int> xs, ys, z;
bool a[2500][2500];
long long int ans;
typedef pair<int, int> P;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
queue<P> q, r;

void bfs(int s, int t) {
  if (a[s][t])
    return;
  //	printf("%d %d\\n", s, t);
  ans++;
  q = r;
  q.push(P(s, t));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    a[p.first][p.second] = true;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (0 <= nx && 0 <= ny && nx < xs.size() - 1 && ny < ys.size() - 1)
        if (!a[nx][ny]) {
          q.push(P(nx, ny));
          a[nx][ny] = true;
        }
    }
  }
}

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d %d %d %d", &x[i][0], &y[i][0], &x[i][1], &y[i][1]);

    xs.push_back(0);
    ys.push_back(0);
    xs.push_back(w);
    ys.push_back(h);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 2; j++) {
        xs.push_back(x[i][j]);
        ys.push_back(y[i][j]);
      }
    }
    sort(xs.begin(), xs.end());
    sort(ys.begin(), ys.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    /*
                    for(int i=0; i<xs.size(); i++) printf("%d\\n", xs[i]);
                    for(int i=0; i<ys.size(); i++) printf("%d\\n", ys[i]);
    */
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 2; j++) {
        x[i][j] = find(xs.begin(), xs.end(), x[i][j]) - xs.begin();
        y[i][j] = find(ys.begin(), ys.end(), y[i][j]) - ys.begin();
      }
    }
    /*
                    for(int i=0; i<n; i++){
                            printf("%d %d %d %d\\n", x[i][0], y[i][0], x[i][1],
       y[i][1]);
                    }
    */
    memset(a, 0, sizeof(a));
    for (int i = 0; i < n; i++) {
      for (int j = x[i][0]; j < x[i][1]; j++) {
        for (int k = y[i][0]; k < y[i][1]; k++) {
          a[j][k] = true;
        }
      }
    }
    /*
                    for(int i=0; i<xs.size(); i++){
                            for(int j=0; j<ys.size(); j++){
                                    if(a[i][j]) printf("1 ");
                                    else printf("0 ");
                            }
                            printf("\\n");
                    }
    */
    //		printf("%lu %lu\\n", xs.size(), ys.size());

    ans = 0;
    for (int i = 0; i < xs.size() - 1; i++) {
      for (int j = 0; j < ys.size() - 1; j++) {
        bfs(i, j);
      }
    }

    printf("%lld\\n", ans);
  }
}

Error: MLE

Fixed submission:

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

int h, w, n;
int x[1000][2], y[1000][2];
vector<int> xs, ys, z;
bool a[2500][2500];
long long int ans;
typedef pair<int, int> P;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
queue<P> q, r;

void bfs(int s, int t) {
  if (a[s][t])
    return;
  //	printf("%d %d\\n", s, t);
  ans++;
  q = r;
  q.push(P(s, t));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    a[p.first][p.second] = true;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (0 <= nx && 0 <= ny && nx < xs.size() - 1 && ny < ys.size() - 1)
        if (!a[nx][ny])
          q.push(P(nx, ny));
    }
  }
}

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d %d %d %d", &x[i][0], &y[i][0], &x[i][1], &y[i][1]);

    xs.push_back(0);
    ys.push_back(0);
    xs.push_back(w);
    ys.push_back(h);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 2; j++) {
        xs.push_back(x[i][j]);
        ys.push_back(y[i][j]);
      }
    }
    sort(xs.begin(), xs.end());
    sort(ys.begin(), ys.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    /*
                    for(int i=0; i<xs.size(); i++) printf("%d\\n", xs[i]);
                    for(int i=0; i<ys.size(); i++) printf("%d\\n", ys[i]);
    */
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 2; j++) {
        x[i][j] = find(xs.begin(), xs.end(), x[i][j]) - xs.begin();
        y[i][j] = find(ys.begin(), ys.end(), y[i][j]) - ys.begin();
      }
    }
    /*
                    for(int i=0; i<n; i++){
                            printf("%d %d %d %d\\n", x[i][0], y[i][0], x[i][1],
       y[i][1]);
                    }
    */
    memset(a, 0, sizeof(a));
    for (int i = 0; i < n; i++) {
      for (int j = x[i][0]; j < x[i][1]; j++) {
        for (int k = y[i][0]; k < y[i][1]; k++) {
          a[j][k] = true;
        }
      }
    }
    /*
                    for(int i=0; i<xs.size(); i++){
                            for(int j=0; j<ys.size(); j++){
                                    if(a[i][j]) printf("1 ");
                                    else printf("0 ");
                            }
                            printf("\\n");
                    }
    */
    //		printf("%lu %lu\\n", xs.size(), ys.size());

    ans = 0;
    for (int i = 0; i < xs.size() - 1; i++) {
      for (int j = 0; j < ys.size() - 1; j++) {
        bfs(i, j);
      }
    }

    printf("%lld\\n", ans);
  }
}