Language: C++

#include <bits/stdc++.h>

using namespace std;

#ifdef DGC
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second

struct treap
{
	struct node
	{
		node *ch[2];
		int key, mn, id, prio, sz;

		node(int key, int id) : key(key), mn(key), id(id)
		{
			ch[0] = ch[1] = NULL;
			sz = 1;
			prio = rand();
		}
	}*root;

	node* new_node(int key, int id)
	{
		return new node(key, id);
	}

	int size(node *u)
	{
		return u ? u->sz : 0;
	}

	int mn(node *u)
	{
		return u ? u->mn : (1<<30);
	}

	node* update(node *u)
	{
		if (u)
		{
			u->sz = size(u->ch[0]) + size(u->ch[1]) + 1;
			u->mn = min({ u->key, mn(u->ch[0]), mn(u->ch[1]) });
		}
		return u;
	}

	pair<node*, node*> split(node* u, int k)
	{// split for the kth first elements
		if (!u)
			return { u, u };

		if (size(u->ch[0]) >= k)
		{
			auto s = split(u->ch[0], k);
			u->ch[0] = s.second;
			return { s.first, update(u) };
		}

		auto s = split(u->ch[1], k - size(u->ch[0]) - 1);
		u->ch[1] = s.first;
		return { update(u), s.second };
	}

	node* merge(node *u, node *v)
	{
		if (!u || !v)
			return u ? u : v;

		if (u->prio > v->prio)
		{
			u->ch[1] = merge(u->ch[1], v);
			return update(u);
		}

		v->ch[0] = merge(u, v->ch[0]);
		return update(v);
	}

	pair<int, int> go(node *u)
	{
		int r = 1;
		while (true)
		{
			if (u->key == u->mn)
			{
				r += size(u->ch[0]);
				return { r, u->id };
			}
			if (mn(u->ch[1]) == u->mn)
			{
				r += size(u->ch[0]) + 1;
				u = u->ch[1];
				continue;
			}
			u = u->ch[0];
		}
		assert(0);
	}

	treap() : root(NULL) {}
};


int main()
{
	#ifdef DGC
		//freopen("a.in", "r", stdin);
		//freopen("b.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int n, m;
	cin >> n >> m;
	vector<pair<int, int>> a(n);
	for (auto &i : a) cin >> i.F >> i.S;
	sort(a.begin(), a.end());

	treap t;
	vector<treap::node*> v(n+1, NULL);
	for (int l, r, id = 0; id < m; ++id)
	{
		cin >> l >> r;
		l = lower_bound(a.begin(), a.end(), make_pair(l, 0)) - a.begin();
		r = upper_bound(a.begin(), a.end(), make_pair(r, 1)) - a.begin() - 1;
		if (l <= r) v[l] = t.merge(v[l], t.new_node(r, id));
	}

	vector<int> b(n, -1), id(n);
	for (int i = 0; i < n; ++i)
	{
		while (v[i] != NULL)
		{
			int x = t.mn(v[i]);
			if (x >= i) break;
			int k = t.go(v[i]).F;
			auto s1 = t.split(v[i], k);
			auto s2 = t.split(s1.F, k-1);
			v[i] = t.merge(s2.F, s1.S);
		}

		if (v[i] == NULL) continue;
		int x = t.mn(v[i]);
		b[i] = x;
		id[i] = t.go(v[i]).S;
		v[x+1] = t.merge(v[x+1], v[i]);
	}

	vector<int> ans;
	vector<int> dp(n+1);
	for (int i = 0; i < n; ++i)
	{
		if (i) dp[i] += dp[i-1];
		if (a[i].S != dp[i] % 2)
		{
			if (b[i] == -1)
			{
				cout << "-1\n";
				return 0;
			}
			++dp[i];
			--dp[b[i]+1];
			ans.push_back(id[i]);
		}
	}

	cout << ans.size() << "\n";
	sort(ans.begin(), a.end());
	for (auto &i : ans)
		cout << i+1 << " \n"[&i==&ans.back()];

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:177:13: error: no matching function for call to 'sort(std::vector<int>::iterator, std::vector<std::pair<int, int> >::iterator)'
  177 |         sort(ans.begin(), a.end());
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:4762:5: note: candidate: 'template<class _RAIter> void std::sort(_RAIter, _RAIter)'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4762:5: note:   template argument deduction/substitution failed:
a.cc:177:13: note:   deduced conflicting types for parameter '_RAIter' ('__gnu_cxx::__normal_iterator<int*, std::vector<int> >' and '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >')
  177 |         sort(ans.begin(), a.end());
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note: candidate: 'template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)'
 4793 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare)'
  292 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator)'
  296 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note:   candidate expects 3 arguments, 2 provided