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

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define N 2048

using namespace std;

struct rectangle {
  int x1, y1;
  int x2, y2;
  rectangle() : x1(0), y1(0), x2(0), y2(0) {}
  rectangle(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {}
};

typedef pair<int, int> pi;
typedef map<int, int> CCS; // compressed coordinate system

void CompressCCS(CCS &ccs) {
  int cnt = 0;
  for (CCS::iterator itCC = ccs.begin(); itCC != ccs.end(); ++itCC) {
    itCC->second = cnt;
    ++cnt;
  }
  return;
}

bool isinside(pi p, int h, int w) {
  return 0 <= p.first && p.first < h - 1 && 0 <= p.second && p.second < w - 1;
}

void BFS(int st, int ed, int h, int w, int tapes[][N]) {
  queue<pi> qp;
  static const int di[] = {-1, 0, 1, 0};
  static const int dj[] = {0, 1, 0, -1};

  qp.push(pi(st, ed));
  tapes[st][ed] = -1;
  while (!qp.empty()) {
    pi now = qp.front();

    qp.pop();

    for (int k = 0; k < sizeof(di) / sizeof(*di); ++k) {
      pi next(now.first + di[k], now.second + dj[k]);
      if (isinside(next, h, w)) {
        if (tapes[next.first][next.second] == 0) {
          tapes[next.first][next.second] = -1;
          qp.push(next);
        }
      }
    }
  }
}

int main() {
  while (true) {
    int ans = 0;
    static int tapes[N][N];
    CCS CCx; // compressed coordinate system
    CCS CCy;
    vector<rectangle> vrect;

    int W, H;
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      break;
    CCx[0] = CCx[W] = 0;
    CCy[0] = CCy[H] = 0;

    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      CCx[a] = CCx[c] = 0;
      CCy[b] = CCy[d] = 0;
      vrect.push_back(rectangle(a, b, c, d));
    }

    CompressCCS(CCx);
    CompressCCS(CCy);

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        tapes[i][j] = 0;
      }
    }

    for (int i = 0; i < vrect.size(); ++i) {
      CCS::iterator xbeg = CCx.find(vrect[i].x1);
      CCS::iterator xend = CCx.find(vrect[i].x2);
      for (CCS::iterator itCCx = xbeg; itCCx != xend; ++itCCx) {
        CCS::iterator ybeg = CCy.find(vrect[i].y1);
        CCS::iterator yend = CCy.find(vrect[i].y2);
        for (CCS::iterator itCCy = ybeg; itCCy != yend; ++itCCy) {
          tapes[itCCy->second][itCCx->second]++;
        }
      }
    }

    /*
    for(int i = 0; i < CCy.size(); ++i){
      for(int j = 0; j < CCx.size(); ++j){
        cout << tapes[i][j] << ' ';
      }
      cout << endl;
    }
    cout << endl;
    */

    for (int i = 0; i < CCy.size(); ++i) {
      for (int j = 0; j < CCx.size(); ++j) {
        if (tapes[i][j] == 0 && isinside(pi(i, j), CCy.size(), CCx.size())) {
          BFS(i, j, CCy.size(), CCx.size(), tapes);
          ++ans;
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define N 2048

using namespace std;

struct rectangle {
  int x1, y1;
  int x2, y2;
  rectangle() : x1(0), y1(0), x2(0), y2(0) {}
  rectangle(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {}
};

typedef pair<int, int> pi;
typedef map<int, int> CCS; // compressed coordinate system

void CompressCCS(CCS &ccs) {
  int cnt = 0;
  for (CCS::iterator itCC = ccs.begin(); itCC != ccs.end(); ++itCC) {
    itCC->second = cnt;
    ++cnt;
  }
  return;
}

bool isinside(pi p, int h, int w) {
  return 0 <= p.first && p.first < h - 1 && 0 <= p.second && p.second < w - 1;
}

void BFS(int st, int ed, int h, int w, int tapes[][N]) {
  queue<pi> qp;
  static const int di[] = {-1, 0, 1, 0};
  static const int dj[] = {0, 1, 0, -1};

  qp.push(pi(st, ed));
  while (!qp.empty()) {
    pi now = qp.front();
    tapes[now.first][now.second] = -1;
    qp.pop();

    for (int k = 0; k < sizeof(di) / sizeof(*di); ++k) {
      pi next(now.first + di[k], now.second + dj[k]);
      if (isinside(next, h, w)) {
        if (tapes[next.first][next.second] == 0) {
          qp.push(next);
        }
      }
    }
  }
}

int main() {
  while (true) {
    int ans = 0;
    static int tapes[N][N];
    CCS CCx; // compressed coordinate system
    CCS CCy;
    vector<rectangle> vrect;

    int W, H;
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      break;
    CCx[0] = CCx[W] = 0;
    CCy[0] = CCy[H] = 0;

    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      CCx[a] = CCx[c] = 0;
      CCy[b] = CCy[d] = 0;
      vrect.push_back(rectangle(a, b, c, d));
    }

    CompressCCS(CCx);
    CompressCCS(CCy);

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        tapes[i][j] = 0;
      }
    }

    for (int i = 0; i < vrect.size(); ++i) {
      CCS::iterator xbeg = CCx.find(vrect[i].x1);
      CCS::iterator xend = CCx.find(vrect[i].x2);
      for (CCS::iterator itCCx = xbeg; itCCx != xend; ++itCCx) {
        CCS::iterator ybeg = CCy.find(vrect[i].y1);
        CCS::iterator yend = CCy.find(vrect[i].y2);
        for (CCS::iterator itCCy = ybeg; itCCy != yend; ++itCCy) {
          tapes[itCCy->second][itCCx->second]++;
        }
      }
    }

    /*
    for(int i = 0; i < CCy.size(); ++i){
      for(int j = 0; j < CCx.size(); ++j){
        cout << tapes[i][j] << ' ';
      }
      cout << endl;
    }
    cout << endl;
    */

    for (int i = 0; i < CCy.size(); ++i) {
      for (int j = 0; j < CCx.size(); ++j) {
        if (tapes[i][j] == 0 && isinside(pi(i, j), CCy.size(), CCx.size())) {
          BFS(i, j, CCy.size(), CCx.size(), tapes);
          ++ans;
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}