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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int N, M;
long long int A[1050], B[1050], C[1050], D[1050], E[1050], F[1050];

set<long long int> SX, SY;
long long int INF = 9999999999;

vector<long long int> X;
vector<long long int> Y;
map<long long int, int> NX, NY;

int num_X, num_Y;

set<pair<int, int>> Xp, Xm, Yp, Ym;

int MXp[3050][3050], MXm[3050][3050], MYp[3050][3050], MYm[3050][3050];

int used[3050][3050];

int q = 0;

void visit(int Px, int Py) {
  // q++;
  // cout << "Px= " << Px << "  Py= " << Py  <<"   q= " <<q<< endl;
  used[Px][Py] = 1;
  if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1) {
    visit(Px - 1, Py);
  }
  if (Px + 1 <= num_X && used[Px + 1][Py] == 0 && MXp[Px][Py] != -1) {
    visit(Px + 1, Py);
  }
  if (Py - 1 >= 0 && used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) {
    visit(Px, Py - 1);
  }
  if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1) {
    visit(Px, Py + 1);
  }
}

queue<pair<int, int>> Q;
queue<int> qx, qy;

int vx[100000000], vy[100000000];

int main(void) {

  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    cin >> A[i] >> B[i] >> C[i];
    SX.insert(A[i]);
    SX.insert(B[i]);
    SY.insert(C[i]);
  }
  for (int i = 1; i <= M; i++) {
    cin >> D[i] >> E[i] >> F[i];
    SX.insert(D[i]);
    SY.insert(E[i]);
    SY.insert(F[i]);
  }

  if (N == 1 || M == 1) {
    cout << "INF" << endl;
    return 0;
  }

  SX.insert(0);
  SY.insert(0);

  X.push_back(-INF);
  num_X = 0;

  for (auto v : SX) {
    NX[v] = X.size();
    num_X++;
    X.push_back(v);
  }

  Y.push_back(-INF);
  num_Y = 0;

  for (auto v : SY) {
    NY[v] = Y.size();
    num_Y++;
    Y.push_back(v);
  }

  for (int k = 1; k <= N; k++) {
    int j = NY[C[k]] - 1;
    // cout << "k="<< k << endl;
    for (int i = 1; i <= num_X - 1; i++) {
      long long int a = X[i];
      long long int b = X[i + 1];
      // cout << "a= " << a << " b= " << b;
      if (A[k] <= a && b <= B[k]) { // Yp.insert(make_pair(i, j));
                                    // Ym.insert(make_pair(i, j + 1));
        MYp[i][j] = -1;
        MYm[i][j + 1] = -1;
      }
    }
  }

  for (int k = 1; k <= M; k++) {
    int i = NX[D[k]] - 1;
    for (int j = 1; j <= num_Y - 1; j++) {
      long long int e = Y[j];
      long long int f = Y[j + 1];
      if (E[k] <= e && f <= F[k]) { // Xp.insert(make_pair(i, j));
                                    // Xm.insert(make_pair(i+1, j));
        MXp[i][j] = -1;
        MXm[i + 1][j] = -1;
      }
    }
  }

  // cout << "check<" << endl;
  //	visit(NX[0], NY[0]);

  /*	Q.push(make_pair(NX[0], NY[0]));
          pair<int, int> P; int Px, Py;

          while (Q.size() > 0) {
                  //q++;
                  //cout << "q= " << q << "   size: " <<Q.size()<<endl;
                  P = Q.front(); Q.pop();
                  Px = P.first; Py = P.second;
                  used[Px][Py] = 1;
                  if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1)
     { Q.push(make_pair(Px - 1, Py)); } if (Px + 1 <= num_X && used[Px + 1][Py]
     == 0 && MXp[Px][Py] != -1) { Q.push(make_pair(Px + 1, Py)); } if (Py - 1 >=
     0 && used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) { Q.push(make_pair(Px, Py
     - 1)); } if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1)
     { Q.push(make_pair(Px, Py + 1)); }

          }*/

  /*qx.push(NX[0]);
  qy.push(NY[0]);
int Px, Py;

  while (qx.size() > 0) {

          Px = qx.front(); qx.pop();
          Py = qy.front(); qy.pop();

          used[Px][Py] = 1;
          if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1) {
qx.push(Px - 1); qy.push(Py); } if (Px + 1 <= num_X && used[Px + 1][Py] == 0 &&
MXp[Px][Py] != -1) { qx.push(Px + 1); qy.push(Py);} if (Py - 1 >= 0 &&
used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) { qx.push(Px); qy.push(Py - 1);  }
          if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1) {
qx.push(Px); qy.push(Py + 1);  }

  }*/

  int p = 0;
  int q = 0;
  int Px, Py;
  vx[q] = NX[0];
  vy[q] = NY[0];
  q++;

  while (q > p) {
    Px = vx[p];
    Py = vy[p];
    p++;
    // cout << "Px= " << Px << " Py= " << Py << endl;
    used[Px][Py] = 1;
    if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1) {
      vx[q] = Px - 1;
      ;
      vy[q] = Py;
      used[Px - 1][Py] = 1;
      q++;
    }
    if (Px + 1 <= num_X && used[Px + 1][Py] == 0 && MXp[Px][Py] != -1) {
      vx[q] = Px + 1;
      vy[q] = Py;
      used[Px + 1][Py] = 1;
      q++;
    }
    if (Py - 1 >= 0 && used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) {
      vx[q] = Px;
      vy[q] = Py - 1;
      used[Px][Py - 1] = 1;
      q++;
    }
    if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1) {
      vx[q] = Px;
      vy[q] = Py + 1;
      used[Px][Py + 1] = 1;
      q++;
    }
  }

  int sign = 1;

  for (int i = 0; i <= num_X; i++) {
    if (used[i][0] == 1 || used[i][num_Y] == 1) {
      sign = -1;
    }
  }

  for (int j = 0; j <= num_Y; j++) {
    if (used[0][j] == 1 || used[num_X][j] == 1) {
      sign = -1;
    }
  }

  long long int ans = 0;

  for (int i = 1; i <= num_X - 1; i++) {
    for (int j = 1; j <= num_Y - 1; j++) {
      if (used[i][j] == 1) {
        ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
      }
    }
  }
  if (sign == -1) {
    cout << "INF" << endl;
  } else {
    cout << ans << endl;
  }

  /*for (int i = 0; i <= num_X; i++) {
          for (int j = 0; j <= num_Y; j++) {
                  cout << " ";
                  if (Xm.count(make_pair(i, j)) > 0) { cout << "-"; }
                  else { cout << " "; }
          }cout << endl;
          for (int j = 0; j <= num_Y; j++) {
                  if (Ym.count(make_pair(i, j)) > 0) { cout << "l"; }
                  else { cout << " "; }
                  cout << "o";

                  //if (Yp.count(make_pair(i, j)) > 0) { cout << "l"; }
                  //else { cout << " "; }
          }cout << endl;
  }*/

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int N, M;
long long int A[1050], B[1050], C[1050], D[1050], E[1050], F[1050];

set<long long int> SX, SY;
long long int INF = 9999999999;

vector<long long int> X;
vector<long long int> Y;
map<long long int, int> NX, NY;

int num_X, num_Y;

set<pair<int, int>> Xp, Xm, Yp, Ym;

int MXp[3050][3050], MXm[3050][3050], MYp[3050][3050], MYm[3050][3050];

int used[3050][3050];

int q = 0;

void visit(int Px, int Py) {
  // q++;
  // cout << "Px= " << Px << "  Py= " << Py  <<"   q= " <<q<< endl;
  used[Px][Py] = 1;
  if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1) {
    visit(Px - 1, Py);
  }
  if (Px + 1 <= num_X && used[Px + 1][Py] == 0 && MXp[Px][Py] != -1) {
    visit(Px + 1, Py);
  }
  if (Py - 1 >= 0 && used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) {
    visit(Px, Py - 1);
  }
  if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1) {
    visit(Px, Py + 1);
  }
}

queue<pair<int, int>> Q;
queue<int> qx, qy;

int vx[100000000], vy[100000000];

int main(void) {

  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    cin >> A[i] >> B[i] >> C[i];
    SX.insert(A[i]);
    SX.insert(B[i]);
    SY.insert(C[i]);
  }
  for (int i = 1; i <= M; i++) {
    cin >> D[i] >> E[i] >> F[i];
    SX.insert(D[i]);
    SY.insert(E[i]);
    SY.insert(F[i]);
  }

  if (N == 1 || M == 1) {
    cout << "INF" << endl;
    return 0;
  }

  SX.insert(0);
  SY.insert(0);

  X.push_back(-INF);
  num_X = 0;

  for (auto v : SX) {
    NX[v] = X.size();
    num_X++;
    X.push_back(v);
  }

  Y.push_back(-INF);
  num_Y = 0;

  for (auto v : SY) {
    NY[v] = Y.size();
    num_Y++;
    Y.push_back(v);
  }

  for (int k = 1; k <= N; k++) {
    int j = NY[C[k]] - 1;
    // cout << "k="<< k << endl;
    for (int i = 1; i <= num_X - 1; i++) {
      long long int a = X[i];
      long long int b = X[i + 1];
      // cout << "a= " << a << " b= " << b;
      if (A[k] <= a && b <= B[k]) { // Yp.insert(make_pair(i, j));
                                    // Ym.insert(make_pair(i, j + 1));
        MYp[i][j] = -1;
        MYm[i][j + 1] = -1;
      }
    }
  }

  for (int k = 1; k <= M; k++) {
    int i = NX[D[k]] - 1;
    for (int j = 1; j <= num_Y - 1; j++) {
      long long int e = Y[j];
      long long int f = Y[j + 1];
      if (E[k] <= e && f <= F[k]) { // Xp.insert(make_pair(i, j));
                                    // Xm.insert(make_pair(i+1, j));
        MXp[i][j] = -1;
        MXm[i + 1][j] = -1;
      }
    }
  }

  // cout << "check<" << endl;
  //	visit(NX[0], NY[0]);

  /*	Q.push(make_pair(NX[0], NY[0]));
          pair<int, int> P; int Px, Py;

          while (Q.size() > 0) {
                  //q++;
                  //cout << "q= " << q << "   size: " <<Q.size()<<endl;
                  P = Q.front(); Q.pop();
                  Px = P.first; Py = P.second;
                  used[Px][Py] = 1;
                  if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1)
     { Q.push(make_pair(Px - 1, Py)); } if (Px + 1 <= num_X && used[Px + 1][Py]
     == 0 && MXp[Px][Py] != -1) { Q.push(make_pair(Px + 1, Py)); } if (Py - 1 >=
     0 && used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) { Q.push(make_pair(Px, Py
     - 1)); } if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1)
     { Q.push(make_pair(Px, Py + 1)); }

          }*/

  /*qx.push(NX[0]);
  qy.push(NY[0]);
int Px, Py;

  while (qx.size() > 0) {

          Px = qx.front(); qx.pop();
          Py = qy.front(); qy.pop();

          used[Px][Py] = 1;
          if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1) {
qx.push(Px - 1); qy.push(Py); } if (Px + 1 <= num_X && used[Px + 1][Py] == 0 &&
MXp[Px][Py] != -1) { qx.push(Px + 1); qy.push(Py);} if (Py - 1 >= 0 &&
used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) { qx.push(Px); qy.push(Py - 1);  }
          if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1) {
qx.push(Px); qy.push(Py + 1);  }

  }*/

  int p = 0;
  int q = 0;
  int Px, Py;
  vx[q] = NX[0];
  vy[q] = NY[0];
  q++;

  while (q > p) {
    Px = vx[p];
    Py = vy[p];
    p++;
    // cout << "Px= " << Px << " Py= " << Py << endl;
    used[Px][Py] = 1;
    if (Px - 1 >= 0 && used[Px - 1][Py] == 0 && MXm[Px][Py] != -1) {
      vx[q] = Px - 1;
      ;
      vy[q] = Py;
      q++;
    }
    if (Px + 1 <= num_X && used[Px + 1][Py] == 0 && MXp[Px][Py] != -1) {
      vx[q] = Px + 1;
      vy[q] = Py;
      q++;
    }
    if (Py - 1 >= 0 && used[Px][Py - 1] == 0 && MYm[Px][Py] != -1) {
      vx[q] = Px;
      vy[q] = Py - 1;
      q++;
    }
    if (Py + 1 <= num_Y && used[Px][Py + 1] == 0 && MYp[Px][Py] != -1) {
      vx[q] = Px;
      vy[q] = Py + 1;
      q++;
    }
  }

  int sign = 1;

  for (int i = 0; i <= num_X; i++) {
    if (used[i][0] == 1 || used[i][num_Y] == 1) {
      sign = -1;
    }
  }

  for (int j = 0; j <= num_Y; j++) {
    if (used[0][j] == 1 || used[num_X][j] == 1) {
      sign = -1;
    }
  }

  long long int ans = 0;

  for (int i = 1; i <= num_X - 1; i++) {
    for (int j = 1; j <= num_Y - 1; j++) {
      if (used[i][j] == 1) {
        ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
      }
    }
  }
  if (sign == -1) {
    cout << "INF" << endl;
  } else {
    cout << ans << endl;
  }

  /*for (int i = 0; i <= num_X; i++) {
          for (int j = 0; j <= num_Y; j++) {
                  cout << " ";
                  if (Xm.count(make_pair(i, j)) > 0) { cout << "-"; }
                  else { cout << " "; }
          }cout << endl;
          for (int j = 0; j <= num_Y; j++) {
                  if (Ym.count(make_pair(i, j)) > 0) { cout << "l"; }
                  else { cout << " "; }
                  cout << "o";

                  //if (Yp.count(make_pair(i, j)) > 0) { cout << "l"; }
                  //else { cout << " "; }
          }cout << endl;
  }*/

  return 0;
}