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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

// ??\\???
int W, H, N;
const int MAX_N = 1000 + 1;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
// ???????????¶?????¨
bool fld[MAX_N * 6][MAX_N * 6];

// 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 (0 <= tx1 && tx1 < w)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 < w)
        xs.push_back(tx2);
    }
  }

  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  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();
}

// 4????????????, ???, ???, ??????
vector<int> dy = {0, -1, 0, 1};
vector<int> dx = {1, 0, -1, 0};

void solve() {

  cin >> N;
  FOR(i, 0, N) {
    cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
    X2[i]--;
    Y2[i]--;
  }

  // ??§?¨???§???
  W = compress(X1, X2, W);
  H = compress(Y1, Y2, H);

  // ??????????????¨?????????????????¶???
  memset(fld, 0, sizeof(fld));
  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;
      }
    }
  }

  // ???????????°??????
  int 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;
        }
      }
    }
  }

  std::cout << ans << std::endl;
}

int main(int argc, char *argv[]) {

  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0) {
      break;
    }
    solve();
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

class Compress {
public:
  vector<long long> v;
  map<long long, long long> rev;

  // ??§?¨???§?????????
  // vec????????¨????????±??????????????????????????????????????????????´???????????????????
  Compress(vector<long long> vec) {
    this->v = vec;
    sort(this->v.begin(), this->v.end());
    this->v.erase(unique(this->v.begin(), this->v.end()), this->v.end());

    for (long long x : v) {
      rev[this->zip(x)] = x;
    }
  }

  long long zip(long long x) {
    return lower_bound(ALL(this->v), x) - this->v.begin();
  }

  long long unzip(long long x) { return this->rev[x]; }
};

void show(vector<vector<int>> field) {
  for (int y = 0; y < field.size(); ++y) {
    for (int x = 0; x < field[y].size(); ++x) {
      // std::cout << (field[y][x] == 0 ? "o" : "x")  << " ";
      std::cout << (field[y][x]) << " ";
    }
    std::cout << "" << std::endl;
  }
  std::cout << "" << std::endl;
}

// 4????????????, ???, ???, ??????
vector<int> dy = {0, -1, 0, 1};
vector<int> dx = {1, 0, -1, 0};

bool inside(int y, int x, int H, int W) {
  return 0 <= y && y < H && 0 <= x && x < W;
}

void paint(int y, int x, int H, int W, vector<vector<int>> &field) {
  queue<pair<int, int>> que;
  que.push({y, x});

  while (!que.empty()) {
    int sy = que.front().first;
    int sx = que.front().second;
    que.pop();
    field[sy][sx] = 1;
    for (int i = 0; i < dy.size(); i++) {
      int ny = sy + dy[i];
      int nx = sx + dx[i];

      if (inside(ny, nx, field.size() - 1, field[0].size() - 1) &&
          field[ny][nx] == 0) {
        que.push({ny, nx});
      }
    }
  }
}

int solve(LL W, LL H) {
  LL N, X1, Y1, X2, Y2;
  cin >> N;
  vector<LL> allX, allY;
  vector<LL> x1(N), y1(N), x2(N), y2(N);
  FOR(i, 0, N) {
    cin >> X1 >> Y1 >> X2 >> Y2;
    x1[i] = X1;
    y1[i] = Y1;
    x2[i] = X2;
    y2[i] = Y2;

    FOR(d, 0, 2) {
      allX.push_back(X1 + d);
      allX.push_back(X2 + d);
      allY.push_back(Y1 + d);
      allY.push_back(Y2 + d);
    }
  }
  allY.push_back(0);
  allY.push_back(H);
  allX.push_back(0);
  allX.push_back(W);

  // ??§?¨???§???
  Compress cx(allX);
  Compress cy(allY);

  vector<vector<int>> field(cy.zip(H) + 1, vector<int>(cx.zip(W) + 1, 0));
  FOR(i, 0, N) {
    y1[i] = H - y1[i];
    y2[i] = H - y2[i];

    field[cy.zip(y2[i])][cx.zip(x1[i])]++; // ??????
    field[cy.zip(y1[i])][cx.zip(x2[i])]++; // ??????
    field[cy.zip(y2[i])][cx.zip(x2[i])]--; // ??????
    field[cy.zip(y1[i])][cx.zip(x1[i])]--; // ??????
  }

  // ?´???????
  FOR(x, 0, field[0].size()) {
    FOR(y, 1, field.size()) { field[y][x] += field[y - 1][x]; }
  }
  FOR(y, 0, field.size() - 1) {
    FOR(x, 1, field[0].size() - 1) { field[y][x] += field[y][x - 1]; }
  }

  LL ans = 0;
  FOR(y, 0, field.size() - 1) {
    FOR(x, 0, field[0].size() - 1) {
      if (field[y][x] == 0) {
        ans++;
        paint(y, x, H, W, field);
      }
    }
  }

  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  int W, H;
  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0) {
      break;
    }
    solve(W, H);
  }
}