luogu#P16369. [OOI 2026] Decoration

[OOI 2026] Decoration

Problem Description

During the preparation for the Closed Olympiad of School Students in Programming, the organizers decided to decorate the auditorium. For this purpose, 2n2n pegs were hammered into a straight line on the wall. All pegs are in different positions. Between them, nn strings were stretched, the ii-th of which was stretched between the pegs at distances lil_i and rir_i from the start of the wall (li<ril_i < r_i). It is known that each peg has exactly one string stretched to it.

The strings can undergo operations called restringing\textit{restringing}. In one restringing\textit{restringing} operation, you can choose two strings (li,ri)(l_i, r_i) and (lj,rj)(l_j, r_j), remove them from the pegs, and then hang two new strings, using each of the four freed pegs exactly once. That is, from the four freed pegs, two new pairs are formed, between which new strings are stretched.

The strings stretched between the pegs (li,ri)(l_i, r_i) and (lj,rj)(l_j, r_j) intersect if the segments [li,ri][l_i, r_i] and [lj,rj][l_j, r_j] have at least one common point. A configuration of strings is considered to have beauty at least kk if there exists a set of kk strings, each pair of which intersects. Note that if a configuration has beauty kk, it also has beauty at least k1, k2, , 0k - 1,\ k - 2,\ \ldots,\ 0.

The organizers of the Olympiad have qq queries for obtaining a configuration of a certain beauty as a result of several restringing\textit{restringing} operations. In the ii-th query, they would like to achieve beauty at least kik_i. For each query, the organizers want to know the minimum number of restringing\textit{restringing} operations required to achieve this. The queries are independent of each other, meaning that the restringing\textit{restringing} operations between queries are not preserved.

Input Format

The first line contains two integers nn and qq (1qn2000001 \le q \le n \le 200\,000) --- the number of strings and the number of queries.

The next nn lines describe the strings. In the ii-th line, there are two integers lil_i and rir_i (1li<ri1091 \le l_i < r_i \le 10^9) --- the numbers of the pegs connected by the ii-th string. It is guaranteed that each peg appears exactly once.

The next line contains qq integers k1k_1, k2k_2, \ldots, kqk_q (1kin1 \le k_i \le n) --- the desired beauty sizes in the queries from the organizers. It is guaranteed that all kik_i are distinct.

Output Format

For each query, output a single non-negative integer --- the minimum number of restringing\textit{restringing} operations that need to be performed to obtain a configuration of strings with the required beauty as specified in the query.

It is guaranteed that for each query, it is possible to perform several restringings to achieve a configuration of the required beauty.

6 6
25 30
15 29
7 12
8 14
4 5
16 23
1 2 3 4 5 6
0 0 1 1 2 3

Hint

Note

In the first example, the strings initially have the following configuration:

:::align{center} :::

Since strings 3 and 4 already intersect, no operations are needed to achieve beauty at least 11 and beauty at least 22.

To achieve beauties 33 and 44, you can perform a restringing\textit{restringing} operation on strings 2 and 5 to obtain strings (4,29)(4, 29) and (5,15)(5, 15).

:::align{center} :::

To achieve beauty 55, you can additionally perform an operation on strings 3 and 6 to obtain (7,16)(7, 16) and (12,23)(12, 23).

:::align{center} :::

To make all 6 strings intersect, you can perform operations on strings 1 and 5, 2 and 3, 4 and 6 to obtain the following configuration:

:::align{center} :::

Scoring

The tests for this problem consist of six groups. Points for each group are awarded only if all tests of the group and all tests of some previous groups are passed. Note that passing the tests from the statement is not required for some groups.

Group Points Additional constraints < Required groups Comment
nn qq kik_i
00 -- -- -- Tests from the statement.
11 1414 n100n \le 100 00 --
22 1616 n3000n \le 3000 0,10, 1
33 1313 -- -- Strings do not intersect pairwise.
44 2525 q=1q=1 ki=nk_i = n --
55 1717 ki10k_i \le 10
66 1515 -- 00 -- 55