Problem p02776 (C++)

Buggy submission (status: Runtime Error):
// Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me

#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()

using LL = long long;
int vis[100005];
vector<int> ans;

namespace _buff {

const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  return getchar();
  //    if (ib == ie) {
  //        ib = ibuf;
  //        ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  //    }
  //    return ib == ie ? -1 : *ib++;
}

} // namespace _buff

LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}

using P = pair<int, int>;
vector<vector<P>> g(100005 + 1);
int b[100005];
#define fi first
#define se second

void dfs(int x) {
  vis[x] = 1;
  for (auto u : g[x]) {
    if (vis[u.first])
      continue;
    dfs(u.first);
    if (b[u.first]) {
      ans.push_back(u.second);
      b[u.first] ^= 1;
      b[x] ^= 1;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<P> bomb;
  vector<int> lsh;
  for (int i = 0; i < n; ++i) {
    int a = read(), b = read();
    bomb.emplace_back(a, b);
    lsh.push_back(a);
  }
  sort(all(lsh));
  sort(begin(bomb), end(bomb));
  for (int i = 0; i < n; i++) {
    b[i] = bomb[i].second;
  }
  for (int i = n; i >= 0; i--) {
    b[i] ^= (i ? b[i - 1] : 0);
  }
  for (int i = 1; i <= m; ++i) {
    int l, r;
    cin >> l >> r;
    l = lower_bound(all(lsh), l) - lsh.begin();
    r = upper_bound(all(lsh), r) - lsh.begin();
    g[l].push_back(make_pair(r, i));
    g[r].push_back(make_pair(l, i));
  }

  for (int i = 0; i <= n; i++) {
    if (vis[i])
      continue;
    dfs(i);
    if (b[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  sort(all(ans));
  cout << ans.size() << endl;
  for (auto x : ans) {
    cout << x << ' ';
  }
  cout << endl;
  return 0;
}


Fixed submission:
// Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me

#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()

using LL = long long;
int vis[100005];
vector<int> ans;

namespace _buff {

const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  return getchar();
  //    if (ib == ie) {
  //        ib = ibuf;
  //        ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  //    }
  //    return ib == ie ? -1 : *ib++;
}

} // namespace _buff

LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}

using P = pair<int, int>;
vector<vector<P>> g(100005 + 1);
int b[100005];
#define fi first
#define se second

void dfs(int x) {
  vis[x] = 1;
  for (auto u : g[x]) {
    if (vis[u.first])
      continue;
    dfs(u.first);
    if (b[u.first]) {
      ans.push_back(u.second);
      b[u.first] ^= 1;
      b[x] ^= 1;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<P> bomb;
  vector<int> lsh;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    bomb.emplace_back(a, b);
    lsh.push_back(a);
  }
  sort(all(lsh));
  sort(begin(bomb), end(bomb));
  for (int i = 0; i < n; i++) {
    b[i] = bomb[i].second;
  }
  for (int i = n; i >= 0; i--) {
    b[i] ^= (i ? b[i - 1] : 0);
  }
  for (int i = 1; i <= m; ++i) {
    int l, r;
    cin >> l >> r;
    l = lower_bound(all(lsh), l) - lsh.begin();
    r = upper_bound(all(lsh), r) - lsh.begin();
    g[l].push_back(make_pair(r, i));
    g[r].push_back(make_pair(l, i));
  }

  for (int i = 0; i <= n; i++) {
    if (vis[i])
      continue;
    dfs(i);
    if (b[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  sort(all(ans));
  cout << ans.size() << endl;
  for (auto x : ans) {
    cout << x << ' ';
  }
  cout << endl;
  return 0;
}