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

#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  char ch = getchar();
  int f = 0;
  x = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = 1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
  if (f)
    x = -x;
}
const int N = 1024;
int n, m, sx, sy, tx[N], ty[N];
int cx[N][N], cy[N][N];
long long res = 0;
struct ea {
  int xa, xb, y;
  inline bool operator<(const ea &x) const {
    return y == x.y ? xa < x.xa : y < x.y;
  }
} a[N];
struct eb {
  int ya, yb, x;
  inline bool operator<(const eb &y) const {
    return x == y.x ? ya < y.ya : x < y.x;
  }
} b[N];
const int M = 1e6 + 1e5;
inline int f(int x, int y) { return (x - 1) * sx + y; }
vector<pair<int, int>> to[M];
int k[M];
#define fi first
#define se second
void dfs(int x, int y) {
  if (k[f(x, y)])
    return;
  k[f(x, y)] = 1;
  if (x == 1 || y == 1 || x == sy + 1 || y == sx + 1) {
    puts("INF");
    exit(0);
  }
  res += 1ll * (ty[x] - ty[x - 1]) * (tx[y] - tx[y - 1]);
  int tmp = f(x, y);
  for (int i = 0; i < to[tmp].size(); ++i)
    dfs(to[tmp][i].fi, to[tmp][i].se);
}
signed main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i)
    read(a[i].xa), read(a[i].xb), read(a[i].y);
  for (int i = 1; i <= m; ++i)
    read(b[i].x), read(b[i].ya), read(b[i].yb);

  sort(a + 1, a + n + 1), sort(b + 1, b + m + 1);
  ty[++sy] = a[1].y, tx[++sx] = b[1].x;
  for (int i = 2; i <= n; ++i)
    if (a[i].y != a[i - 1].y)
      ty[++sy] = a[i].y;
  for (int i = 2; i <= m; ++i)
    if (b[i].x != b[i - 1].x)
      tx[++sx] = b[i].x;
  for (int i = 1; i <= n; ++i)
    a[i].y = lower_bound(ty + 1, ty + sy + 1, a[i].y) - ty;
  for (int i = 1; i <= m; ++i)
    b[i].x = lower_bound(tx + 1, tx + sx + 1, b[i].x) - tx;

  for (int i = 1; i <= n; ++i) {
    int lx = a[i].xa, rx = a[i].xb;
    for (int j = i + 1; j <= n; ++j) {
      if (a[j].y != a[j - 1].y || a[j].xa > rx)
        break;
      rx = max(rx, a[j].xb), i = j;
    }
    lx = lower_bound(tx + 1, tx + sx + 1, lx) - tx;
    rx = upper_bound(tx + 1, tx + sx + 1, rx) - tx - 1;
    if (lx < rx)
      ++cx[a[i].y][lx], --cx[a[i].y][rx];
  }
  for (int i = 1; i <= m; ++i) {
    int ly = b[i].ya, ry = b[i].yb;
    for (int j = i + 1; j <= m; ++j) {
      if (b[j].x != b[j - 1].x || b[j].ya > ry)
        break;
      ry = max(ry, b[j].yb), i = j;
    }
    ly = lower_bound(ty + 1, ty + sy + 1, ly) - ty;
    ry = upper_bound(ty + 1, ty + sy + 1, ry) - ty - 1;
    if (ly < ry)
      ++cy[b[i].x][ly], --cy[b[i].x][ry];
  }
  for (int i = 1; i <= sy; ++i) {
    for (int j = 0, s = 0; j <= sx; ++j)
      if ((s += cx[i][j]) == 0)
        to[f(i, j + 1)].push_back(make_pair(i + 1, j + 1)),
            to[f(i + 1, j + 1)].push_back(make_pair(i, j + 1));
  }
  for (int i = 1; i <= sx; ++i) {
    for (int j = 0, s = 0; j <= sy; ++j)
      if ((s += cy[i][j]) == 0)
        to[f(j + 1, i)].push_back(make_pair(j + 1, i + 1)),
            to[f(j + 1, i + 1)].push_back(make_pair(j + 1, i));
  }
  int Sx = lower_bound(tx + 1, tx + sx + 1, 0) - tx;
  int Sy = lower_bound(ty + 1, ty + sy + 1, 0) - ty;
  if (0 > tx[sx] || tx[1] > 0 || 0 > ty[sy] || 0 < ty[1])
    return puts("INF"), 0;
  dfs(Sy, Sx);
  return printf("%lld\\n", res), 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  char ch = getchar();
  int f = 0;
  x = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = 1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
  if (f)
    x = -x;
}
const int N = 1024;
int n, m, sx, sy, tx[N], ty[N];
int cx[N][N], cy[N][N];
long long res = 0;
struct ea {
  int xa, xb, y;
  inline bool operator<(const ea &x) const { return y < x.y; }
} a[N];
struct eb {
  int ya, yb, x;
  inline bool operator<(const eb &y) const { return x < y.x; }
} b[N];
const int M = 1e6 + 10;
inline int f(int x, int y) { return (x - 1) * sx + y; }
vector<pair<int, int>> to[M];
int k[M];
#define fi first
#define se second
void dfs(int x, int y) {
  if (k[f(x, y)])
    return;
  k[f(x, y)] = 1;
  if (x == 1 || y == 1 || x == sx + 1 || y == sx + 1) {
    puts("INF");
    exit(0);
  }
  res += (ty[x] - ty[x - 1]) * (tx[y] - tx[y - 1]);
  int tmp = f(x, y);
  for (int i = 0; i < to[tmp].size(); ++i)
    dfs(to[tmp][i].fi, to[tmp][i].se);
}
signed main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i)
    read(a[i].xa), read(a[i].xb), read(a[i].y);
  for (int i = 1; i <= m; ++i)
    read(b[i].x), read(b[i].ya), read(b[i].yb);

  sort(a + 1, a + n + 1), sort(b + 1, b + m + 1);
  ty[++sy] = a[1].y, tx[++sx] = b[1].x;
  for (int i = 2; i <= n; ++i)
    if (a[i].y != a[i - 1].y)
      ty[++sy] = a[i].y;
  for (int i = 2; i <= m; ++i)
    if (b[i].x != b[i - 1].x)
      tx[++sx] = b[i].x;
  for (int i = 1; i <= n; ++i)
    a[i].y = lower_bound(ty + 1, ty + sy + 1, a[i].y) - ty;
  for (int i = 1; i <= m; ++i)
    b[i].x = lower_bound(tx + 1, tx + sx + 1, b[i].x) - tx;

  // for (int i = 1; i <= sx; ++i) printf ("%d ", tx[i]); puts ("");
  // for (int i = 1; i <= sy; ++i) printf ("%d ", ty[i]); puts ("");

  for (int i = 1; i <= n; ++i) {
    int lx = lower_bound(tx + 1, tx + sx + 1, a[i].xa) - tx;
    int rx = upper_bound(tx + 1, tx + sx + 1, a[i].xb) - tx - 1;
    ++cx[a[i].y][lx], --cx[a[i].y][rx];
  }
  for (int i = 1; i <= m; ++i) {
    int ly = lower_bound(ty + 1, ty + sy + 1, b[i].ya) - ty;
    int ry = upper_bound(ty + 1, ty + sy + 1, b[i].yb) - ty - 1;
    ++cy[b[i].x][ly], --cy[b[i].x][ry];
  }
  //    puts ("666");
  for (int i = 1; i <= sy; ++i) {
    for (int j = 0, s = 0; j <= sx; ++j)
      if ((s += cx[i][j]) == 0)
        to[f(i, j + 1)].push_back(make_pair(i + 1, j + 1)),
            to[f(i + 1, j + 1)].push_back(make_pair(i, j + 1));
  }
  for (int i = 1; i <= sx; ++i) {
    for (int j = 0, s = 0; j <= sy; ++j)
      if ((s += cy[i][j]) == 0)
        to[f(j + 1, i)].push_back(make_pair(j + 1, i + 1)),
            to[f(j + 1, i + 1)].push_back(make_pair(j + 1, i));
  }
  int Sx = lower_bound(tx + 1, tx + sx + 1, 0) - tx;
  int Sy = lower_bound(ty + 1, ty + sy + 1, 0) - ty;
  if (0 > tx[sx] || tx[1] > 0 || 0 > ty[sy] || 0 < ty[1])
    return puts("INF"), 0;
  //    puts ("666");
  dfs(Sx, Sy);
  return printf("%lld\\n", res), 0;
}