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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 40005;

#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline int in() {
  int n, c;
  while ((c = getchar()) < '0')
    if (c == EOF)
      return -1;
  n = c - '0';
  while ((c = getchar()) >= '0')
    n = n * 10 + c - '0';
  return n;
}

map<P, int> mp;

class UF_pop {
public:
  vector<int> par, nrank;
  vector<vector<int>> hist;
  int sz;
  UF_pop(int node_size) {
    sz = node_size;
    par = nrank = vector<int>(sz, 0);
    rep(i, sz) { par[i] = i; }
  }
  void reinit() {
    rep(i, sz) {
      nrank[i] = 0;
      par[i] = i;
    }
  }
  int find(int x) { return (par[x] == x) ? (x) : find(par[x]); }
  void pop() {
    if (len(hist)) {
      auto &a = hist.back();
      par[a[0]] = a[0], nrank[a[0]] = a[1], par[a[2]] = a[2];
      hist.pop_back();
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    hist.pb({x, nrank[x], y});
    if (x == y)
      return;
    if (nrank[x] < nrank[y]) {
      par[x] = y;
    } else {
      nrank[x] += (nrank[x] == nrank[y]);
      par[y] = x;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

vvi vec;

void dfs(int l, int r, UF_pop &uf, vi &vid, vi &qid, vvi &que, vi &res) {
  vi lvid, rvid;
  int num = 0;
  int mid = (l + r) / 2;
  int st = lower_bound(all(qid), l) - qid.begin();
  int ed = lower_bound(all(qid), r) - qid.begin();
  if (ed <= st) {
    return;
  }
  if (vid.empty()) {
    srep(i, st, ed) {
      if (uf.same(que[i][0], que[i][1])) {
        res[i] = 1;
      }
    }
    return;
  }
  each(i, vid) {
    vi &v = vec[i];
    if (v[2] <= l && r <= v[3]) {
      uf.unite(v[0], v[1]);
      ++num;
    } else {
      if (v[2] < mid) {
        lvid.pb(i);
      }
      if (v[3] > mid) {
        rvid.pb(i);
      }
    }
  }
  dfs(l, mid, uf, lvid, qid, que, res);
  dfs(mid, r, uf, rvid, qid, que, res);
  while (num) {
    uf.pop();
    --num;
  }
}

int main() {
  int n = in(), q = in();
  vi vid, qid;
  vvi que;
  rep(i, q) {
    int x = in(), y = in(), z = in();
    if (x == 1) {
      mp[{y, z}] = i;
    } else if (x == 2) {
      vec.pb({y, z, mp[{y, z}], i});
      mp.erase({y, z});
    } else {
      qid.pb(i);
      que.pb({y, z});
    }
  }
  each(it, mp) { vec.pb({(it.fi).fi, (it.fi).se, it.se, q}); }
  vid.resize(len(vec));
  iota(all(vid), 0);
  UF_pop uf(n);
  vi res(len(que), 0);
  dfs(0, q, uf, vid, qid, que, res);
  rep(i, len(res)) {
    if (res[i]) {
      putchar('Y'), putchar('E'), putchar('S'), putchar('\\n');
    } else {
      putchar('N'), putchar('O'), putchar('\\n');
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 40005;

#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline int in() {
  int n, c;
  while ((c = getchar()) < '0')
    if (c == EOF)
      return -1;
  n = c - '0';
  while ((c = getchar()) >= '0')
    n = n * 10 + c - '0';
  return n;
}

map<P, int> mp;

class UF_pop {
public:
  vector<int> par, nrank;
  vector<vector<int>> hist;
  int sz;
  UF_pop(int node_size) {
    sz = node_size;
    par = nrank = vector<int>(sz, 0);
    rep(i, sz) { par[i] = i; }
  }
  void reinit() {
    rep(i, sz) {
      nrank[i] = 0;
      par[i] = i;
    }
  }
  int find(int x) { return (par[x] == x) ? (x) : find(par[x]); }
  void pop() {
    if (len(hist)) {
      auto &a = hist.back();
      hist.pop_back();
      par[a[0]] = a[0], nrank[a[0]] = a[1], par[a[2]] = a[2];
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    hist.pb({x, nrank[x], y});
    if (x == y)
      return;
    if (nrank[x] < nrank[y]) {
      par[x] = y;
    } else {
      nrank[x] += (nrank[x] == nrank[y]);
      par[y] = x;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

vvi vec;

void dfs(int l, int r, UF_pop &uf, vi &vid, vi &qid, vvi &que, vi &res) {
  vi lvid, rvid;
  int num = 0;
  int mid = (l + r) / 2;
  int st = lower_bound(all(qid), l) - qid.begin();
  int ed = lower_bound(all(qid), r) - qid.begin();
  if (ed <= st) {
    return;
  }
  if (vid.empty()) {
    srep(i, st, ed) {
      if (uf.same(que[i][0], que[i][1])) {
        res[i] = 1;
      }
    }
    return;
  }
  each(i, vid) {
    vi &v = vec[i];
    if (v[2] <= l && r <= v[3]) {
      uf.unite(v[0], v[1]);
      ++num;
    } else {
      if (v[2] < mid) {
        lvid.pb(i);
      }
      if (v[3] > mid) {
        rvid.pb(i);
      }
    }
  }
  dfs(l, mid, uf, lvid, qid, que, res);
  dfs(mid, r, uf, rvid, qid, que, res);
  while (num) {
    uf.pop();
    --num;
  }
}

int main() {
  int n = in(), q = in();
  vi vid, qid;
  vvi que;
  rep(i, q) {
    int x = in(), y = in(), z = in();
    if (x == 1) {
      mp[{y, z}] = i;
    } else if (x == 2) {
      vec.pb({y, z, mp[{y, z}], i});
      mp.erase({y, z});
    } else {
      qid.pb(i);
      que.pb({y, z});
    }
  }
  each(it, mp) { vec.pb({(it.fi).fi, (it.fi).se, it.se, q}); }
  vid.resize(len(vec));
  iota(all(vid), 0);
  UF_pop uf(n);
  vi res(len(que), 0);
  dfs(0, q, uf, vid, qid, que, res);
  rep(i, len(res)) {
    if (res[i]) {
      putchar('Y'), putchar('E'), putchar('S'), putchar('\\n');
    } else {
      putchar('N'), putchar('O'), putchar('\\n');
    }
  }
  return 0;
}