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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0531

//
//  main.cpp
//

#include <algorithm>
#include <iostream>
#include <vector>
// #include <limits>
#include <math.h>
#include <queue>
// #include <tuple>
// #include <stdio.h>
// #include <stdlib.h>
// #include <string>
// #include "string.h"
//  #include <unordered_map>
#include <map>
#include <unordered_set>
// #include <array>
#include <limits>
#include <set>

using namespace std;

using ll = long long;

int main() {
  /*
15 6
10
1 4 5 6
2 1 4 5
1 0 5 1
6 1 7 5
7 5 9 6
7 0 9 2
9 1 10 5
11 0 14 1
12 1 13 5
11 5 14 6
0 0
   */

  while (true) {
    int W, H;
    scanf("%d %d", &W, &H);
    if (W == 0 && H == 0) {
      return 0;
    }
    int N;
    scanf("%d", &N);
    struct Tape {
      int x1;
      int y1;
      int x2;
      int y2;
      Tape(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
    };
    vector<Tape> tapes;
    tapes.reserve(N);
    set<int> xs;
    set<int> ys;
    xs.insert(0);
    xs.insert(W);
    ys.insert(0);
    ys.insert(H);
    for (int i = 0; i < N; i++) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      tapes.emplace_back(x1, y1, x2, y2);
      xs.insert(x1);
      xs.insert(x2);
      ys.insert(y1);
      ys.insert(y2);
    }
    W = (int)xs.size() - 1;
    H = (int)ys.size() - 1;
    for (auto tape = tapes.begin(); tape != tapes.end(); ++tape) {
      tape->x1 = (int)distance(xs.begin(), xs.find(tape->x1));
      tape->x2 = (int)distance(xs.begin(), xs.find(tape->x2));
      tape->y1 = (int)distance(ys.begin(), ys.find(tape->y1));
      tape->y2 = (int)distance(ys.begin(), ys.find(tape->y2));
    }
    vector<bool> board(W * H, false);
    for (Tape &tape : tapes) {
      for (int x = tape.x1; x < tape.x2; x++) {
        for (int y = tape.y1; y < tape.y2; y++) {
          board[x + y * W] = true;
        }
      }
    }
    vector<pair<int, int>> moves;
    moves.push_back(make_pair(-1, 0));
    moves.push_back(make_pair(1, 0));
    moves.push_back(make_pair(0, -1));
    moves.push_back(make_pair(0, 1));
    int count = 0;
    for (int x = 0; x < W; x++) {
      for (int y = 0; y < H; y++) {
        if (!board[x + W * y]) {
          count++;
          queue<pair<int, int>> wfs;
          wfs.push(make_pair(x, y));
          board[x + W * y] = true;
          while (!wfs.empty()) {
            pair<int, int> xy = wfs.front();
            board[xy.first + W * xy.second] = true;
            wfs.pop();
            for (pair<int, int> vec : moves) {
              pair<int, int> moved =
                  make_pair(xy.first + vec.first, xy.second + vec.second);
              if (moved.first < 0 || moved.first >= W || moved.second < 0 ||
                  moved.second >= H) {
                continue;
              }
              if (!board[moved.first + W * moved.second]) {
                wfs.push(moved);
                board[moved.first + W * moved.second] = true;
              }
            }
          }
        }
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}


Error: MLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0531

//
//  main.cpp
//

#include <algorithm>
#include <iostream>
#include <vector>
// #include <limits>
#include <math.h>
#include <queue>
// #include <tuple>
// #include <stdio.h>
// #include <stdlib.h>
// #include <string>
// #include "string.h"
//  #include <unordered_map>
#include <map>
#include <unordered_set>
// #include <array>
#include <limits>
#include <set>

using namespace std;

using ll = long long;

int main() {
  /*
15 6
10
1 4 5 6
2 1 4 5
1 0 5 1
6 1 7 5
7 5 9 6
7 0 9 2
9 1 10 5
11 0 14 1
12 1 13 5
11 5 14 6
0 0
   */

  while (true) {
    int W, H;
    scanf("%d %d", &W, &H);
    if (W == 0 && H == 0) {
      return 0;
    }
    int N;
    scanf("%d", &N);
    struct Tape {
      int x1;
      int y1;
      int x2;
      int y2;
      Tape(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
    };
    vector<Tape> tapes;
    tapes.reserve(N);
    set<int> xs;
    set<int> ys;
    xs.insert(0);
    xs.insert(W);
    ys.insert(0);
    ys.insert(H);
    for (int i = 0; i < N; i++) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      tapes.emplace_back(x1, y1, x2, y2);
      xs.insert(x1);
      xs.insert(x2);
      ys.insert(y1);
      ys.insert(y2);
    }
    W = (int)xs.size() - 1;
    H = (int)ys.size() - 1;
    for (auto tape = tapes.begin(); tape != tapes.end(); ++tape) {
      tape->x1 = (int)distance(xs.begin(), xs.find(tape->x1));
      tape->x2 = (int)distance(xs.begin(), xs.find(tape->x2));
      tape->y1 = (int)distance(ys.begin(), ys.find(tape->y1));
      tape->y2 = (int)distance(ys.begin(), ys.find(tape->y2));
    }
    vector<bool> board(W * H, false);
    for (Tape &tape : tapes) {
      for (int x = tape.x1; x < tape.x2; x++) {
        for (int y = tape.y1; y < tape.y2; y++) {
          board[x + y * W] = true;
        }
      }
    }
    vector<pair<int, int>> moves;
    moves.push_back(make_pair(-1, 0));
    moves.push_back(make_pair(1, 0));
    moves.push_back(make_pair(0, -1));
    moves.push_back(make_pair(0, 1));
    int count = 0;
    for (int x = 0; x < W; x++) {
      for (int y = 0; y < H; y++) {
        if (!board[x + W * y]) {
          count++;
          queue<pair<int, int>> wfs;
          wfs.push(make_pair(x, y));
          while (!wfs.empty()) {
            pair<int, int> xy = wfs.front();
            board[xy.first + W * xy.second] = true;
            wfs.pop();
            for (pair<int, int> vec : moves) {
              pair<int, int> moved =
                  make_pair(xy.first + vec.first, xy.second + vec.second);
              if (moved.first < 0 || moved.first >= W || moved.second < 0 ||
                  moved.second >= H) {
                continue;
              }
              if (!board[moved.first + W * moved.second]) {
                wfs.push(moved);
              }
            }
          }
        }
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}