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

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int W, H, N;
int X1[5000];
int X2[5000];
int Y1[5000];
int Y2[5000];
int field[4001][4001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
bool FX[1000005];
bool FY[1000005];
int Num_X[5000];
int Num_Y[5000];
queue<pair<int, int>> que;
int main() {
  while (true) {
    for (int i = 0; i < 4001; i++)
      for (int j = 0; j < 4001; j++)
        field[i][j] = 0;
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      return 0;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
    for (int i = 0; i < N; i++) {
      X1[i]++;
      X2[i]++;
      Y1[i]++;
      Y2[i]++;
      if (X1[i] != 1)
        FX[X1[i] - 1] = true;
      FX[X1[i]] = true;
      if (X2[i] != W + 1)
        FX[X2[i]] = true;
      FX[X2[i] - 1] = true;
      FY[Y1[i]] = true;
      if (Y1[i] != 1)
        FY[Y1[i] - 1] = true;
      if (Y2[i] != H + 1)
        FY[Y2[i]] = true;
      FY[Y2[i] - 1] = true;
    }
    int cou1 = 0, cou2 = 0;
    for (int i = 0; i <= W + 1; i++) {
      if (FX[i]) {
        Num_X[cou1] = i;
        cou1++;
      }
    }
    for (int i = 0; i <= H + 1; i++) {
      if (FY[i]) {
        Num_Y[cou2] = i;
        cou2++;
      }
    }
    // for(int i=0;i<cou1;i++)printf("%d:%d\\n",i,Num_X[i]);
    // for(int i=0;i<cou2;i++)printf("%d:%d\\n",i,Num_Y[i]);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < cou1; j++) {
        if (Num_X[j] == X1[i])
          X1[i] = j;
        if (Num_X[j] == X2[i])
          X2[i] = j;
      }
      if (X2[i] >= cou1)
        X2[i] = cou1 + 1;
      for (int j = 0; j < cou2; j++) {
        if (Num_Y[j] == Y1[i])
          Y1[i] = j;
        if (Num_Y[j] == Y2[i])
          Y2[i] = j;
      }
      if (Y2[i] >= cou2)
        Y2[i] = cou2 + 1;
    }
    for (int i = 0; i < N; i++) {
      field[X1[i]][Y1[i]]++;
      field[X2[i]][Y2[i]]++;
      field[X1[i]][Y2[i]]--;
      field[X2[i]][Y1[i]]--;
      // printf("%d\\n",i);
    }
    for (int i = 1; i < cou1; i++) {
      for (int j = 0; j < cou2; j++) {
        field[i][j] += field[i - 1][j];
      }
    }
    for (int i = 0; i < cou1; i++) {
      for (int j = 1; j < cou2; j++) {
        field[i][j] += field[i][j - 1];
      }
    }

    /*for(int i=0;i<cou2;i++){
            for(int j=0;j<cou1;j++){
                    printf("%d ",Field[j][i]);
            }
            printf("\\n");
    }*/
    int ans = 0;
    for (int i = 0; i < cou1; i++) {
      for (int j = 0; j < cou2; j++) {
        que.empty();
        if (field[i][j] > 0)
          continue;
        ans++;
        que.push(make_pair(i, j));
        while (!que.empty()) {
          pair<int, int> P = que.front();
          que.pop();
          int pf = P.first;
          int ps = P.second;
          if (field[pf][ps] > 0)
            continue;
          field[pf][ps] = 1;
          for (int k = 0; k < 4; k++) {
            if (pf + dx[k] < 0)
              continue;
            if (pf + dx[k] >= cou1)
              continue;
            if (ps + dy[k] < 0)
              continue;
            if (ps + dy[k] >= cou2)
              continue;
            if (field[pf + dx[k]][ps + dy[k]] > 0)
              continue;
            que.push(make_pair(pf + dx[k], ps + dy[k]));
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int W, H, N;
int X1[5000];
int X2[5000];
int Y1[5000];
int Y2[5000];
int field[5001][5001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
bool FX[1000005];
bool FY[1000005];
int Num_X[5000];
int Num_Y[5000];
queue<pair<int, int>> que;
int main() {
  while (true) {
    for (int i = 0; i < 5001; i++)
      for (int j = 0; j < 5001; j++)
        field[i][j] = 0;
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      return 0;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
    for (int i = 0; i < N; i++) {
      X1[i]++;
      X2[i]++;
      Y1[i]++;
      Y2[i]++;
      if (X1[i] != 1)
        FX[X1[i] - 1] = true;
      FX[X1[i]] = true;
      if (X2[i] != W + 1)
        FX[X2[i]] = true;
      FX[X2[i] - 1] = true;
      FY[Y1[i]] = true;
      if (Y1[i] != 1)
        FY[Y1[i] - 1] = true;
      if (Y2[i] != H + 1)
        FY[Y2[i]] = true;
      FY[Y2[i] - 1] = true;
    }
    int cou1 = 0, cou2 = 0;
    for (int i = 0; i <= W + 1; i++) {
      if (FX[i]) {
        Num_X[cou1] = i;
        cou1++;
      }
    }
    for (int i = 0; i <= H + 1; i++) {
      if (FY[i]) {
        Num_Y[cou2] = i;
        cou2++;
      }
    }
    // for(int i=0;i<cou1;i++)printf("%d:%d\\n",i,Num_X[i]);
    // for(int i=0;i<cou2;i++)printf("%d:%d\\n",i,Num_Y[i]);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < cou1; j++) {
        if (Num_X[j] == X1[i])
          X1[i] = j;
        if (Num_X[j] == X2[i])
          X2[i] = j;
      }
      if (X2[i] >= cou1)
        X2[i] = cou1 + 1;
      for (int j = 0; j < cou2; j++) {
        if (Num_Y[j] == Y1[i])
          Y1[i] = j;
        if (Num_Y[j] == Y2[i])
          Y2[i] = j;
      }
      if (Y2[i] >= cou2)
        Y2[i] = cou2 + 1;
    }
    for (int i = 0; i < N; i++) {
      field[X1[i]][Y1[i]]++;
      field[X2[i]][Y2[i]]++;
      field[X1[i]][Y2[i]]--;
      field[X2[i]][Y1[i]]--;
      // printf("%d\\n",i);
    }
    for (int i = 1; i < cou1; i++) {
      for (int j = 0; j < cou2; j++) {
        field[i][j] += field[i - 1][j];
      }
    }
    for (int i = 0; i < cou1; i++) {
      for (int j = 1; j < cou2; j++) {
        field[i][j] += field[i][j - 1];
      }
    }

    /*for(int i=0;i<cou2;i++){
            for(int j=0;j<cou1;j++){
                    printf("%d ",Field[j][i]);
            }
            printf("\\n");
    }*/
    int ans = 0;
    for (int i = 0; i < cou1; i++) {
      for (int j = 0; j < cou2; j++) {
        que.empty();
        if (field[i][j] > 0)
          continue;
        ans++;
        que.push(make_pair(i, j));
        while (!que.empty()) {
          pair<int, int> P = que.front();
          que.pop();
          int pf = P.first;
          int ps = P.second;
          if (field[pf][ps] > 0)
            continue;
          field[pf][ps] = 1;
          for (int k = 0; k < 4; k++) {
            if (pf + dx[k] < 0)
              continue;
            if (pf + dx[k] >= cou1)
              continue;
            if (ps + dy[k] < 0)
              continue;
            if (ps + dy[k] >= cou2)
              continue;
            if (field[pf + dx[k]][ps + dy[k]] > 0)
              continue;
            que.push(make_pair(pf + dx[k], ps + dy[k]));
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}