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

#include <bits/stdc++.h>
using namespace std;
int n, b[200005], tot, v[10000005], h[10000005], ans[200005], ww, s;
vector<int> vec;
struct P {
  int x, y, id;
} a[200005];
struct E {
  int to, next;
} e[28000005];
void A(int x, int y) { e[++ww] = {y, h[x]}, h[x] = ww; }
void dfs(int x) {
  if (!x || v[x])
    return;
  v[x] = 1;
  if (x <= n)
    vec.push_back(x), s++;
  for (int i = h[x]; i; i = e[i].next)
    dfs(e[i].to);
}
struct SEGMENT {
  int c[10000005][2], rt;
  bool is;
  void Ins(int &p, int l, int r, int x, int y) {
    int q = ++tot;
    c[q][0] = c[p][0], c[q][1] = c[p][1];
    if (!is)
      A(q, p);
    else
      A(p, q);
    p = q;
    if (l == r) {
      if (!is)
        A(p, y);
      else
        A(y, p);
      return;
    }
    int mid = (l + r) / 2;
    if (!is) {
      if (x <= mid)
        A(p, c[p][1]), Ins(c[p][0], l, mid, x, y), A(p, c[p][0]);
      else
        A(p, c[p][0]), Ins(c[p][1], mid + 1, r, x, y), A(p, c[p][1]);
    } else {
      if (x <= mid)
        A(c[p][1], p), Ins(c[p][0], l, mid, x, y), A(c[p][0], p);
      else
        A(c[p][0], p), Ins(c[p][1], mid + 1, r, x, y), A(c[p][1], p);
    }
  }
  void Link(int p, int l, int r, int x, int y) {
    if (!p)
      return;
    if (r <= x) {
      if (!is)
        return A(y, p), void();
      else
        return A(p, y), void();
    }
    int mid = (l + r) / 2;
    Link(c[p][0], l, mid, x, y);
    if (x > mid)
      Link(c[p][1], mid + 1, r, x, y);
  }
} q1, q2;
int main() {
  cin >> n, tot = n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].y, b[i] = a[i].y, a[i].id = i;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    a[i].y = lower_bound(b + 1, b + n + 1, a[i].y) - b;
  sort(a + 1, a + n + 1,
       [](P i, P j) { return i.x ^ j.x ? i.x < j.x : i.y < j.y; });
  q2.is = 1;
  for (int i = 1; i <= n; i++) {
    q1.Link(q1.rt, 1, n, a[i].y, i);
    q1.Ins(q1.rt, 1, n, a[i].y, i);
    q2.Link(q2.rt, 1, n, a[i].y, i);
    q2.Ins(q2.rt, 1, n, a[i].y, i);
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i]) {
      s = 0, vec.clear(), dfs(i);
      for (int j : vec)
        ans[a[j].id] = s;
    }
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, b[200005], tot, v[8000005], h[8000005], ans[200005], ww, s;
vector<int> vec;
struct P {
  int x, y, id;
} a[200005];
struct E {
  int to, next;
} e[24000005];
void A(int x, int y) { e[++ww] = {y, h[x]}, h[x] = ww; }
void dfs(int x) {
  if (!x || v[x])
    return;
  v[x] = 1;
  if (x <= n)
    vec.push_back(x), s++;
  for (int i = h[x]; i; i = e[i].next)
    dfs(e[i].to);
}
struct SEGMENT {
  int c[8000005][2], rt;
  bool is;
  void Ins(int &p, int l, int r, int x, int y) {
    int q = ++tot;
    c[q][0] = c[p][0], c[q][1] = c[p][1];
    if (!is)
      A(q, p);
    else
      A(p, q);
    p = q;
    if (l == r) {
      if (!is)
        A(p, y);
      else
        A(y, p);
      return;
    }
    int mid = (l + r) / 2;
    if (!is) {
      if (x <= mid)
        A(p, c[p][1]), Ins(c[p][0], l, mid, x, y), A(p, c[p][0]);
      else
        A(p, c[p][0]), Ins(c[p][1], mid + 1, r, x, y), A(p, c[p][1]);
    } else {
      if (x <= mid)
        A(c[p][1], p), Ins(c[p][0], l, mid, x, y), A(c[p][0], p);
      else
        A(c[p][0], p), Ins(c[p][1], mid + 1, r, x, y), A(c[p][1], p);
    }
  }
  void Link(int p, int l, int r, int x, int y) {
    if (!p)
      return;
    if (r <= x) {
      if (!is)
        return A(y, p), void();
      else
        return A(p, y), void();
    }
    int mid = (l + r) / 2;
    Link(c[p][0], l, mid, x, y);
    if (x > mid)
      Link(c[p][1], mid + 1, r, x, y);
  }
} q1, q2;
int main() {
  cin >> n, tot = n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].y, b[i] = a[i].y, a[i].id = i;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    a[i].y = lower_bound(b + 1, b + n + 1, a[i].y) - b;
  sort(a + 1, a + n + 1,
       [](P i, P j) { return i.x ^ j.x ? i.x < j.x : i.y < j.y; });
  q2.is = 1;
  for (int i = 1; i <= n; i++) {
    q1.Link(q1.rt, 1, n, a[i].y, i);
    q1.Ins(q1.rt, 1, n, a[i].y, i);
    q2.Link(q2.rt, 1, n, a[i].y, i);
    q2.Ins(q2.rt, 1, n, a[i].y, i);
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i]) {
      s = 0, vec.clear(), dfs(i);
      for (int j : vec)
        ans[a[j].id] = s;
    }
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}