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

// I look at my watch
// it say nine twenty-five
// And I think oh God
// I'm still alive

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) int((x).size())
#define all(v) (v).begin(), (v).end()
#define trace(x) cerr << #x << " = " << x << endl
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define roF(i, a, b) for (int i = int(a); i >= int(b); i--)
using namespace std;

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

const int N = 4e3 + 100;
const int inf = 2e9;
const ll inf2 = 5e18;

int fx[] = {0, 0, -1, 1};
int fy[] = {1, -1, 0, 0};

bool gfo[2 * N * N][5];
bool vst[2 * N * N];
ll area[2 * N * N], A;
int n, m;

int trans(int i, int j) { return m * i + j; }

ll sum(ll a, ll b) {
  if (a == inf2 || b == inf2)
    return inf2;
  return a + b;
}

void dfs(int y, int x) {
  vst[trans(y, x)] = true;
  A = sum(A, area[trans(y, x)]);
  if (A == inf2)
    return;
  For(c, 0, 4) if (gfo[trans(y, x)][c]) {
    int yy = y + fy[c];
    int xx = x + fx[c];
    if (xx < 0 || xx >= m)
      continue;
    if (yy < 0 || yy >= n)
      continue;
    if (vst[trans(yy, xx)])
      continue;
    dfs(yy, xx);
  }
}

void bfs(int iniy, int inix) {
  vst[trans(iniy, inix)] = true;
  queue<ii> q;
  q.push({iniy, inix});
  while (!q.empty()) {
    int y = q.front().ff;
    int x = q.front().ss;
    q.pop();
    A = sum(A, area[trans(y, x)]);
    For(c, 0, 4) if (gfo[trans(y, x)][c]) {
      int yy = y + fy[c];
      int xx = x + fx[c];
      if (xx < 0 || xx >= m)
        continue;
      if (yy < 0 || yy >= n)
        continue;
      if (vst[trans(yy, xx)])
        continue;
      q.push({yy, xx});
      vst[trans(yy, xx)] = true;
    }
  }
}

int main() {
  memset(gfo, false, sizeof(gfo));
  memset(vst, false, sizeof(vst));
  memset(area, 0, sizeof(area));
  cin >> n >> m;
  set<int> X, Y;
  X.insert(inf);
  X.insert(-inf);
  Y.insert(inf);
  Y.insert(-inf);
  vector<ii> LH, LV;
  For(i, 0, n) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b)
      swap(a, b);
    X.insert(c);
    Y.insert(a);
    Y.insert(b);
    LV.pb({c, a});
    LV.pb({c, b});
  }
  For(i, 0, m) {
    int d, e, f;
    cin >> d >> e >> f;
    if (e > f)
      swap(e, f);
    X.insert(e);
    X.insert(f);
    Y.insert(d);
    LH.pb({e, d});
    LH.pb({f, d});
  }
  vector<ll> XX, YY;
  for (int x : X)
    XX.pb(x);
  for (int y : Y)
    YY.pb(y);

  n = sz(Y) - 1, m = sz(X) - 1;
  For(i, 0, sz(YY) - 1) For(j, 0, sz(XX) - 1) {
    if (XX[j] == inf || XX[j] == -inf || XX[j + 1] == inf ||
        XX[j + 1] == -inf || YY[i] == inf || YY[i] == -inf ||
        YY[i + 1] == inf || YY[i + 1] == -inf)
      area[trans(i, j)] = inf2;
    else
      area[trans(i, j)] = (YY[i + 1] - YY[i]) * (XX[j + 1] - XX[j]);
  }
  For(i, 0, sz(YY) - 1) For(j, 0, sz(XX) - 1) {
    For(c, 0, 4) gfo[trans(i, j)][c] = true;
    if (i == 0)
      gfo[trans(i, j)][1] = false;
    if (i == sz(YY) - 2)
      gfo[trans(i, j)][0] = false;
    if (j == 0)
      gfo[trans(i, j)][3] = false;
    if (j == sz(XX) - 2)
      gfo[trans(i, j)][2] = false;
  }

  for (int i = 0; i < sz(LH); i += 2) {
    int pos = lower_bound(all(YY), LH[i].ss) - YY.begin();
    For(j, 0, sz(XX)) if (XX[j] >= LH[i].ff && XX[j] < LH[i + 1].ff) {
      gfo[trans(pos - 1, j)][0] = false;
      gfo[trans(pos, j)][1] = false;
    }
  }
  for (int i = 0; i < sz(LV); i += 2) {
    int pos = lower_bound(all(XX), LV[i].ff) - XX.begin();
    For(j, 0, sz(YY)) if (YY[j] >= LV[i].ss && YY[j] < LV[i + 1].ss) {
      gfo[trans(j, pos - 1)][3] = false;
      gfo[trans(j, pos)][2] = false;
    }
  }

  // if(sz(LV)==2000||sz(LH)==2000)return 0;

  int iniy = upper_bound(all(YY), 0) - YY.begin();
  int inix = upper_bound(all(XX), 0) - XX.begin();
  iniy--, inix--;
  A = 0;
  bfs(iniy, inix);
  if (A != inf2)
    cout << A << endl;
  else
    cout << "INF" << endl;
  return 0;
}


Error: -11

Fixed submission:

// I look at my watch
// it say nine twenty-five
// And I think oh God
// I'm still alive

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) int((x).size())
#define all(v) (v).begin(), (v).end()
#define trace(x) cerr << #x << " = " << x << endl
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define roF(i, a, b) for (int i = int(a); i >= int(b); i--)
using namespace std;

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

const int N = 4e3 + 100;
const int inf = 2e9;
const ll inf2 = 5e18;

int fx[] = {0, 0, -1, 1};
int fy[] = {1, -1, 0, 0};

bool gfo[2 * N * N][5];
bool vst[2 * N * N];
ll area[2 * N * N], A;
int n, m;

int trans(int i, int j) { return m * i + j; }

ll sum(ll a, ll b) {
  if (a == inf2 || b == inf2)
    return inf2;
  return a + b;
}

void dfs(int y, int x) {
  vst[trans(y, x)] = true;
  A = sum(A, area[trans(y, x)]);
  if (A == inf2)
    return;
  For(c, 0, 4) if (gfo[trans(y, x)][c]) {
    int yy = y + fy[c];
    int xx = x + fx[c];
    if (xx < 0 || xx >= m)
      continue;
    if (yy < 0 || yy >= n)
      continue;
    if (vst[trans(yy, xx)])
      continue;
    dfs(yy, xx);
  }
}

int main() {
  memset(gfo, false, sizeof(gfo));
  memset(vst, false, sizeof(vst));
  memset(area, 0, sizeof(area));
  cin >> n >> m;
  set<int> X, Y;
  X.insert(inf);
  X.insert(-inf);
  Y.insert(inf);
  Y.insert(-inf);
  vector<ii> LH, LV;
  For(i, 0, n) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b)
      swap(a, b);
    X.insert(c);
    Y.insert(a);
    Y.insert(b);
    LV.pb({c, a});
    LV.pb({c, b});
  }
  For(i, 0, m) {
    int d, e, f;
    cin >> d >> e >> f;
    if (e > f)
      swap(e, f);
    X.insert(e);
    X.insert(f);
    Y.insert(d);
    LH.pb({e, d});
    LH.pb({f, d});
  }
  vector<ll> XX, YY;
  for (int x : X)
    XX.pb(x);
  for (int y : Y)
    YY.pb(y);

  n = sz(Y) - 1, m = sz(X) - 1;
  For(i, 0, sz(YY) - 1) For(j, 0, sz(XX) - 1) {
    if (XX[j] == inf || XX[j] == -inf || XX[j + 1] == inf ||
        XX[j + 1] == -inf || YY[i] == inf || YY[i] == -inf ||
        YY[i + 1] == inf || YY[i + 1] == -inf)
      area[trans(i, j)] = inf2;
    else
      area[trans(i, j)] = (YY[i + 1] - YY[i]) * (XX[j + 1] - XX[j]);
  }
  For(i, 0, sz(YY) - 1) For(j, 0, sz(XX) - 1) {
    For(c, 0, 4) gfo[trans(i, j)][c] = true;
    if (i == 0)
      gfo[trans(i, j)][1] = false;
    if (i == sz(YY) - 2)
      gfo[trans(i, j)][0] = false;
    if (j == 0)
      gfo[trans(i, j)][3] = false;
    if (j == sz(XX) - 2)
      gfo[trans(i, j)][2] = false;
  }

  for (int i = 0; i < sz(LH); i += 2) {
    int pos = lower_bound(all(YY), LH[i].ss) - YY.begin();
    For(j, 0, sz(XX)) if (XX[j] >= LH[i].ff && XX[j] < LH[i + 1].ff) {
      gfo[trans(pos - 1, j)][0] = false;
      gfo[trans(pos, j)][1] = false;
    }
  }
  for (int i = 0; i < sz(LV); i += 2) {
    int pos = lower_bound(all(XX), LV[i].ff) - XX.begin();
    For(j, 0, sz(YY)) if (YY[j] >= LV[i].ss && YY[j] < LV[i + 1].ss) {
      gfo[trans(j, pos - 1)][3] = false;
      gfo[trans(j, pos)][2] = false;
    }
  }

  // if(sz(LV)==2000||sz(LH)==2000)return 0;

  int iniy = upper_bound(all(YY), 0) - YY.begin();
  int inix = upper_bound(all(XX), 0) - XX.begin();
  iniy--, inix--;
  A = 0;
  dfs(iniy, inix);
  if (A != inf2)
    cout << A << endl;
  else
    cout << "INF" << endl;
  return 0;
}