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

// Problem F

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>

#define MAXN 3030
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;

map<ll, int> mappax, mappay;
set<ii> grafo[MAXN][MAXN];
set<ll> x, y;
ll invmapx[MAXN], invmapy[MAXN];
int N, M;
ll a[MAXN], b[MAXN], c[MAXN]; // to store segments
bool reached[MAXN][MAXN];

int main() {
  // input
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
    x.insert(a[i]);
    x.insert(b[i]);
    y.insert(c[i]);
  }
  for (int i = 0; i < M; i++) {
    cin >> a[i + N] >> b[i + N] >> c[i + N];
    x.insert(a[i + N]);
    y.insert(b[i + N]);
    y.insert(c[i + N]);
  }

  x.insert(1000000001);
  x.insert(-1000000001);
  y.insert(1000000001);
  y.insert(-1000000001);

  // le mappe
  int cardx = 0;
  for (set<ll>::iterator it = x.begin(); it != x.end(); it++) {
    invmapx[cardx] = *it;
    mappax[*it] = cardx;
    cardx++;
  }
  int cardy = 0;
  for (set<ll>::iterator it = y.begin(); it != y.end(); it++) {
    invmapy[cardy] = *it;
    mappay[*it] = cardy;
    cardy++;
  }

  // i bordi
  int muri = 0;
  for (int i = 0; i < N; i++) {
    int X1 = mappax[a[i]];
    int X2 = mappax[b[i]];
    int Y = mappay[c[i]];
    //~ cout << "Muro orizzontale di lunghezza " << X2-X1 << endl;

    for (int j = X1; j < X2; j++) {
      grafo[Y - 1][j].insert(mp(Y, j));
      grafo[Y][j].insert(mp(Y - 1, j));
      muri++;
    }
  }
  for (int i = N; i < N + M; i++) {
    int X = mappax[a[i]];
    int Y1 = mappay[b[i]];
    int Y2 = mappay[c[i]];
    //~ cout << "Muro verticale di lunghezza " << Y2-Y1 << endl;

    for (int j = Y1; j < Y2; j++) {
      grafo[j][X - 1].insert(mp(j, X));
      grafo[j][X].insert(mp(j, X - 1));
      muri++;
    }
  }
  //~ cout << "Ho messo " << muri << " muri\\n";

  // dove sta 0
  int x0 = 0;
  while (invmapx[x0 + 1] <= 0)
    x0++;
  int y0 = 0;
  while (invmapy[y0 + 1] <= 0)
    y0++;

  // DEBUG
  //~ for (int i=0; i<cardx; i++) cout << invmapx[i] << " ";
  //~ cout << endl;
  //~ for (int i=0; i<cardy; i++) cout << invmapy[i] << " ";
  //~ cout << endl;

  // BFS
  queue<ii> Q;
  Q.push(mp(y0, x0));
  reached[y0][x0] = true;
  while (!Q.empty()) {
    ii d = Q.front();
    Q.pop();
    // look at the four neighbors
    // up
    if (d.first != 0 &&
        grafo[d.first][d.second].find(mp(d.first - 1, d.second)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first - 1][d.second]) {
      reached[d.first - 1][d.second] = true;
      Q.push(mp(d.first - 1, d.second));
    }
    // right
    if (d.second != cardx - 1 &&
        grafo[d.first][d.second].find(mp(d.first, d.second + 1)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first][d.second + 1]) {
      reached[d.first][d.second + 1] = true;
      Q.push(mp(d.first, d.second + 1));
    }
    // left
    if (d.second != 0 &&
        grafo[d.first][d.second].find(mp(d.first, d.second - 1)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first][d.second - 1]) {
      reached[d.first][d.second - 1] = true;
      Q.push(mp(d.first, d.second - 1));
    }
    // down
    if (d.first != cardy - 1 &&
        grafo[d.first][d.second].find(mp(d.first + 1, d.second)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first + 1][d.second]) {
      reached[d.first + 1][d.second] = true;
      Q.push(mp(d.first + 1, d.second));
    }
  }

  //~ cout << "Print output of BFS \\n";
  //~ for (int i=0; i<cardy; i++) {
  //~ for (int j=0; j<cardx; j++) cout << reached[i][j];
  //~ cout << endl;
  //~ }

  // calcolo l'output
  for (int i = 0; i < cardx; i++) {
    if (reached[0][i] || reached[cardy - 1][i]) {
      cout << "INF\\n";
      return 0;
    }
  }
  for (int i = 0; i < cardy; i++) {
    if (reached[i][0] || reached[i][cardx - 1]) {
      cout << "INF\\n";
      return 0;
    }
  }
  ll ans = 0;
  for (int i = 0; i < cardy; i++)
    for (int j = 0; j < cardx; j++)
      if (reached[i][j])
        ans =
            ans + (invmapx[j + 1] - invmapx[j]) * (invmapy[i + 1] - invmapy[i]);
  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

// Problem F

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>

#define MAXN 3000
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;

map<ll, int> mappax, mappay;
set<ii> grafo[MAXN][MAXN];
set<ll> x, y;
ll invmapx[MAXN], invmapy[MAXN];
int N, M;
ll a[MAXN], b[MAXN], c[MAXN]; // to store segments
bool reached[MAXN][MAXN];

int main() {
  // input
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
    x.insert(a[i]);
    x.insert(b[i]);
    y.insert(c[i]);
  }
  for (int i = 0; i < M; i++) {
    cin >> a[i + N] >> b[i + N] >> c[i + N];
    x.insert(a[i + N]);
    y.insert(b[i + N]);
    y.insert(c[i + N]);
  }

  x.insert(1000000001);
  x.insert(-1000000001);
  y.insert(1000000001);
  y.insert(-1000000001);

  // le mappe
  int cardx = 0;
  for (set<ll>::iterator it = x.begin(); it != x.end(); it++) {
    invmapx[cardx] = *it;
    mappax[*it] = cardx;
    cardx++;
  }
  int cardy = 0;
  for (set<ll>::iterator it = y.begin(); it != y.end(); it++) {
    invmapy[cardy] = *it;
    mappay[*it] = cardy;
    cardy++;
  }

  // i bordi
  for (int i = 0; i < N; i++) {
    int X1 = mappax[a[i]];
    int X2 = mappax[b[i]];
    int Y = mappay[c[i]];

    for (int j = X1; j < X2; j++) {
      grafo[Y - 1][j].insert(mp(Y, j));
      grafo[Y][j].insert(mp(Y - 1, j));
    }
  }
  for (int i = N; i < N + M; i++) {
    int X = mappax[a[i]];
    int Y1 = mappax[b[i]];
    int Y2 = mappay[c[i]];

    for (int j = Y1; j < Y2; j++) {
      grafo[j][X - 1].insert(mp(j, X));
      grafo[j][X].insert(mp(j, X - 1));
    }
  }

  // dove sta 0
  int x0 = 0;
  while (invmapx[x0 + 1] < 0)
    x0++;
  int y0 = 0;
  while (invmapy[y0 + 1] < 0)
    y0++;

  // BFS
  queue<ii> Q;
  Q.push(mp(y0, x0));
  reached[y0][x0] = true;
  while (!Q.empty()) {
    ii d = Q.front();
    Q.pop();
    // look at the four neighbors
    // up
    if (d.first != 0 &&
        grafo[d.first][d.second].find(mp(d.first - 1, d.second)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first - 1][d.second]) {
      reached[d.first - 1][d.second] = true;
      Q.push(mp(d.first - 1, d.second));
    }
    // right
    if (d.second != cardx - 1 &&
        grafo[d.first][d.second].find(mp(d.first, d.second + 1)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first][d.second + 1]) {
      reached[d.first][d.second + 1] = true;
      Q.push(mp(d.first, d.second + 1));
    }
    // left
    if (d.second != 0 &&
        grafo[d.first][d.second].find(mp(d.first, d.second - 1)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first][d.second - 1]) {
      reached[d.first][d.second - 1] = true;
      Q.push(mp(d.first, d.second - 1));
    }
    // down
    if (d.first != cardy - 1 &&
        grafo[d.first][d.second].find(mp(d.first + 1, d.second)) ==
            grafo[d.first][d.second].end() &&
        !reached[d.first + 1][d.second]) {
      reached[d.first + 1][d.second] = true;
      Q.push(mp(d.first + 1, d.second));
    }
  }

  // calcolo l'output
  for (int i = 0; i < cardx; i++) {
    if (reached[0][i] || reached[cardy - 1][i]) {
      cout << "INF\\n";
      return 0;
    }
  }
  for (int i = 0; i < cardy; i++) {
    if (reached[i][0] || reached[i][cardx - 1]) {
      cout << "INF\\n";
      return 0;
    }
  }
  ll ans = 0;
  for (int i = 0; i < cardy; i++)
    for (int j = 0; j < cardx; j++)
      if (reached[i][j])
        ans =
            ans + (invmapx[j + 1] - invmapx[j]) * (invmapy[i + 1] - invmapy[i]);
  cout << ans << endl;

  return 0;
}