Segment Covering
Contest: Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)
Difficulty rating: 3200
Tags: brute force, combinatorics, constructive algorithms, data structures, dp, trees

ChthollyNotaSeniorious gives DataStructures a number axis with $$$m$$$ distinct segments on it. Let $$$f(l,r)$$$ be the number of ways to choose an even number of segments such that the union of them is exactly $$$[l,r]$$$, and $$$g(l,r)$$$ be the number of ways to choose an odd number of segments such that the union of them is exactly $$$[l,r]$$$.

ChthollyNotaSeniorious asked DataStructures $$$q$$$ questions. In each query, ChthollyNotaSeniorious will give DataStructures two numbers $$$l, r$$$, and now he wishes that you can help him find the value $$$f(l,r)-g(l,r)$$$ modulo $$$998\,244\,353$$$ so that he wouldn't let her down.

Input format:
The first line of input contains two integers $$$m$$$ ($$$1 \leq m \leq 2 \cdot 10^5$$$) and $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^5$$$)  — the number of segments and queries, correspondingly.

The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i < y_i \leq 10^9$$$), denoting a segment $$$[x_i, y_i]$$$.

It is guaranteed that all segments are distinct. More formally, there do not exist two numbers $$$i, j$$$ with $$$1 \le i < j \le m$$$ such that $$$x_i = x_j$$$ and $$$y_i = y_j$$$.

The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i < r_i \leq 10^9$$$), describing a query.

Output format:
For each query, output a single integer  — $$$f(l_i,r_i)-g(l_i,r_i)$$$ modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first query, we have to find $$$f(1, 4) - g(1, 4)$$$. The only subset of segments with union $$$[1, 4]$$$ is $$$\{[1, 3], [2, 4]\}$$$, so $$$f(1, 4) = 1, g(1, 4) = 0$$$.

In the second query, we have to find $$$f(1, 5) - g(1, 5)$$$. The only subsets of segments with union $$$[1, 5]$$$ are $$$\{[1, 3], [2, 4], [3, 5]\}$$$ and $$$\{[1, 3], [3, 5]\}$$$, so $$$f(1, 5) = 1, g(1, 5) = 1$$$.

Example input:
4 2
1 3
4 6
2 4
3 5
1 4
1 5

Example output:
1
0

Editorial:
Hint 1: If there exist two segments $$$(l_1, r_1), (l_2, r_2)$$$ such that $$$l_1 \le l_2 \le r_2 \le r_1$$$ and we choose $$$(l_1, r_1)$$$, number of ways of choosing $$$(l_2, r_2)$$$ at the same time will be equal to that of not choosing. Hence if we choose $$$(l_1, r_1)$$$, the signed number of ways will be $$$0$$$. So we can delete $$$(l_1, r_1)$$$.
Hint 2: It can be proved that the absolute value of every $$$f_{l,r} - g_{l,r}$$$ doesn't exceed $$$1$$$.
Proof: First, find segments $$$(l_i, r_i)$$$ which are completely contained by $$$(l, r)$$$.
Let us sort $$$(l_i, r_i)$$$ in ascending order of $$$l_i$$$. As there does not exist a segment that contains another, $$$r_i$$$ are also sorted.
Assume that $$$l_2 < l_3 \leq r_1$$$ and $$$r_3 \geq r_2$$$. If we choose segments $$$1$$$ and $$$3$$$, choosing $$$2$$$ or not will be the same except for the sign. So $$$3$$$ is useless. So we can delete such useless segments. After the process, $$$l_3 > r_1, l_4 > r_2, \cdots$$$ will be held. If $$$[l_1, r_1] \cup [l_2, r_2] \cup \cdots \cup [l_k, r_k] = [l, r]$$$, answer will be $$$(-1)^k$$$, else answer will be $$$0$$$.
This picture shows what the segments eventually are like:
For $$$[l_i, r_i]$$$, we can find the lowest $$$j$$$ such that $$$l_j > r_j$$$ and construct a tree by linking such $$$i$$$ and $$$j$$$. Then the LCA of $$$1$$$ and $$$2$$$ will be $$$5$$$, where the answer becomes 0.
So we can get the answer quickly by simply finding the LCA of two segments.