Problem p02776 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n, m) for (int(i) = (n); (i) < (m); (i)++)
#define rrep(i, n, m) for (int(i) = (n); (i) > (m); (i)--)
using ll = long long;
const ll MOD = 1e9 + 7;

int dfs(int p, vector<vector<vector<int>>> &lis, vector<int> &visited,
        vector<int> &each, vector<int> &ans) {

  visited[p] = 1;
  int ret = each[p];

  rep(i, 0, lis[p].size()) {

    int nex = lis[p][i][0];
    int eind = lis[p][i][1];

    if (visited[nex] == 0) {
      int cat = dfs(nex, lis, visited, each, ans);
      if (cat != 0)
        ans.push_back(eind + 1);
      ret ^= cat;
    }
  }

  return ret;
}

int main() {

  int N, M;
  cin >> N >> M;
  vector<vector<int>> AB(N, vector<int>(2, 0));
  vector<int> Alis(N, 0);

  rep(i, 0, N) {
    cin >> AB[i][0] >> AB[i][1];
    Alis[i] = AB[i][0];
  }

  sort(AB.begin(), AB.end());
  sort(Alis.begin(), Alis.end());
  AB.push_back({1000000, 0});
  Alis.push_back(1000000);

  // rep(i,0,N+1){
  //     cout << AB[i][0] << " " << AB[i][1] << endl;
  // }
  //[] (vector<int>& x , vector<int>& y) {return x[0] < y[0];}

  vector<int> each(1, AB[0][1]);
  rep(i, 1, N + 1) { each.push_back(AB[i - 1][1] ^ AB[i][1]); }

  vector<vector<vector<int>>> lis(N + 1,
                                  vector<vector<int>>(0, vector<int>(2)));

  int L, R;
  int lind, rind;
  rep(i, 0, M) {
    cin >> L >> R;
    lind = distance(Alis.begin(), lower_bound(Alis.begin(), Alis.end(), L));
    rind = distance(Alis.begin(), lower_bound(Alis.begin(), Alis.end(), R + 1));

    if (lind == rind)
      continue;

    lis[lind].push_back({rind, i});
    lis[rind].push_back({lind, i});
  }

  vector<int> ans(0);
  vector<int> visited(N + 1, 0);

  rrep(i, N, -1) {
    if (visited[i] == 0) {
      int st = dfs(i, lis, visited, each, ans);

      if ((i != N) && (st != 0)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  sort(ans.begin(), ans.end());
  cout << ans.size() << endl;
  rep(i, 0, ans.size()) { cout << ans[i] << endl; }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n, m) for (int(i) = (n); (i) < (m); (i)++)
#define rrep(i, n, m) for (int(i) = (n); (i) > (m); (i)--)
using ll = long long;
const ll MOD = 1e9 + 7;

int dfs(int p, vector<vector<vector<int>>> &lis, vector<int> &visited,
        vector<int> &each, vector<int> &ans) {

  visited[p] = 1;
  int ret = each[p];

  rep(i, 0, lis[p].size()) {

    int nex = lis[p][i][0];
    int eind = lis[p][i][1];

    if (visited[nex] == 0) {
      int cat = dfs(nex, lis, visited, each, ans);
      if (cat != 0)
        ans.push_back(eind + 1);
      ret ^= cat;
    }
  }

  return ret;
}

int main() {

  int N, M;
  cin >> N >> M;
  vector<vector<int>> AB(N, vector<int>(2, 0));
  vector<int> Alis(N, 0);

  rep(i, 0, N) {
    cin >> AB[i][0] >> AB[i][1];
    Alis[i] = AB[i][0];
  }

  sort(AB.begin(), AB.end());
  sort(Alis.begin(), Alis.end());
  AB.push_back({1000000001, 0});
  Alis.push_back(1000000001);

  // rep(i,0,N+1){
  //     cout << AB[i][0] << " " << AB[i][1] << endl;
  // }
  //[] (vector<int>& x , vector<int>& y) {return x[0] < y[0];}

  vector<int> each(1, AB[0][1]);
  rep(i, 1, N + 1) { each.push_back(AB[i - 1][1] ^ AB[i][1]); }

  vector<vector<vector<int>>> lis(N + 1,
                                  vector<vector<int>>(0, vector<int>(2)));

  int L, R;
  int lind, rind;
  rep(i, 0, M) {
    cin >> L >> R;
    lind = distance(Alis.begin(), lower_bound(Alis.begin(), Alis.end(), L));
    rind = distance(Alis.begin(), lower_bound(Alis.begin(), Alis.end(), R + 1));

    if (lind == rind)
      continue;

    lis[lind].push_back({rind, i});
    lis[rind].push_back({lind, i});
  }

  vector<int> ans(0);
  vector<int> visited(N + 1, 0);

  rrep(i, N, -1) {
    if (visited[i] == 0) {
      int st = dfs(i, lis, visited, each, ans);

      if ((i != N) && (st != 0)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  sort(ans.begin(), ans.end());
  cout << ans.size() << endl;
  rep(i, 0, ans.size()) { cout << ans[i] << endl; }

  return 0;
}