Problem p02776 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int SIZEN = 100010;
int a[SIZEN], b[SIZEN], c[SIZEN];
int s[SIZEN];
int N, M;
struct Link {
  int to, next, id;
} link[SIZEN << 2];
int head[SIZEN] = {0}, tot = 0;
void add(int from, int to, int id) {
  link[++tot].to = to;
  link[tot].id = id;
  link[tot].next = head[from];
  head[from] = tot;
}
bool used[SIZEN] = {0};
bool vis[SIZEN] = {0};
bool dfs(int x) {
  vis[x] = true;
  bool t = c[x];
  // printf("c[%d]=%d\n",x,c[x]);
  for (int i = head[x]; i; i = link[i].next) {
    if (!vis[link[i].to] && dfs(link[i].to)) {
      t ^= 1;
      used[link[i].id] = true;
    }
  }
  return t;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &a[i], &b[i]);
    s[i] = a[i];
  }
  sort(s + 1, s + 1 + N);
  for (int i = 1; i <= N; i++) {
    a[i] = lower_bound(s + 1, s + 1 + N, a[i]) - s;
    c[a[i]] = b[i];
  }
  for (int i = N + 1; i >= 1; i--)
    c[i] ^= c[i - 1];
  for (int i = 1; i <= M; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l = lower_bound(s + 1, s + 1 + N, l) - s;
    r = upper_bound(s + 1, s + 1 + N, r) - s - 1;
    if (l > r)
      continue;
    add(l, r + 1, i);
    add(r + 1, l, i);
  }
  for (int i = 1; i <= N + 1; i++) {
    if (!vis[i] && dfs(i)) {
      puts("-1");
      return 0;
    }
  }
  int ans = 0;
  for (int i = 1; i <= M; i++)
    ans += used[i];
  printf("%d\n", ans);
  for (int i = 1; i <= M; i++)
    if (used[i])
      printf("%d ", i);
  printf("\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int SIZEN = 200010;
int a[SIZEN], b[SIZEN], c[SIZEN];
int s[SIZEN];
int N, M;
struct Link {
  int to, next, id;
} link[SIZEN << 2];
int head[SIZEN] = {0}, tot = 0;
void add(int from, int to, int id) {
  link[++tot].to = to;
  link[tot].id = id;
  link[tot].next = head[from];
  head[from] = tot;
}
bool used[SIZEN] = {0};
bool vis[SIZEN] = {0};
bool dfs(int x) {
  vis[x] = true;
  bool t = c[x];
  // printf("c[%d]=%d\n",x,c[x]);
  for (int i = head[x]; i; i = link[i].next) {
    if (!vis[link[i].to] && dfs(link[i].to)) {
      t ^= 1;
      used[link[i].id] = true;
    }
  }
  return t;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &a[i], &b[i]);
    s[i] = a[i];
  }
  sort(s + 1, s + 1 + N);
  for (int i = 1; i <= N; i++) {
    a[i] = lower_bound(s + 1, s + 1 + N, a[i]) - s;
    c[a[i]] = b[i];
  }
  for (int i = N + 1; i >= 1; i--)
    c[i] ^= c[i - 1];
  for (int i = 1; i <= M; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l = lower_bound(s + 1, s + 1 + N, l) - s;
    r = upper_bound(s + 1, s + 1 + N, r) - s - 1;
    if (l > r)
      continue;
    add(l, r + 1, i);
    add(r + 1, l, i);
  }
  for (int i = 1; i <= N + 1; i++) {
    if (!vis[i] && dfs(i)) {
      puts("-1");
      return 0;
    }
  }
  int ans = 0;
  for (int i = 1; i <= M; i++)
    ans += used[i];
  printf("%d\n", ans);
  for (int i = 1; i <= M; i++)
    if (used[i])
      printf("%d ", i);
  printf("\n");
  return 0;
}