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

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
template <typename T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
  return b < a ? (a = b, true) : false;
}
typedef long long ll;

int W, H, N;
const int MAX_N = 1000;
vector<int> VP, VQ, A, B, C, D;
int acc[MAX_N * 2 + 17][MAX_N * 2 + 17];
bool vis[MAX_N * 2 + 17][MAX_N * 2 + 17];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int getIdx(vector<int> &v, int val) {
  return int(lower_bound(v.begin(), v.end(), val) - v.begin());
}

void dfs(int x, int y) {
  vis[x][y] = true;
  FOR(i, 0, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if ((nx >= 0 && nx + 1 < VP.size() && ny >= 0 && ny + 1 < VQ.size()) ==
        false)
      continue;
    if (vis[nx][ny] || acc[nx][ny] > 0)
      continue;
    dfs(nx, ny);
  }
}

int main() {
  while (scanf("%d%d", &W, &H), W) {
    scanf("%d", &N);
    VP = {0, W};
    VQ = {0, H};
    memset(vis, 0, sizeof(vis));
    memset(acc, 0, sizeof(acc));
    A.assign(N, 0);
    B.assign(N, 0);
    C.assign(N, 0);
    D.assign(N, 0);
    FOR(i, 0, N) {
      scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
      VP.push_back(max(0, A[i] - 1));
      VP.push_back(A[i]);
      VP.push_back(max(0, C[i] - 1));
      VP.push_back(C[i]);
      VQ.push_back(max(0, B[i] - 1));
      VQ.push_back(B[i]);
      VQ.push_back(max(0, D[i] - 1));
      VQ.push_back(D[i]);
    }
    sort(VP.begin(), VP.end());
    sort(VQ.begin(), VQ.end());
    VP.erase(unique(VP.begin(), VP.end()), VP.end());
    VQ.erase(unique(VQ.begin(), VQ.end()), VQ.end());

    FOR(i, 0, N) {
      acc[getIdx(VP, A[i])][getIdx(VQ, B[i])]++;
      acc[getIdx(VP, C[i])][getIdx(VQ, B[i])]--;
      acc[getIdx(VP, A[i])][getIdx(VQ, D[i])]--;
      acc[getIdx(VP, C[i])][getIdx(VQ, D[i])]++;
    }
    FOR(i, 0, VP.size()) FOR(j, 0, VQ.size()) {
      if (i > 0)
        acc[i][j] += acc[i - 1][j];
      if (j > 0)
        acc[i][j] += acc[i][j - 1];
      if (i > 0 && j > 0)
        acc[i][j] -= acc[i - 1][j - 1];
    }

    int ans = 0;
    FOR(i, 0, int(VP.size()) - 1)
    FOR(j, 0, int(VQ.size()) - 1) if (vis[i][j] == false && acc[i][j] == 0) {
      dfs(i, j);
      ans++;
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
template <typename T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
  return b < a ? (a = b, true) : false;
}
typedef long long ll;

int W, H, N;
const int MAX_N = 1000;
vector<int> VP, VQ, A, B, C, D;
int acc[MAX_N * 4][MAX_N * 4];
bool vis[MAX_N * 4][MAX_N * 4];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int getIdx(vector<int> &v, int val) {
  return int(lower_bound(v.begin(), v.end(), val) - v.begin());
}

void dfs(int x, int y) {
  vis[x][y] = true;
  FOR(i, 0, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if ((nx >= 0 && nx + 1 < VP.size() && ny >= 0 && ny + 1 < VQ.size()) ==
        false)
      continue;
    if (vis[nx][ny] || acc[nx][ny] > 0)
      continue;
    dfs(nx, ny);
  }
}

int main() {
  while (scanf("%d%d", &W, &H), W) {
    scanf("%d", &N);
    VP = {0, W};
    VQ = {0, H};
    memset(vis, 0, sizeof(vis));
    memset(acc, 0, sizeof(acc));
    A.assign(N, 0);
    B.assign(N, 0);
    C.assign(N, 0);
    D.assign(N, 0);
    FOR(i, 0, N) {
      scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
      VP.push_back(max(0, A[i] - 1));
      VP.push_back(A[i]);
      VP.push_back(max(0, C[i] - 1));
      VP.push_back(C[i]);
      VQ.push_back(max(0, B[i] - 1));
      VQ.push_back(B[i]);
      VQ.push_back(max(0, D[i] - 1));
      VQ.push_back(D[i]);
    }
    sort(VP.begin(), VP.end());
    sort(VQ.begin(), VQ.end());
    VP.erase(unique(VP.begin(), VP.end()), VP.end());
    VQ.erase(unique(VQ.begin(), VQ.end()), VQ.end());

    FOR(i, 0, N) {
      acc[getIdx(VP, A[i])][getIdx(VQ, B[i])]++;
      acc[getIdx(VP, C[i])][getIdx(VQ, B[i])]--;
      acc[getIdx(VP, A[i])][getIdx(VQ, D[i])]--;
      acc[getIdx(VP, C[i])][getIdx(VQ, D[i])]++;
    }
    FOR(i, 0, VP.size()) FOR(j, 0, VQ.size()) {
      if (i > 0)
        acc[i][j] += acc[i - 1][j];
      if (j > 0)
        acc[i][j] += acc[i][j - 1];
      if (i > 0 && j > 0)
        acc[i][j] -= acc[i - 1][j - 1];
    }

    int ans = 0;
    FOR(i, 0, int(VP.size()) - 1)
    FOR(j, 0, int(VQ.size()) - 1) if (vis[i][j] == false && acc[i][j] == 0) {
      dfs(i, j);
      ans++;
    }
    printf("%d\\n", ans);
  }

  return 0;
}