A - Two in the Range Editorial by Rainbow_qwq

Another Solution

First, delete all intervals that contains some other intervals. If \(L_i\le L_j\le R_j\le R_i\), mark all positions in \([L_i,L_j),(R_j,R_i]\) as \(0\), and delete \([L_i,R_i]\).

After this, let the remaining intervals be sorted as \([L_1,R_1],\dots,[L_k,R_k]\). Since no interval contains another one, we have \(L_1<L_2<\dots<L_k\) and \(R_1<R_2<\dots<R_k\).

For every position \(p\) not marked as \(0\), the intervals containing \(p\) form a consecutive range, say \(u,u+1,\dots,v\).

Construct a flow graph with vertices \(1,2,\dots,k+1\). For each such position, add an edge \((u,v+1)\) with capacity \(1\). Choosing this edge is equivalent to setting \(x_p=1\). For every \(i\), the edges crossing the cut between \(i\) and \(i+1\) are exactly the positions contained in \([L_i,R_i]\).

All intervals have sum \(2\) iff there is a flow of value \(2\) from \(1\) to \(k+1\). So the answer is Yes iff the maximum flow is at least \(2\).

Time complexity: \(O(N+M)\).