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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int a[2010][2010];
int imos[2010][2010] = {};
vector<int> Zx, Zy;
int n, w, h;
int x[1002][2], y[1002][2];

int dfs(int s, int t) {
  if (a[s][t] != 0)
    return 0;
  a[s][t] = 1;
  rep(i, 4) { dfs(s + dir_4[i][0], t + dir_4[i][1]); }
  return 1;
}

void init() {
  rep(i, 2010) rep(j, 2010) a[i][j] = 0;
  rep(i, 2010) rep(j, 2010) imos[i][j] = 0;
  Zx.clear();
  Zy.clear();
}

int main() {
  while (1) {
    init();

    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d", &n);
    rep(i, n) {
      scanf("%d", &x[i][0]);
      scanf("%d", &y[i][0]);
      scanf("%d", &x[i][1]);
      scanf("%d", &y[i][1]);
    }

    Zx.pb(0);
    Zx.pb(w);
    Zy.pb(0);
    Zy.pb(h);
    rep(i, n) {
      Zx.pb(x[i][0]);
      Zx.pb(x[i][1]);
      Zy.pb(y[i][0]);
      Zy.pb(y[i][1]);
    }
    sor(Zx);
    uniq(Zx);
    sor(Zy);
    uniq(Zy);

    rep(i, n) {
      int x1 = lb(Zx, x[i][0]) - Zx.begin();
      int x2 = lb(Zx, x[i][1]) - Zx.begin();
      int y1 = lb(Zy, y[i][0]) - Zy.begin();
      int y2 = lb(Zy, y[i][1]) - Zy.begin();
      imos[x1][y1]++;
      imos[x1][y2]--;
      imos[x2][y1]--;
      imos[x2][y2]++;
    }

    rep1(i, 2009) rep(j, 2010) imos[i][j] += imos[i - 1][j];
    rep(i, 2010) rep1(j, 2009) imos[i][j] += imos[i][j - 1];

    rep(i, 2010) rep(j, 2010) a[i][j] = 1;
    rep1(i, Zx.size() - 1) {
      rep1(j, Zy.size() - 1) { a[i][j] = imos[i - 1][j - 1]; }
    }

    static int ret = 0;
    ret = 0;
    rep(i, 2010) {
      rep(j, 2010) { ret += dfs(i, j); }
    }

    printf("%d\\n", ret);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int a[2010][2010];
int imos[2010][2010];

P par[2010][2010];
int r[2010][2010];
int cnt;

P find(P x) {
  if (x == par[x.fr][x.sc])
    return x;
  return par[x.fr][x.sc] = find(par[x.fr][x.sc]);
}

bool same(P x, P y) { return find(x) == find(y); }

void unit(P x, P y) {
  if (same(x, y))
    return;
  x = find(x);
  y = find(y);
  if (r[x.fr][x.sc] > r[y.fr][y.sc]) {
    par[x.fr][x.sc] = y;
  } else {
    par[y.fr][y.sc] = x;
    if (r[x.fr][x.sc] == r[y.fr][y.sc]) {
      r[y.fr][y.sc]++;
    }
  }
  cnt--;
}

/*int dfs(int x,int y){
        if(a[x][y] != 0)return 0;
        a[x][y] = 1;
        rep(i,4){
                used[x+dir_4[i][0]][y+dir_4[i][1]] = true;
        }
        if(used[x][y])return 0;
        used[x][y] = true;
        return 1;
}*/

void init() {
  rep(i, 2010) rep(j, 2010) {
    a[i][j] = 0;
    imos[i][j] = 0;
    par[i][j] = P(i, j);
    r[i][j] = 0;
  }
  cnt = 0;
}

int main() {
  while (1) {
    init();

    static int n, w, h;
    static int x[1002][2], y[1002][2];

    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d", &n);
    rep(i, n) {
      scanf("%d", &x[i][0]);
      scanf("%d", &y[i][0]);
      scanf("%d", &x[i][1]);
      scanf("%d", &y[i][1]);
    }

    static vector<int> Zx, Zy;
    Zx.pb(0);
    Zx.pb(w);
    Zy.pb(0);
    Zy.pb(h);
    rep(i, n) {
      Zx.pb(x[i][0]);
      Zx.pb(x[i][1]);
      Zy.pb(y[i][0]);
      Zy.pb(y[i][1]);
    }
    sor(Zx);
    uniq(Zx);
    sor(Zy);
    uniq(Zy);

    rep(i, n) {
      int x1 = lb(Zx, x[i][0]) - Zx.begin();
      int x2 = lb(Zx, x[i][1]) - Zx.begin();
      int y1 = lb(Zy, y[i][0]) - Zy.begin();
      int y2 = lb(Zy, y[i][1]) - Zy.begin();
      imos[x1][y1]++;
      imos[x1][y2]--;
      imos[x2][y1]--;
      imos[x2][y2]++;
    }

    rep1(i, 2009) rep(j, 2010) imos[i][j] += imos[i - 1][j];
    rep(i, 2010) rep1(j, 2009) imos[i][j] += imos[i][j - 1];

    rep(i, 2010) rep(j, 2010) a[i][j] = 1;
    rep1(i, Zx.size() - 1) {
      rep1(j, Zy.size() - 1) { a[i][j] = imos[i - 1][j - 1]; }
    }

    rep(i, 2010) {
      rep(j, 2010) {
        // if(i%100 == 0 && j%100 == 0)printf("%d %d\\n",i,j);
        if (a[i][j] == 0)
          cnt++;
      }
    }

    rep1(i, 2009) rep(j, 2010) {
      // if(i%100 == 0 && j%100 == 0)printf("%d %d\\n",i,j);
      if (a[i][j] == 0 && a[i - 1][j] == 0)
        unit(P(i - 1, j), P(i, j));
    }
    rep(i, 2010) rep1(j, 2009) {
      // if(i%100 == 0 && j%100 == 0)printf("%d %d\\n",i,j);
      if (a[i][j] == 0 && a[i][j - 1] == 0)
        unit(P(i, j - 1), P(i, j));
    }

    /*static int ret = 0;
    rep(i,2010){
            rep(j,2010){
                    ret += dfs(i,j);
            }
    }*/

    printf("%d\\n", cnt);
  }
}