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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const ll inf = 5e18;
const int min_pos = -2e9;
ll ans = 0;
ll netx[1010], nety[1010];
int xcnt = 0, ycnt = 0;
struct node {
  int xl = 1;
  int xr = 1;
  int yu = 1;
  int yd = 1;
  int mark = 0;
} net[1010][1010];
struct p {
  int x;
  int y;
};
queue<p> que;
struct seg {
  int pos;
  int fr;
  int to;
  int pos_rank;
  bool operator<(const seg &seg2) {
    return (pos < seg2.pos || (pos == seg2.pos && fr < seg2.fr));
  }
} xseg[1001], yseg[1001], lastx = {min_pos}, lasty = {min_pos};

int lb(ll *q, int l, int r, const int &x) {
  while (l < r) {
    int mid = (l + r) / 2;
    if (x > q[mid])
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}

void cutx(const seg &s) {
  for (int i = lb(nety, 0, ycnt - 1, s.fr); nety[i + 1] <= s.to; i++) {
    net[s.pos_rank - 1][i].xr = net[s.pos_rank][i].xl = 0;
  }
}
void cuty(const seg &s) {
  for (int i = lb(netx, 0, xcnt - 1, s.fr); netx[i + 1] <= s.to; i++) {
    net[i][s.pos_rank - 1].yu = net[i][s.pos_rank].yd = 0;
  }
}
// bfs
void bfs() {
  que.push({lb(netx, 0, xcnt - 1, 0) - 1, lb(nety, 0, ycnt - 1, 0) - 1});
  do {
    p p1 = que.front();
    que.pop();
    int x = p1.x;
    int y = p1.y;
    node &n1 = net[x][y];
    if (n1.mark)
      continue;
    if (x == 0 || x == xcnt || y == 0 || y == ycnt) {
      ans = inf;
      break;
    }
    ans += (netx[x + 1] - netx[x]) * (nety[y + 1] - nety[y]);
    n1.mark = 1;
    if (n1.xl)
      que.push({x - 1, y});
    if (n1.xr)
      que.push({x + 1, y});
    if (n1.yu)
      que.push({x, y + 1});
    if (n1.yd)
      que.push({x, y - 1});
  } while (!que.empty());
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> xseg[i].fr >> xseg[i].to >> xseg[i].pos;
  }
  for (int i = 0; i < m; i++) {
    cin >> yseg[i].pos >> yseg[i].fr >> yseg[i].to;
  }
  sort(xseg, xseg + n);
  sort(yseg, yseg + m);
  netx[xcnt++] = min_pos;
  nety[ycnt++] = min_pos;
  for (auto &i : xseg) {
    if (i.pos > lastx.pos) {
      netx[xcnt++] = lastx.pos = i.pos;
    }
    i.pos_rank = xcnt - 1;
  }
  for (auto &i : yseg) {
    if (i.pos > lasty.pos) {
      nety[ycnt++] = lasty.pos = i.pos;
    }
    i.pos_rank = ycnt - 1;
  }
  netx[xcnt] = 2e9;
  nety[ycnt] = 2e9;
  if (0 <= netx[1] || 0 <= nety[1] || 0 >= netx[xcnt - 1] ||
      0 >= nety[ycnt - 1]) {
    cout << "INF";
    return 0;
  }
  for (int j = 0; j < n; j++) {
    if (j < n - 1)
      if (xseg[j].pos == xseg[j + 1].pos && xseg[j].to >= xseg[j + 1].fr) {
        xseg[j + 1].fr = xseg[j].fr;
        xseg[j + 1].to = max(xseg[j].to, xseg[j + 1].to);
        continue;
      }
    cutx(xseg[j]);
  }
  for (int j = 0; j < m; j++) {
    if (j < m - 1)
      if (yseg[j].pos == yseg[j + 1].pos && yseg[j].to >= yseg[j + 1].fr) {
        yseg[j + 1].fr = yseg[j].fr;
        yseg[j + 1].to = max(yseg[j].to, yseg[j + 1].to);
        continue;
      }
    cuty(yseg[j]);
  }
  bfs();
  if (ans >= inf)
    cout << "INF";
  else
    cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const ll inf = 5e18;
const int min_pos = -2e9;
ll ans = 0;
int netx[1001], nety[1001], xcnt = 0, ycnt = 0;
struct node {
  int xl = 1;
  int xr = 1;
  int yu = 1;
  int yd = 1;
  int mark = 0;
} net[1001][1001];
struct p {
  int x;
  int y;
} que[1000001];
struct seg {
  int pos;
  int fr;
  int to;
  int pos_rank;
  bool operator<(const seg &seg2) {
    return (pos < seg2.pos || (pos == seg2.pos && fr < seg2.fr));
  }
} xseg[1001], yseg[1001], lastx = {min_pos}, lasty = {min_pos};

int lb(int *q, int l, int r, const int &x) {
  while (l < r) {
    int mid = (l + r) / 2;
    if (x > q[mid])
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}

void cutx(const seg &s) {
  for (int i = lb(nety, 0, ycnt - 1, s.fr); nety[i + 1] <= s.to; i++) {
    net[s.pos_rank - 1][i].xr = net[s.pos_rank][i].xl = 0;
  }
}
void cuty(const seg &s) {
  for (int i = lb(netx, 0, xcnt - 1, s.fr); netx[i + 1] <= s.to; i++) {
    net[i][s.pos_rank - 1].yu = net[i][s.pos_rank].yd = 0;
  }
}
// bfs
void bfs() {
  que[0].x = lb(netx, 0, xcnt - 1, 0) - 1;
  que[0].y = lb(nety, 0, ycnt - 1, 0) - 1;
  int cur = 0;
  int cnt = 1;
  do {
    int x = que[cur].x;
    int y = que[cur].y;
    node &n1 = net[x][y];
    if (n1.mark)
      continue;
    if (x == 0 || x == xcnt || y == 0 || y == ycnt) {
      ans = inf;
      break;
    }
    ans += (netx[x + 1] - netx[x]) * (nety[y + 1] - nety[y]);
    n1.mark = 1;
    if (n1.xl)
      que[cnt++] = {x - 1, y};
    if (n1.xr)
      que[cnt++] = {x + 1, y};
    if (n1.yu)
      que[cnt++] = {x, y + 1};
    if (n1.yd)
      que[cnt++] = {x, y - 1};
  } while (++cur < cnt);
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> xseg[i].fr >> xseg[i].to >> xseg[i].pos;
  }
  for (int i = 0; i < m; i++) {
    cin >> yseg[i].pos >> yseg[i].fr >> yseg[i].to;
  }
  sort(xseg, xseg + n);
  sort(yseg, yseg + m);
  netx[xcnt++] = min_pos;
  nety[ycnt++] = min_pos;
  for (auto &i : xseg) {
    if (i.pos > lastx.pos) {
      netx[xcnt++] = lastx.pos = i.pos;
    }
    i.pos_rank = xcnt - 1;
  }
  for (auto &i : yseg) {
    if (i.pos > lasty.pos) {
      nety[ycnt++] = lasty.pos = i.pos;
    }
    i.pos_rank = ycnt - 1;
  }
  netx[xcnt] = 2e9;
  nety[ycnt] = 2e9;
  for (int j = 0; j < n; j++) {
    if (j < n - 1)
      if (xseg[j].pos == xseg[j + 1].pos && xseg[j].to >= xseg[j + 1].fr) {
        xseg[j + 1].fr = xseg[j].fr;
        xseg[j + 1].to = max(xseg[j].to, xseg[j + 1].to);
        continue;
      }
    cutx(xseg[j]);
  }
  for (int j = 0; j < m; j++) {
    if (j < m - 1)
      if (yseg[j].pos == yseg[j + 1].pos && yseg[j].to >= yseg[j + 1].fr) {
        yseg[j + 1].fr = yseg[j].fr;
        yseg[j + 1].to = max(yseg[j].to, yseg[j + 1].to);
        continue;
      }
    cuty(yseg[j]);
  }
  bfs();
  if (ans >= inf)
    cout << "INF";
  else
    cout << ans;
}