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


#include <algorithm>
#include <cstring>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

int w, h, n;

int compress(int *x1, int *x2) {
  vector<int> tmp;
  for (int i = 0; i <= n; ++i) {
    tmp.push_back(x1[i]);
    tmp.push_back(x2[i]);
  }
  sort(tmp.begin(), tmp.end());
  tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
  for (int i = 0; i < n; ++i) {
    x1[i] = find(tmp.begin(), tmp.end(), x1[i]) - tmp.begin();
    x2[i] = find(tmp.begin(), tmp.end(), x2[i]) - tmp.begin();
  }
  return tmp.size() - 1;
}

int x1_[1234], x2_[1234], y1_[1234], y2_[1234];

int ww, hh;
bool mask[1234][1234];
const int dxy[5] = {1, 0, -1, 0, 1};
void bfs(int x, int y) {
  queue<pair<int, int>> que;
  mask[x][y] = true;
  que.emplace(x, y);
  while (!que.empty()) {
    x = que.front().first;
    y = que.front().second;
    que.pop();
    for (int dd = 0; dd < 4; ++dd) {
      if (!(0 <= x + dxy[dd] && x + dxy[dd] < ww) ||
          !(0 <= y + dxy[dd + 1] && y + dxy[dd + 1] < hh)) {
        continue;
      }
      if (!mask[x + dxy[dd]][y + dxy[dd + 1]]) {
        que.emplace(x + dxy[dd], y + dxy[dd + 1]);
        mask[x + dxy[dd]][y + dxy[dd + 1]] = true;
      }
    }
  }
}

int main() {
  for (;;) {
    scanf("%d %d", &w, &h);
    if (h == 0)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
      scanf("%d %d %d %d", x1_ + i, y1_ + i, x2_ + i, y2_ + i);
    }
    x1_[n] = 0;
    x2_[n] = w;
    y1_[n] = 0;
    y2_[n] = h;
    ww = compress(x1_, x2_);
    hh = compress(y1_, y2_);
    memset(mask, false, sizeof(mask));
    for (int i = 0; i < n; ++i) {
      for (int p = x1_[i]; p < x2_[i]; ++p) {
        for (int q = y1_[i]; q < y2_[i]; ++q) {
          mask[p][q] = true;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < ww; ++i) {
      for (int j = 0; j < hh; ++j) {
        if (!mask[i][j]) {
          bfs(i, j);
          ++ans;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 10000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define all(n) n.begin(), n.end()

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

const int MAX_N = 1000;

int w, h, n;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
int fld[6 * MAX_N][6 * MAX_N];

int compress(int *x1, int *x2, int W) {
  vector<int> xs;

  REP(i, n) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if (0 <= tx1 && tx1 <= W * 2)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 <= W * 2)
        xs.push_back(tx2);
    }
  }

  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  REP(i, n) {
    x1[i] = find(all(xs), x1[i]) - xs.begin();
    x2[i] = find(all(xs), x2[i]) - xs.begin();
  }
  return xs.size();
}

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void dfs(int x, int y) {
  if (fld[x][y])
    return;

  fld[x][y] = true;

  REP(i, 4) {

    // int tx = x + dx[i];
    // int ty = y + dy[i];
    if (0 <= x + dx[i] && x + dx[i] < w && 0 <= y + dy[i] && y + dy[i] < h &&
        !fld[x][y])
      dfs(x + dx[i], y + dy[i]);
  }
}

int main(void) {
  while (cin >> w >> h && w && h) {
    cin >> n;
    REP(i, n) {
      cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
      X1[i] = X1[i] * 2;
      X2[i] = X2[i] * 2;
      Y1[i] = Y1[i] * 2;
      Y2[i] = Y2[i] * 2;
    }

    REP(i, MAX_N * 6) { fill(fld[i], fld[i] + (MAX_N * 6), false); }

    w = compress(X1, X2, w);
    h = compress(Y1, Y2, h);

    REP(k, n) {
      for (int i = X1[k]; i <= X2[k]; i++) {
        for (int j = Y1[k]; j <= Y2[k]; j++) {
          fld[i][j] = true;
        }
      }
    }

    int res = 0;
    REP(i, w) {
      REP(j, h) {
        if (fld[i][j])
          continue;

        res++;
        dfs(i, j);
      }
    }

    cout << res << endl;
  }
}