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

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

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdbi;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1 << 30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

// #define int long long
#define addm(X, Y) ((X) = ((X) + (Y) % mod) % mod)

int w, h, n;
vector<int> x_1(1005), y_1(1005), x_2(1005), y_2(1005);
int g[2005][2005];

int compress(int n, int lim, vector<int> &v1, vector<int> &v2) {
  vector<int> sx;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      int cx1 = v1[i] + j;
      int cx2 = v2[i] + j;
      if (0 <= cx1 && cx1 < lim)
        sx.push_back(cx1);
      if (0 <= cx2 && cx2 < lim)
        sx.push_back(cx2);
    }
  }
  sort(sx.begin(), sx.end());
  sx.erase(unique(sx.begin(), sx.end()), sx.end());
  for (int i = 0; i < n; i++) {
    v1[i] = lower_bound(sx.begin(), sx.end(), v1[i]) - sx.begin();
    v2[i] = lower_bound(sx.begin(), sx.end(), v2[i]) - sx.begin();
  }
  return sx.size();
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    rep(i, 2005) rep(j, 2005) g[i][j] = 0;
    x_1.clear();
    x_2.clear();
    y_1.clear();
    y_2.clear();
    cin >> n;
    rep(i, n) {
      cin >> x_1[i] >> y_1[i] >> x_2[i] >> y_2[i];
      x_2[i]--;
      y_2[i]--;
    }
    w = compress(n, w, x_1, x_2);
    h = compress(n, h, y_1, y_2);
    /*
    rep(i, n) {
        for (int y = y_1[i]; y <= y_2[i]; y++) {
            for (int x = x_1[i]; x <= x_2[i]; x++) {
                g[y][x] = 1;
            }
        }
    }
    */

    rep(i, n) {
      g[y_1[i]][x_1[i]] += 1;
      g[y_1[i]][x_2[i] + 1] -= 1;
      g[y_2[i] + 1][x_1[i]] -= 1;
      g[y_2[i] + 1][x_2[i] + 1] += 1;
    }
    rep(i, h) rep(j, w) g[i][j + 1] += g[i][j];
    rep(j, w) rep(i, h) g[i + 1][j] += g[i][j];

    int ans = 0;
    rep(i, h) rep(j, w) {
      if (g[i][j] >= 1)
        continue;
      ans++;
      queue<pair<int, int>> q;
      q.push({i, j});
      g[i][j] = 1;
      while (!q.empty()) {
        pair<int, int> now = q.front();
        q.pop();
        rep(i, 4) {
          int ny = now.fi + dy4[i];
          int nx = now.se + dx4[i];
          if (!(0 <= ny && ny < h && 0 <= nx && nx < w) || g[ny][nx] >= 1)
            continue;
          q.push({ny, nx});
          g[ny][nx] = 1;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdbi;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1 << 30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

// #define int long long
#define addm(X, Y) ((X) = ((X) + (Y) % mod) % mod)

int w, h, n;
vector<int> x_1(1005), y_1(1005), x_2(1005), y_2(1005);
int g[6005][6005];

int compress(int n, int lim, vector<int> &v1, vector<int> &v2) {
  vector<int> sx;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      int cx1 = v1[i] + j;
      int cx2 = v2[i] + j;
      if (0 <= cx1 && cx1 < lim)
        sx.push_back(cx1);
      if (0 <= cx2 && cx2 < lim)
        sx.push_back(cx2);
    }
  }
  sort(sx.begin(), sx.end());
  sx.erase(unique(sx.begin(), sx.end()), sx.end());
  for (int i = 0; i < n; i++) {
    v1[i] = lower_bound(sx.begin(), sx.end(), v1[i]) - sx.begin();
    v2[i] = lower_bound(sx.begin(), sx.end(), v2[i]) - sx.begin();
  }
  return sx.size();
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    rep(i, 6005) rep(j, 6005) g[i][j] = 0;
    x_1.clear();
    x_2.clear();
    y_1.clear();
    y_2.clear();
    cin >> n;
    rep(i, n) {
      cin >> x_1[i] >> y_1[i] >> x_2[i] >> y_2[i];
      x_2[i]--;
      y_2[i]--;
    }
    w = compress(n, w, x_1, x_2);
    h = compress(n, h, y_1, y_2);
    /*
    rep(i, n) {
        for (int y = y_1[i]; y <= y_2[i]; y++) {
            for (int x = x_1[i]; x <= x_2[i]; x++) {
                g[y][x] = 1;
            }
        }
    }
    */

    rep(i, n) {
      g[y_1[i]][x_1[i]] += 1;
      g[y_1[i]][x_2[i] + 1] -= 1;
      g[y_2[i] + 1][x_1[i]] -= 1;
      g[y_2[i] + 1][x_2[i] + 1] += 1;
    }
    rep(i, h) rep(j, w) g[i][j + 1] += g[i][j];
    rep(j, w) rep(i, h) g[i + 1][j] += g[i][j];

    int ans = 0;
    rep(i, h) rep(j, w) {
      if (g[i][j] >= 1)
        continue;
      ans++;
      queue<pair<int, int>> q;
      q.push({i, j});
      g[i][j] = 1;
      while (!q.empty()) {
        pair<int, int> now = q.front();
        q.pop();
        rep(i, 4) {
          int ny = now.fi + dy4[i];
          int nx = now.se + dx4[i];
          if (!(0 <= ny && ny < h && 0 <= nx && nx < w) || g[ny][nx] >= 1)
            continue;
          q.push({ny, nx});
          g[ny][nx] = 1;
        }
      }
    }
    cout << ans << endl;
  }
}