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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

int a[1111], b[1111], c[1111], d[1111];
short g[4444][4444];
int w, h, n;
vector<int> xs, ys;

int main(void) {
  for (;;) {
    scanf("%d%d", &w, &h);
    if (w + h == 0)
      break;
    xs.clear();
    ys.clear();
    memset(g, 0, sizeof(g));
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", a + i, b + i, c + i, d + i);
      a[i] *= 2;
      b[i] *= 2;
      c[i] *= 2;
      d[i] *= 2;
      xs.push_back(a[i]);
      xs.push_back(c[i]);
      xs.push_back(a[i] + 1);
      xs.push_back(c[i] + 1);
      ys.push_back(b[i]);
      ys.push_back(d[i]);
      ys.push_back(b[i] + 1);
      ys.push_back(d[i] + 1);
    }

    xs.push_back(w * 2);
    ys.push_back(h * 2);
    xs.push_back(0);
    ys.push_back(0);
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    sort(ys.begin(), ys.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());

    for (int i = 0; i < n; i++) {
      int x1 = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
      int y1 = lower_bound(ys.begin(), ys.end(), b[i]) - ys.begin();
      int x2 = lower_bound(xs.begin(), xs.end(), c[i]) - xs.begin();
      int y2 = lower_bound(ys.begin(), ys.end(), d[i]) - ys.begin();

      g[y1][x1] += 1;
      g[y1][x2 + 1] += -1;
      g[y2 + 1][x1] += -1;
      g[y2 + 1][x2 + 1] += 1;
    }

    for (int i = 0; i < int(ys.size()) - 1; i++) {
      for (int j = 0; j < int(xs.size()) - 1; j++) {
        g[i][j + 1] += g[i][j];
      }
    }
    for (int i = 0; i < int(xs.size()) - 1; i++) {
      for (int j = 0; j < int(ys.size()) - 1; j++) {
        g[j + 1][i] += g[j][i];
      }
    }

    int res = 0;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    for (int i = 0; i < int(ys.size()) - 1; i++) {
      for (int j = 0; j < int(xs.size()) - 1; j++) {
        if (g[i][j] == 0) {
          res += 1;
          queue<int> q;
          q.push(j * 10000 + i);
          while (!q.empty()) {
            int x = q.front() / 10000;
            int y = q.front() % 10000;
            q.pop();
            for (int i = 0; i < 4; i++) {
              int nx = x + dx[i], ny = y + dy[i];
              if (nx < 0 || nx >= int(xs.size()) - 1 || ny < 0 ||
                  ny >= int(ys.size()) - 1 || g[ny][nx])
                continue;
              g[ny][nx] = 1;
              q.push(nx * 10000 + ny);
            }
          }
        }
      }
    }

    printf("%d\\n", res);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

int a[1111], b[1111], c[1111], d[1111];
int g[5555][5555];
int w, h, n;
vector<int> xs, ys;

int main(void) {
  for (;;) {
    scanf("%d%d", &w, &h);
    if (w + h == 0)
      break;
    xs.clear();
    ys.clear();
    memset(g, 0, sizeof(g));
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", a + i, b + i, c + i, d + i);
      a[i] *= 2;
      b[i] *= 2;
      c[i] *= 2;
      d[i] *= 2;
      xs.push_back(a[i]);
      xs.push_back(c[i]);
      xs.push_back(a[i] + 1);
      xs.push_back(c[i] + 1);
      ys.push_back(b[i]);
      ys.push_back(d[i]);
      ys.push_back(b[i] + 1);
      ys.push_back(d[i] + 1);
    }

    xs.push_back(w * 2);
    ys.push_back(h * 2);
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    sort(ys.begin(), ys.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());

    for (int i = 0; i < n; i++) {
      int x1 = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
      int y1 = lower_bound(ys.begin(), ys.end(), b[i]) - ys.begin();
      int x2 = lower_bound(xs.begin(), xs.end(), c[i]) - xs.begin();
      int y2 = lower_bound(ys.begin(), ys.end(), d[i]) - ys.begin();

      g[y1][x1] += 1;
      g[y1][x2 + 1] += -1;
      g[y2 + 1][x1] += -1;
      g[y2 + 1][x2 + 1] += 1;
    }

    for (int i = 0; i < int(ys.size()) - 1; i++) {
      for (int j = 0; j < int(xs.size()) - 1; j++) {
        g[i][j + 1] += g[i][j];
      }
    }
    for (int i = 0; i < int(xs.size()) - 1; i++) {
      for (int j = 0; j < int(ys.size()) - 1; j++) {
        g[j + 1][i] += g[j][i];
      }
    }

    int res = 0;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    for (int i = 0; i < int(ys.size()) - 1; i++) {
      for (int j = 0; j < int(xs.size()) - 1; j++) {
        if (g[i][j] == 0) {
          res += 1;
          queue<int> q;
          q.push(j * 10000 + i);
          while (!q.empty()) {
            int x = q.front() / 10000;
            int y = q.front() % 10000;
            q.pop();
            for (int i = 0; i < 4; i++) {
              int nx = x + dx[i], ny = y + dy[i];
              if (nx < 0 || nx >= int(xs.size()) - 1 || ny < 0 ||
                  ny >= int(ys.size()) - 1 || g[ny][nx])
                continue;
              g[ny][nx] = 1;
              q.push(nx * 10000 + ny);
            }
          }
        }
      }
    }

    printf("%d\\n", res);
  }
  return 0;
}