Problem p02776 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pi pair<int, int>
#define l first
#define r second
#define pb push_back
#define all(x) x.begin(), x.end()
// #define DEBUG

using namespace std;

const int maxn = 2e5 + 1;

bool b[maxn], t[maxn], usd[maxn], hon[maxn];
int rm[maxn], n, m, val[maxn], pal[maxn];
vector<pi> g[maxn], f[maxn];
vector<int> cv, gv[maxn];
pi a[maxn], v[maxn];

int pst(bool x) { return (x ? -1 : 1); }

void dfs0(int x, int pr = -1) {
  val[x] = 1;
  hon[x] = t[x];
  for (auto to : f[x]) {
    if (to.l == pr)
      continue;
    dfs0(to.l, x);
    hon[x] ^= hon[to.l];
  }
}

void dfs(int x, int pr = -1) {
  usd[x] = 1;
  for (auto to : f[x]) {
    if (to.l == pr) {
      if (hon[x])
        cv.pb(to.r + 1);
      continue;
    }
    dfs(to.l, x);
  }
}

int p[maxn], sz[maxn], cnt[maxn];

int getp(int x) {
  if (p[x] == -1)
    return x;
  return (p[x] = getp(p[x]));
}

void unions(int x, int y) {
  x = getp(x);
  y = getp(y);
  if (x == y)
    return;
  if (sz[x] < sz[y])
    swap(x, y);
  if (sz[x] == sz[y])
    ++sz[x];
  p[y] = x;
  cnt[x] += cnt[y];
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i].l >> a[i].r;
  for (int i = 0; i < m; i++)
    cin >> v[i].l >> v[i].r;
  sort(a, a + n);
  b[n] = 0;
  for (int i = 0; i < n; i++) {
    rm[i] = a[i].l;
    b[i] = a[i].r;
  }
  for (int i = 0; i < m; i++) {
    v[i].l = (int)(lower_bound(rm, rm + n, v[i].l) - rm);
    v[i].r = (int)(upper_bound(rm, rm + n, v[i].r) - rm) - 1;
  }
  for (int i = 0; i <= n; i++) {
    if (!b[i])
      continue;
    t[i] ^= 1;
    t[i + 1] ^= 1;
  }
  for (int i = 0; i <= n; i++)
    cnt[i] = t[i];
  for (int i = 0; i < m; i++) {
    if (v[i].l > v[i].r)
      continue;
    g[v[i].l].pb({v[i].r + 1, i});
    g[v[i].r + 1].pb({v[i].l, i});
  }
  for (int i = 0; i <= n; i++) {
    for (auto to : g[i]) {
      if (getp(to.l) == getp(i))
        continue;
      f[i].pb({to.l, to.r});
      f[to.l].pb({i, to.r});
      unions(i, to.l);
    }
  }
  for (int i = 0; i <= n; i++) {
    if (cnt[getp(i)] % 2) {
      cout << -1;
      return 0;
    }
    if (t[i])
      gv[getp(i)].pb(i);
  }
  for (int i = 0; i <= n; i++)
    if (!val[i])
      dfs0(i);
  int ls = -1;
  for (int i = 0; i <= n; i++)
    if (!usd[i])
      dfs(i);
  sort(all(cv));
  cout << cv.size() << '\n';
  for (auto gg : cv)
    cout << gg << ' ';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pi pair<int, int>
#define l first
#define r second
#define pb push_back
#define all(x) x.begin(), x.end()
// #define DEBUG

using namespace std;

const int maxn = 2e5 + 1;

bool b[maxn], t[maxn], usd[maxn], hon[maxn];
int rm[maxn], n, m, val[maxn], pal[maxn];
vector<pi> g[maxn], f[maxn];
vector<int> cv, gv[maxn];
pi a[maxn], v[maxn];

int pst(bool x) { return (x ? -1 : 1); }

void dfs0(int x, int pr = -1) {
  val[x] = 1;
  hon[x] = t[x];
  for (auto to : f[x]) {
    if (to.l == pr)
      continue;
    dfs0(to.l, x);
    hon[x] ^= hon[to.l];
  }
}

void dfs(int x, int pr = -1) {
  usd[x] = 1;
  for (auto to : f[x]) {
    if (to.l == pr) {
      if (hon[x])
        cv.pb(to.r + 1);
      continue;
    }
    dfs(to.l, x);
  }
}

int p[maxn], sz[maxn], cnt[maxn];

int getp(int x) {
  if (p[x] == -1)
    return x;
  return (p[x] = getp(p[x]));
}

void unions(int x, int y) {
  x = getp(x);
  y = getp(y);
  if (x == y)
    return;
  if (sz[x] < sz[y])
    swap(x, y);
  if (sz[x] == sz[y])
    ++sz[x];
  p[y] = x;
  cnt[x] += cnt[y];
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i].l >> a[i].r;
  for (int i = 0; i < m; i++)
    cin >> v[i].l >> v[i].r;
  sort(a, a + n);
  b[n] = 0;
  for (int i = 0; i < n; i++) {
    rm[i] = a[i].l;
    b[i] = a[i].r;
  }
  for (int i = 0; i < m; i++) {
    v[i].l = (int)(lower_bound(rm, rm + n, v[i].l) - rm);
    v[i].r = (int)(upper_bound(rm, rm + n, v[i].r) - rm) - 1;
  }
  for (int i = 0; i <= n; i++) {
    p[i] = -1;
    sz[i] = 1;
    if (!b[i])
      continue;
    t[i] ^= 1;
    t[i + 1] ^= 1;
  }
  for (int i = 0; i <= n; i++)
    cnt[i] = t[i];
  for (int i = 0; i < m; i++) {
    if (v[i].l > v[i].r)
      continue;
    g[v[i].l].pb({v[i].r + 1, i});
    g[v[i].r + 1].pb({v[i].l, i});
  }
  for (int i = 0; i <= n; i++) {
    for (auto to : g[i]) {
      if (getp(to.l) == getp(i))
        continue;
      f[i].pb({to.l, to.r});
      f[to.l].pb({i, to.r});
      unions(i, to.l);
    }
  }
  for (int i = 0; i <= n; i++) {
    if (cnt[getp(i)] % 2) {
      cout << -1;
      return 0;
    }
    if (t[i])
      gv[getp(i)].pb(i);
  }
  for (int i = 0; i <= n; i++)
    if (!val[i])
      dfs0(i);
  int ls = -1;
  for (int i = 0; i <= n; i++)
    if (!usd[i])
      dfs(i);
  sort(all(cv));
  cout << cv.size() << '\n';
  for (auto gg : cv)
    cout << gg << ' ';
  return 0;
}