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

#if 1
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

std::ifstream ifs("0531-input.txt");
#define in std::cin // ifs

int32_t W, H, n;

int32_t pos1x[1001];
int32_t pos1y[1001];
int32_t pos2x[1001];
int32_t pos2y[1001];
int16_t map[2010][2010]; // x,y

int32_t pr(int32_t *v1, int32_t *v2, int32_t N) {
  static int32_t memo[2002];
  auto iter = memo;
  for (int32_t i = 0; i < N; ++i) {
    *iter++ = v1[i];
    *iter++ = v2[i];
  }
  std::sort(memo, iter);
  iter = std::unique(memo, iter);
  for (int32_t i = 0; i < N; ++i) {
    v1[i] = std::lower_bound(memo, iter, v1[i]) - memo;
    v2[i] = std::lower_bound(memo, iter, v2[i]) - memo;
  }
  return iter - memo;
}

int main() {
  in.sync_with_stdio(false);
  std::cout.sync_with_stdio(false);

  in >> W >> H;

  while (W != 0 && H != 0) {
    for (auto &ar : map)
      for (auto &i : ar)
        i = 0;

    in >> n;
    for (int i = 0; i < n; ++i) {
      in >> pos1x[i] >> pos1y[i] >> pos2x[i] >> pos2y[i];
    }
    pos1x[n] = pos1y[n] = 0;
    pos2x[n] = W;
    pos2y[n] = H;

#if 0
		std::cout << "------------------------------------\\n";
		for (int i = 0; i < n; ++i) {
			std::cout << pos1x[i] << ' ' << pos1y[i] << ' ' << pos2x[i] << ' ' << pos2y[i] << '\\n';
		}
#endif

    W = pr(pos1x, pos2x, n + 1) - 1;
    H = pr(pos1y, pos2y, n + 1) - 1;

#if 0
		std::cout << "------------------------------------\\n";
		for (int i = 0; i < n; ++i) {
			std::cout << pos1x[i] << ' ' << pos1y[i] << ' ' << pos2x[i] << ' ' << pos2y[i] << '\\n';
		}
#endif

    //????????????????????£??????????????¶???
    for (int i = 0; i < n; ++i) {
      ++map[pos1x[i]][pos1y[i]];
      --map[pos1x[i]][pos2y[i]];
      --map[pos2x[i]][pos1y[i]];
      ++map[pos2x[i]][pos2y[i]];
    }
    //?¨??????????
    for (int x = 1; x < W; ++x) {
      for (int y = 0; y < H; ++y) {
        map[x][y] += map[x - 1][y];
      }
    }
    //???
    for (int x = 0; x < W; ++x) {
      for (int y = 1; y < H; ++y) {
        map[x][y] += map[x][y - 1];
      }
    }

#if 0
		std::cout << "------------------------------------\\n";
		for (signed y = H - 1; y >= 0; --y) {
			for (int x = 0; x < W; ++x) {
				std::cout << map[x][y];
			}
			std::cout << '\\n';
		}
#endif

    //???????????¢???
    constexpr int32_t diff[4][2] = {
        {1, 0},
        {-1, 0},
        {0, 1},
        {0, -1},
    };
    int32_t res = 0;
    std::queue<uint32_t> que;
    for (int x = 0; x < W; ++x) {
      for (int y = 0; y < H; ++y) {
        if (0 < map[x][y]) {
          continue;
        }
        ++res;
        map[x][y] = 1;
        que.push((x << 16) | y);
        while (!que.empty()) {
          auto raw = que.front();
          que.pop();
          int32_t X_ = raw >> 16;
          int32_t Y_ = raw & 0xFFFF;
          for (auto &d : diff) {
            auto nx = X_ + d[0], ny = Y_ + d[1];
            if ([](int32_t v) { return 0 <= v && v < W; }(nx) &&
                [](int32_t v) { return 0 <= v && v < H; }(ny) &&
                (map[nx][ny] <= 0)) {
              map[nx][ny] = 1;
              que.push(((uint32_t)(nx) << 16) | (uint32_t)(ny));
            }
          }
        }

#if 0
				std::cout << "------------------------------------\\n";
				for (signed y = H - 1; y >= 0; --y) {
					for (int x = 0; x < W; ++x) {
						std::cout << map[x][y];
					}
					std::cout << '\\n';
				}
#endif
      }
    } // for
    std::cout << res << std::endl;

#if 0
		std::cout << "------------------------------------\\n";
#endif

    in >> W >> H;
  }
}
#endif

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

int32_t W, H, n;

int32_t pos1x[1000];
int32_t pos1y[1000];
int32_t pos2x[1000];
int32_t pos2y[1000];
int16_t map[6000][6000]; // x,y

template <typename F>
int32_t pr(int32_t *v1, int32_t *v2, int32_t N, F &&is_right) {
  static int32_t memo[6000];
  auto iter = memo;
  for (int32_t i = 0; i < N; ++i) {
    for (int_fast8_t d = -1; d <= 1; ++d) {
      if (is_right(v1[i] + d))
        *iter++ = v1[i] + d;
      if (is_right(v2[i] + d))
        *iter++ = v2[i] + d;
    }
  }
  std::sort(memo, iter);
  iter = std::unique(memo, iter);
  for (int32_t i = 0; i < N; ++i) {
    v1[i] = std::lower_bound(memo, iter, v1[i]) - memo;
    v2[i] = std::lower_bound(memo, iter, v2[i]) - memo;
  }
  return iter - memo;
}

int main() {
  std::cin.sync_with_stdio(false);
  std::cout.sync_with_stdio(false);

  std::cin >> W >> H;

  while (W != 0 && H != 0) {
    for (auto &ar : map)
      for (auto &i : ar)
        i = 0;

    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> pos1x[i] >> pos1y[i] >> pos2x[i] >> pos2y[i];
    }

    W = pr(pos1x, pos2x, n, [](int32_t v) { return 0 <= v && v < W; });
    H = pr(pos1y, pos2y, n, [](int32_t v) { return 0 <= v && v < H; });

#if 0
		std::cout <<  "------------------------------------\\n";
		for(int i = 0;i < n; ++i){
			std::cout << pos1x[i] <<' ' << pos1y[i]  <<' '<< pos2x[i] <<' ' << pos2y[i] <<'\\n';
		}
#endif

    // 累積和を使って塗りつぶし
    for (int i = 0; i < n; ++i) {
      for (int x = pos1x[i]; x < pos2x[i]; ++x) {
        ++map[x][pos1y[i]];
        --map[x][pos2y[i]];
      }
    }
    for (int x = 0; x < W; ++x) {
      int32_t now = 0;
      for (int y = 0; y < H; ++y) {
        now += map[x][y];
        map[x][y] = now;
      }
    }

#if 0
		std::cout <<  "------------------------------------\\n";
		for(signed y = H-1;y >= 0; --y){
			for(int x = 0;x < W; ++x){
				std::cout << map[x][y];
			}
			std::cout << '\\n';
		}
#endif

    // 幅優先探査
    constexpr int32_t diff[4][2] = {
        {1, 0},
        {-1, 0},
        {0, 1},
        {0, -1},
    };
    int32_t res = 0;
    std::queue<int32_t> que;
    for (int x = 0; x < W; ++x) {
      for (int y = 0; y < H; ++y) {
        if (0 < map[x][y]) {
          continue;
        }
        ++res;
        map[x][y] = 1;
        que.push((x << 16) | y);
        while (!que.empty()) {
          auto raw = que.front();
          que.pop();
          int32_t X_ = raw >> 16;
          int32_t Y_ = raw & 0xFFFF;
          for (auto &d : diff) {
            auto nx = X_ + d[0], ny = Y_ + d[1];
            if ([](int32_t v) { return 0 <= v && v < W; }(nx) &&
                [](int32_t v) { return 0 <= v && v < H; }(ny) &&
                (map[nx][ny] <= 0)) {
              map[nx][ny] = 1;
              que.push((nx << 16) | ny);
            }
          }
        }

#if 0
		std::cout <<  "------------------------------------\\n";
		for(signed y = H-1;y >= 0; --y){
			for(int x = 0;x < W; ++x){
				std::cout << map[x][y];
			}
			std::cout << '\\n';
		}
#endif
      }
    } // for
    std::cout << res << std::endl;

#if 0
		std::cout <<  "------------------------------------\\n";
#endif

    std::cin >> W >> H;
  }
}