Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
map<int, int> mpx, mpy;
int invx[14000], invy[14000], bias = 7000;
int a[1111], b[1111], c[1111], d[1111], e[1111], f[1111];
bool vis[14000][14000], l[14000][14000];
ll ans = 0;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
bool is_inf;
int mnx, mxx, mxy, mny;
void dfs(int x, int y) {
  if (is_inf)
    return;
  if (x > mxx || x < mnx || y > mxy || y < mny) {
    is_inf = 1;
    return;
  }
  if (vis[x + bias][y + bias]) {
    return;
  }
  vis[x + bias][y + bias] = 1;
  ll tmpx, tmpy;
  if (x % 2) {
    tmpx = 0;
  } else {
    tmpx = invx[x + 1 + bias] - invx[x - 1 + bias];
  }
  if (y % 2) {
    tmpy = 0;
  } else {
    tmpy = invy[y + 1 + bias] - invy[y - 1 + bias];
  }
  ans += tmpx * tmpy;
  if (l[x + bias][y + bias]) {
    return;
  }
  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    dfs(xx, yy);
  }
}
set<int> xx[2], yy[2];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < n; ++i) {
      scanf("%d%d%d", a + i, b + i, c + i);
      xx[a[i] > 0].insert(a[i]);
      xx[b[i] > 0].insert(b[i]);
      yy[c[i] > 0].insert(c[i]);
    }
    for (int i = 0; i < m; ++i) {
      scanf("%d%d%d", d + i, e + i, f + i);
      xx[d[i] > 0].insert(d[i]);
      yy[e[i] > 0].insert(e[i]);
      yy[f[i] > 0].insert(f[i]);
    }
    int cnt = -1;
    for (set<int>::reverse_iterator it = xx[0].rbegin(); it != xx[0].rend();
         ++it) {
      mpx[*it] = cnt;
      invx[cnt + bias] = *it;
      cnt -= 2;
    }
    mnx = cnt;
    cnt = 1;
    for (auto it : xx[1]) {
      mpx[it] = cnt;
      invx[cnt + bias] = it;
      cnt += 2;
    }
    mxx = cnt;
    cnt = -1;
    for (set<int>::reverse_iterator it = yy[0].rbegin(); it != yy[0].rend();
         ++it) {
      mpy[*it] = cnt;
      // cout<<*it<<" "<<cnt<<endl;
      invy[cnt + bias] = *it;
      cnt -= 2;
    }
    mny = cnt;
    cnt = 1;
    for (auto it : yy[1]) {
      mpy[it] = cnt;
      // cout<<it<< " "<<cnt<<endl;
      invy[cnt + bias] = it;
      cnt += 2;
    }
    mxy = cnt;
    for (int i = 0; i < n; ++i) {
      int tmp = mpy[c[i]];
      for (int j = mpx[a[i]]; j <= mpx[b[i]]; ++j) {
        // l[j][tmp]=1;
        l[j + bias][tmp + bias] = 1;
        // cout<<j<<" "<<tmp<<endl;
      }
    }
    for (int i = 0; i < m; ++i) {
      int tmp = mpx[d[i]];
      for (int j = mpy[e[i]]; j <= mpy[f[i]]; ++j) {
        // l[tmp][j]=1;
        l[tmp + bias][j + bias] = 1;
        // cout<<tmp<<" "<<j<<endl;
      }
    }
    // for(int i=-10; i<=10; ++i){
    // 	for(int j=-10; j<=10; ++j){
    // 		if(i==0 && j==0){
    // 			cout<<'*';
    // 			continue;
    // 		}
    // 		if(l.count(mk(i, j))){
    // 			cout<<'#';
    // 		}
    // 		else{
    // 			cout<<".";
    // 		}
    // 	}
    // 	cout<<endl;

    // }
    ans = 0;
    is_inf = 0;
    dfs(0, 0);
    if (is_inf) {
      printf("INF\n");
    } else
      printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
map<int, int> mpx, mpy;
int invx[14000], invy[14000], bias = 7000;
int a[1111], b[1111], c[1111], d[1111], e[1111], f[1111];
bool vis[14000][14000], l[14000][14000];
ll ans = 0;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
bool is_inf;
int mnx, mxx, mxy, mny;
void dfs(int x, int y) {
  stack<pair<int, int>> st;
  st.push(mk(x, y));
  while (!st.empty()) {
    x = st.top().fs;
    y = st.top().sc;
    st.pop();
    if (x > mxx || x < mnx || y > mxy || y < mny) {
      is_inf = 1;
      return;
    }
    if (vis[x + bias][y + bias]) {
      continue;
    }
    vis[x + bias][y + bias] = 1;
    ll tmpx, tmpy;
    if (x % 2) {
      tmpx = 0;
    } else {
      tmpx = invx[x + 1 + bias] - invx[x - 1 + bias];
    }
    if (y % 2) {
      tmpy = 0;
    } else {
      tmpy = invy[y + 1 + bias] - invy[y - 1 + bias];
    }
    ans += tmpx * tmpy;
    if (l[x + bias][y + bias]) {
      continue;
    }
    for (int i = 0; i < 4; ++i) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      st.push(mk(xx, yy));
    }
  }
}
set<int> xx[2], yy[2];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < n; ++i) {
      scanf("%d%d%d", a + i, b + i, c + i);
      xx[a[i] > 0].insert(a[i]);
      xx[b[i] > 0].insert(b[i]);
      yy[c[i] > 0].insert(c[i]);
    }
    for (int i = 0; i < m; ++i) {
      scanf("%d%d%d", d + i, e + i, f + i);
      xx[d[i] > 0].insert(d[i]);
      yy[e[i] > 0].insert(e[i]);
      yy[f[i] > 0].insert(f[i]);
    }
    int cnt = -1;
    for (set<int>::reverse_iterator it = xx[0].rbegin(); it != xx[0].rend();
         ++it) {
      mpx[*it] = cnt;
      invx[cnt + bias] = *it;
      cnt -= 2;
    }
    mnx = cnt;
    cnt = 1;
    for (auto it : xx[1]) {
      mpx[it] = cnt;
      invx[cnt + bias] = it;
      cnt += 2;
    }
    mxx = cnt;
    cnt = -1;
    for (set<int>::reverse_iterator it = yy[0].rbegin(); it != yy[0].rend();
         ++it) {
      mpy[*it] = cnt;
      // cout<<*it<<" "<<cnt<<endl;
      invy[cnt + bias] = *it;
      cnt -= 2;
    }
    mny = cnt;
    cnt = 1;
    for (auto it : yy[1]) {
      mpy[it] = cnt;
      // cout<<it<< " "<<cnt<<endl;
      invy[cnt + bias] = it;
      cnt += 2;
    }
    mxy = cnt;
    for (int i = 0; i < n; ++i) {
      int tmp = mpy[c[i]];
      for (int j = mpx[a[i]]; j <= mpx[b[i]]; ++j) {
        // l[j][tmp]=1;
        l[j + bias][tmp + bias] = 1;
        // cout<<j<<" "<<tmp<<endl;
      }
    }
    for (int i = 0; i < m; ++i) {
      int tmp = mpx[d[i]];
      for (int j = mpy[e[i]]; j <= mpy[f[i]]; ++j) {
        // l[tmp][j]=1;
        l[tmp + bias][j + bias] = 1;
        // cout<<tmp<<" "<<j<<endl;
      }
    }
    // for(int i=-10; i<=10; ++i){
    // 	for(int j=-10; j<=10; ++j){
    // 		if(i==0 && j==0){
    // 			cout<<'*';
    // 			continue;
    // 		}
    // 		if(l.count(mk(i, j))){
    // 			cout<<'#';
    // 		}
    // 		else{
    // 			cout<<".";
    // 		}
    // 	}
    // 	cout<<endl;

    // }
    ans = 0;
    is_inf = 0;
    dfs(0, 0);
    if (is_inf) {
      printf("INF\n");
    } else
      printf("%lld\n", ans);
  }
  return 0;
}