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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define PB pop_back
#define bk back()
#define fs first
#define se second
#define INF 1e9 * 2 + 10
#define sq(x) (x) * (x)
#define eps (1e-7)
#define clr(x) memset((x), 0, sizeof(x))
#define cp(a, b) memcpy((a), (b), sizeof(b))

const int maxn = 1005;
int W, H, N;
int x1[maxn], x2[maxn], Y1[maxn], Y2[maxn];
int fld[maxn * 3][maxn * 3];
int dir[4][2] = {1, 0, 0, 1, 0, -1, -1, 0};

int compress(int *x1, int *x2, int w) {
  vector<int> v;
  for (int i = 1; i <= N; i++) {
    int tx1, tx2;
    for (int d = -1; d <= 1; d++) {
      tx1 = x1[i] + d;
      tx2 = x2[i] + d;
      if (tx1 >= 0 && tx1 < w)
        v.pb(tx1);
      if (tx2 >= 0 && tx2 < w)
        v.pb(tx2);
    }
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 1; i <= N; i++) {
    x1[i] = lower_bound(v.begin(), v.end(), x1[i]) - v.begin();
    x2[i] = lower_bound(v.begin(), v.end(), x2[i]) - v.begin();
  }
  return v.size();
}

void solve() {
  W = compress(x1, x2, W);
  H = compress(Y1, Y2, H);
  clr(fld);
  for (int i = 1; i <= N; i++)
    for (int y = Y1[i]; y <= Y2[i]; y++)
      for (int x = x1[i]; x <= x2[i]; x++)
        fld[y][x] = 1;
  int ans = 0;
  /*
  for(int x=0;x<W;x++)
  {
      for(int y=0;y<H-1;y++) cout<<fld[x][y]<<" ";
      cout<<fld[x][H-1]<<endl;
  }
  */
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++) {
      if (fld[y][x])
        continue;
      fld[y][x] = 1;
      ans++;
      queue<pair<int, int>> q;
      q.push(make_pair(x, y));
      while (!q.empty()) {
        int xx, yy, nx, ny;
        xx = q.front().fs, yy = q.front().se;
        q.pop();
        for (int i = 0; i < 4; i++) {
          nx = xx + dir[i][0];
          ny = yy + dir[i][1];
          if (nx < 0 || nx >= W || ny < 0 || ny >= H || fld[ny][nx])
            continue;
          fld[ny][nx] = 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  printf("%d\\n", ans);
}

int main() {
  while (~scanf("%d%d", &W, &H) && W && H) {
    scanf("%d", &N);
    for (
        int i = 1; i <= N;
        i++) // 注意?里，把?个坐?当成?上面的一个一个格子。。。。。然后照着做就行了.......
    {
      scanf("%d%d%d%d", &x1[i], &Y1[i], &x2[i], &Y2[i]);
      x2[i] -= 1;
      Y2[i] -= 1;
    }
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

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

const int MAX_N = 1550;
int W, H, N;
int x1[MAX_N], x2[MAX_N], y1[MAX_N], y2[MAX_N];

bool fld[MAX_N * 6][MAX_N * 6];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

void show(vector<int> x) {
  int len = x.size();
  for (int i = 0; i < len; i++)
    printf("%d ", x[i]);
  printf("\\n");
}

//?x1和x2?行坐??散化，并返回?散化之后的?度
int compress(int *x1, int *x2, int w) {
  vector<int> xs;

  for (int i = 0; i < N; i++) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if (1 <= tx1 && tx1 <= W)
        xs.push_back(tx1);
      if (1 <= tx2 && tx2 <= W)
        xs.push_back(tx2);
    }
  }
  //    printf("\\n");   show(xs);    printf("\\n");
  sort(xs.begin(), xs.end());
  //    printf("\\n");   show(xs);    printf("\\n");
  xs.erase(unique(xs.begin(), xs.end()),
           xs.end()); // unique函数?除?表中所有重?的元素
  //    printf("\\n");   show(xs);    printf("\\n");
  for (int i = 0; i < N; i++) {
    x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  return xs.size();
}

void debug() {
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      printf("%d ", fld[i][j]);
    }
    printf("\\n");
  }
}

void debug(int a[]) {
  for (int i = 0; i < N; i++)
    printf("%d ", a[i]);
  printf("\\n");
}

void solve() {
  //    printf("x1;\\n");debug(x1);  printf("x2;\\n");debug(x2);
  //    printf("y1;\\n");debug(y1);  printf("y2;\\n");debug(y2);
  // 坐??散化
  W = compress(x1, x2, W);
  H = compress(y1, y2, H);

  memset(fld, 0, sizeof(fld));
  //    printf("x1;\\n");debug(x1);  printf("x2;\\n");debug(x2);
  //    printf("y1;\\n");debug(y1);  printf("y2;\\n");debug(y2);
  for (int i = 0; i < N; i++) {
    for (int y = y1[i]; y <= y2[i]; y++) {
      for (int x = x1[i]; x <= x2[i]; x++) {
        fld[y][x] = true;
      }
    }
  }
  //    debug();
  // 求区?的个数
  long long ans = 0;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (fld[y][x])
        continue;
      ans++;

      //?度?先搜索
      queue<pair<int, int>> que;
      que.push(make_pair(x, y));
      while (!que.empty()) {
        int sx = que.front().first, sy = que.front().second;
        que.pop();
        for (int i = 0; i < 4; i++) {
          int tx = sx + dx[i], ty = sy + dy[i];
          if (tx < 0 || W <= tx || ty < 0 || H <= ty)
            continue;
          if (fld[ty][tx])
            continue;
          que.push(make_pair(tx, ty));
          fld[ty][tx] = true;
        }
      }
    }
  }
  cout << ans << endl;
  //    printf("%d\\n", ans);
}

int main(void) {
  while (cin >> W >> H, W + H) {
    cin >> N;
    for (int i = 0; i < N; ++i) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    }
    solve();
  }
  return 0;
}