Problem p00454 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int Arr[4][1001];
int ColPart[1000001] = {}, RowPart[1000001] = {};
bool Area[2010][2010] = {};
int Row = 0, Col = 0;
int W, H;
int n;

typedef struct {
  int x, y;
} NODE;
queue<NODE> q;
NODE mknd(int x, int y) {
  NODE T;
  T.x = x;
  T.y = y;
  return T;
}
void Floodfill(int x, int y) {
  q.push(mknd(x, y));
  while (!q.empty()) {
    int X = q.front().x;
    int Y = q.front().y;
    q.pop();
    if (X + 1 < Row && Area[X + 1][Y] == 0) {
      Area[X + 1][Y] = 1;
      q.push(mknd(X + 1, Y));
    }
    if (X - 1 >= 0 && Area[X - 1][Y] == 0) {
      Area[X - 1][Y] = 1;
      q.push(mknd(X - 1, Y));
    }
    if (Y + 1 < Col && Area[X][Y + 1] == 0) {
      Area[X][Y + 1] = 1;
      q.push(mknd(X, Y + 1));
    }
    if (Y - 1 >= 0 && Area[X][Y - 1] == 0) {
      Area[X][Y - 1] = 1;
      q.push(mknd(X, Y - 1));
    }
  }
  return;
}
int main() {
  do {
    Row = Col = 0;
    for (int i = 0; i < 1000001; i++) {
      RowPart[i] = -1;
      ColPart[i] = -1;
    }
    for (int i = 0; i < 2010; i++) {
      for (int j = 0; j < 2010; j++) {
        Area[i][j] = false;
      }
    }
    for (int i = 0; i < 1001; i++) {
      Arr[0][i] = 0;
      Arr[1][i] = 0;
      Arr[2][i] = 0;
      Arr[3][i] = 0;
    }
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      break;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d%d", &Arr[0][i], &Arr[1][i], &Arr[2][i], &Arr[3][i]);
      ColPart[Arr[0][i]] = 0;
      ColPart[Arr[2][i]] = 0;
      RowPart[Arr[1][i]] = 0;
      RowPart[Arr[3][i]] = 0;
    }
    Arr[0][0] = 0;
    Arr[1][0] = 0;
    Arr[2][0] = W;
    Arr[3][0] = H;
    ColPart[Arr[0][0]] = 0;
    ColPart[Arr[2][0]] = 0;
    RowPart[Arr[1][0]] = 0;
    RowPart[Arr[3][0]] = 0;
    int RS = 0, CS = 0;
    for (int i = 0; i < 1000001; i++) {
      if (!RowPart[i]) {
        RowPart[i] = Row;
        Row++;
      }
      if (!ColPart[i]) {
        ColPart[i] = Col;
        Col++;
      }
    }
    for (int i = 1; i <= n; i++) {
      int x1 = Arr[0][i];
      int y1 = Arr[1][i];
      int x2 = Arr[2][i];
      int y2 = Arr[3][i];
      for (int j = RowPart[y1]; j < RowPart[y2]; j++) {
        for (int k = ColPart[x1]; k < ColPart[x2]; k++) {
          Area[j][k] = 1;
        }
      }
    }
    int Count = 0;
    Row = Row - 1;
    Col = Col - 1;
    for (int i = 0; i < Row; i++) {
      for (int j = 0; j < Col; j++) {
        if (Area[i][j] == 0) {
          Floodfill(i, j);
          Count++;
        }
      }
    }
    printf("%d\\n", Count);
  } while (true);
}

Error: WA

Fixed submission:

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

using namespace std;

typedef struct node {
  int x1, y1;
  int x2, y2;
} NODE;

typedef struct n {
  int i, j;
} N;

bool comp(const int &a, const int &b) { return a < b; }

typedef struct info {
  int i, val;
} INFO;

int w, h, n, tmp1, tmp2, tmp3, tmp4;
vector<int> row;
vector<int> col;
NODE tape[1001] = {};
vector<INFO> RROW;
vector<INFO> RCOL;
bool field[2000][2000] = {};
int lenRow;
int lenCol;

queue<N> q;

void BFS(int i, int j) {
  N u, now;
  u.i = i;
  u.j = j;
  q.push(u);
  field[i][j] = true;
  while (!q.empty()) {
    now = q.front();
    q.pop();
    if (!field[now.i + 1][now.j] && now.i + 1 < lenRow) {
      u.i = now.i + 1;
      u.j = now.j;
      field[u.i][u.j] = 1;
      q.push(u);
    }
    if (!field[now.i - 1][now.j] && now.i - 1 >= 0) {
      u.i = now.i - 1;
      u.j = now.j;
      field[u.i][u.j] = 1;
      q.push(u);
    }
    if (!field[now.i][now.j + 1] && now.j + 1 < lenCol) {
      u.i = now.i;
      u.j = now.j + 1;
      field[u.i][u.j] = 1;
      q.push(u);
    }
    if (!field[now.i][now.j - 1] && now.j - 1 >= 0) {
      u.i = now.i;
      u.j = now.j - 1;
      field[u.i][u.j] = 1;
      q.push(u);
    }
  }
}

int main() {
  do {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &tmp1, &tmp2, &tmp3, &tmp4);
      tape[i].x1 = tmp1, tape[i].x2 = tmp3, tape[i].y1 = tmp2,
      tape[i].y2 = tmp4;
      col.push_back(tmp1);
      col.push_back(tmp3);
      row.push_back(tmp2);
      row.push_back(tmp4);
      /*
       for (int i=0; i<=w; i++) {
       for (int j=1; j<=h; j++) {
       if(field[i][j] == 1)printf("1 ");
       else printf("0 ");
       }
       printf("\\n");
       }
       printf("\\n");
       */
    }
    sort(row.begin(), row.end(), comp);
    sort(col.begin(), col.end(), comp);

    lenRow = row.size();
    lenCol = col.size();
    int p = 1;
    for (int i = 0; i < lenRow; i++) {
      INFO u;
      u.i = p;
      u.val = row[i];
      if (RROW.empty())
        RROW.push_back(u);
      else if (RROW.back().val == row[i]) {
        continue;
      } else
        RROW.push_back(u);
      p++;
    }
    // printf("\\n");
    p = 1;
    for (int i = 0; i < lenCol; i++) {
      INFO u;
      u.i = p;
      u.val = col[i];
      if (RCOL.empty())
        RCOL.push_back(u);
      else if (RCOL.back().val == col[i]) {
        continue;
      } else
        RCOL.push_back(u);
      p++;
    }
    // printf("\\n");
    lenRow = RROW.size();
    lenCol = RCOL.size();
    /*
    printf("\\nRRR:");
    for (int i=0; i<lenRow; i++) {
        printf("%d ",RROW[i].val);
    }
    printf("\\nCCC:");
    for (int i=0; i<lenCol; i++) {
        printf("%d ",RCOL[i].val);
    }
    printf("\\n");
    */

    for (int i = 0; i < n; i++) {
      int x1 = tape[i].x1;
      int x2 = tape[i].x2;
      int l, r;
      int ttt;
      int ddd;
      int lll;
      int rrr;
      l = 0;
      r = RCOL.size();
      while (l < r) {
        int mid = (l + r) / 2;
        if (RCOL[mid].val == x1) {
          lll = RCOL[mid].i;
          break;
        } else if (RCOL[mid].val > x1)
          r = mid;
        else
          l = mid + 1;
      }
      l = 0;
      r = RCOL.size();
      while (l < r) {
        int mid = (l + r) / 2;
        if (RCOL[mid].val == x2) {
          // printf("\\nrrr == x2 %d is %d\\n",x2,RCOL[mid].val);
          rrr = RCOL[mid].i;
          break;
        } else if (RCOL[mid].val > x2)
          r = mid;
        else
          l = mid + 1;
      }
      int y1 = tape[i].y1;
      int y2 = tape[i].y2;
      l = 0;
      r = RROW.size();
      while (l < r) {
        int mid = (l + r) / 2;
        if (RROW[mid].val == y1) {
          ddd = RROW[mid].i;
          break;
        } else if (RROW[mid].val > y1)
          r = mid;
        else
          l = mid + 1;
      }
      l = 0;
      r = RROW.size();
      while (l < r) {
        int mid = (l + r) / 2;
        if (RROW[mid].val == y2) {
          ttt = RROW[mid].i;
          break;
        } else if (RROW[mid].val > y2)
          r = mid;
        else
          l = mid + 1;
      }
      for (int j = lll; j < rrr; j++) {
        for (int k = ddd; k < ttt; k++) {
          field[j][k] = true;
        }
      }
      /*
      printf("R:%d\\nL:%d\\nD:%d\\nT:%d\\n",rrr,lll,ddd,ttt);
      for (int j=0; j<lenRow; j++) {
          for (int k=0; k<lenCol; k++) {
              if(field[j][k])printf("1 ");
              else printf("0 ");
          }
          printf("\\n");
      }
      printf("\\n");
      */
    }
    lenRow = RROW.size();
    lenCol = RCOL.size();
    int count = 0;
    for (int i = 0; i < lenRow; i++) {
      for (int j = 0; j < lenCol; j++) {
        if (!field[i][j])
          BFS(i, j), count++;
      }
    }
    printf("%d\\n", count);
  } while (true);
}