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

#pragma GCC optimize(3)
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define rint register int
#define INF ((1 << 30) - 1)
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define Pair pair<int, int>
#define mst(a, b) memset(a, b, sizeof(a))
#define foR(i, k, j) for (rint i = (k); i >= (j); i--)
#define For(i, k, j) for (rint i = (k); i <= (j); i++)
#define Foe(i, u) for (rint i = lst[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file(s)                                                                \\
  freopen(s ".in", "r", stdin);                                                \\
  freopen(s ".out", "w", stdout)
// #define int long long
const int P = 1000000007; //
using namespace std;
inline void ckmax(int &a, int b) { a = max(a, b); }
inline void ckmin(int &a, int b) { a = min(a, b); }
inline void mulmod(int &a, int b) { a = 1ll * a * b % P; }
inline void addmod(int &a, int b) {
  int t = a + b;
  a = (t >= P ? t - P : t);
}
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = 1ll * ans * a % P;
    a = 1ll * a * a % P;
  }
  return ans;
}
inline int inv(int a) { return ksm(a, P - 2); }

inline void printarray(int *a, int n) {
  For(i, 1, n) fprintf(stderr, "%d ", a[i]);
  fprintf(stderr, "\\n");
}
namespace FastIO {
const int SIZE = 1 << 16;
char buf[SIZE], obuf[SIZE], str[64];
int bi = SIZE, bn = SIZE, opt;
int read(char *s) {
  while (bn) {
    for (; bi < bn && buf[bi] <= ' '; bi++)
      ;
    if (bi < bn)
      break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  int sn = 0;
  while (bn) {
    for (; bi < bn && buf[bi] > ' '; bi++)
      s[sn++] = buf[bi];
    if (bi < bn)
      break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  s[sn] = 0;
  return sn;
}
bool read(int &x) {
  if (x)
    x = 0;
  int bf = 0, n = read(str);
  if (!n)
    return 0;
  int i = 0;
  if (str[i] == '-')
    bf = 1, i = 1;
  for (x = 0; i < n; i++)
    x = x * 10 + str[i] - '0';
  if (bf)
    x = -x;
  return 1;
}
void write(int x) {
  if (!x)
    obuf[opt++] = '0';
  else {
    if (x < 0)
      obuf[opt++] = '-', x = -x;
    int sn = 0;
    while (x)
      str[sn++] = x % 10 + '0', x /= 10;
    for (int i = sn - 1; i >= 0; i--)
      obuf[opt++] = str[i];
  }
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void write(char x) {
  obuf[opt++] = x;
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void Fflush() {
  if (opt)
    fwrite(obuf, 1, opt, stdout);
  opt = 0;
}
}; // namespace FastIO
const int MAXN = 2e5 + 5;
struct Node {
  int x, y, id;
  bool operator<(const Node &b) const { return x < b.x; }
} a[MAXN];
int n, f[MAXN], siz[MAXN];
inline int find(int x) {
  while (x != f[x])
    x = f[x] = f[f[x]];
  return x;
}
inline void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x != y)
    siz[x] += siz[y], f[y] = x;
}
int s[MAXN], top, v[MAXN], ans[MAXN];
signed main() {
#ifndef ONLINE_JUDGE
  file("pro");
#endif
  FI(n);
  For(i, 1, n) FI(a[i].x), FI(a[i].y), a[i].id = i, f[i] = i, siz[i] = 1;
  sort(a + 1, a + 1 + n);
  For(i, 1, n) {
    int y = a[i].y;
    while (top && v[top] < a[i].y) {
      merge(i, s[top]);
      ckmin(y, v[top]);
      top--;
    }
    s[++top] = i, v[top] = y;
  }
  For(i, 1, n) ans[a[i].id] = siz[find(i)];
  For(i, 1, n) FO(ans[i]), FO('\\n');

  return FastIO::Fflush(), 0;
}
/*
Think twice :
mod ?
INF ?
n = 1 ?
long long ?
Fastio::Fflush() ?

*/


Error: WA

Fixed submission:

#pragma GCC optimize(3)
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define rint register int
#define INF ((1 << 30) - 1)
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define Pair pair<int, int>
#define mst(a, b) memset(a, b, sizeof(a))
#define foR(i, k, j) for (rint i = (k); i >= (j); i--)
#define For(i, k, j) for (rint i = (k); i <= (j); i++)
#define Foe(i, u) for (rint i = lst[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file(s)                                                                \\
  freopen(s ".in", "r", stdin);                                                \\
  freopen(s ".out", "w", stdout)
// #define int long long
const int P = 1000000007; //
using namespace std;
inline void ckmax(int &a, int b) { a = max(a, b); }
inline void ckmin(int &a, int b) { a = min(a, b); }
inline void mulmod(int &a, int b) { a = 1ll * a * b % P; }
inline void addmod(int &a, int b) {
  int t = a + b;
  a = (t >= P ? t - P : t);
}
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = 1ll * ans * a % P;
    a = 1ll * a * a % P;
  }
  return ans;
}
inline int inv(int a) { return ksm(a, P - 2); }

inline void printarray(int *a, int n) {
  For(i, 1, n) fprintf(stderr, "%d ", a[i]);
  fprintf(stderr, "\\n");
}
namespace FastIO {
const int SIZE = 1 << 16;
char buf[SIZE], obuf[SIZE], str[64];
int bi = SIZE, bn = SIZE, opt;
int read(char *s) {
  while (bn) {
    for (; bi < bn && buf[bi] <= ' '; bi++)
      ;
    if (bi < bn)
      break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  int sn = 0;
  while (bn) {
    for (; bi < bn && buf[bi] > ' '; bi++)
      s[sn++] = buf[bi];
    if (bi < bn)
      break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  s[sn] = 0;
  return sn;
}
bool read(int &x) {
  if (x)
    x = 0;
  int bf = 0, n = read(str);
  if (!n)
    return 0;
  int i = 0;
  if (str[i] == '-')
    bf = 1, i = 1;
  for (x = 0; i < n; i++)
    x = x * 10 + str[i] - '0';
  if (bf)
    x = -x;
  return 1;
}
void write(int x) {
  if (!x)
    obuf[opt++] = '0';
  else {
    if (x < 0)
      obuf[opt++] = '-', x = -x;
    int sn = 0;
    while (x)
      str[sn++] = x % 10 + '0', x /= 10;
    for (int i = sn - 1; i >= 0; i--)
      obuf[opt++] = str[i];
  }
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void write(char x) {
  obuf[opt++] = x;
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void Fflush() {
  if (opt)
    fwrite(obuf, 1, opt, stdout);
  opt = 0;
}
}; // namespace FastIO
const int MAXN = 2e5 + 5;
struct Node {
  int x, y, id;
  bool operator<(const Node &b) const { return x < b.x; }
} a[MAXN];
int n, f[MAXN], siz[MAXN];
inline int find(int x) {
  while (x != f[x])
    x = f[x] = f[f[x]];
  return x;
}
inline int merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y)
    return 0;
  siz[x] += siz[y], f[y] = x;
}
int s[MAXN], top, ans[MAXN];
signed main() {
#ifndef ONLINE_JUDGE
  file("pro");
#endif
  FI(n);
  For(i, 1, n) FI(a[i].x), FI(a[i].y), a[i].id = i, f[i] = i, siz[i] = 1;
  sort(a + 1, a + 1 + n);
  For(i, 1, n) {
    while (top && a[s[top]].y < a[i].y) {
      merge(i, s[top]);
      top--;
    }
    s[++top] = i;
  }
  For(i, 1, n) { ans[a[i].id] = siz[find(i)]; }
  For(i, 1, n) {
    FO(ans[i]);
    FO('\\n');
  }
  return FastIO::Fflush(), 0;
}
/*
Think twice :
mod ?
INF ?
n = 1 ?
long long ?
Fastio::Fflush() ?

*/