2023-sp_Passport — JOI JOI_spring
Task type: batch | Time limit: 2 s | Memory limit: 1024 MB | Difficulty: 21
Algorithms: shortest path, segment tree

![](_page_0_Picture_0.jpeg)

# Passport

Passport is a certificate which is used worldwide when a traveler enters foreign countries.

In a planet, there are *N* countries, numbered from 1 to *N*. Each country issues a passport. When a traveler has a passport issued by the country *i* (1 ≤ *i* ≤ *N*), the traveler can enter the countries *L<sub>i</sub>*, *L<sub>i</sub>* + 1, . . . , *R<sub>i</sub>*. Here, it is guaranteed that the traveler can enter the country where the passport was issued. Namely, *L<sub>i</sub>* ≤ *i* ≤ *R<sub>i</sub>* is satisfied.

You have a friend who likes traveling. Although he dreams of traveling around the world, he does not have a passport in the beginning. Thus, he plans to visit all of the *N* countries by repeating the following two actions:

- He gets a passport issued by the country where he is currently staying.
- He moves to a country where he can enter using a passport he currently has.

When you hear about his plan, you are wondering whether it is possible to realize the plan, and, if it is possible, what is the minimum number of passports he needs to get. Since you do not know where he lives, you consider *Q* possible countries *X*<sub>1</sub>, *X*<sub>2</sub>, . . . , *X*<sub>Q</sub> where he lives.

Write a program which, given information of the passports and the possibilities of his living place, for each possibility, determines whether it is possible for him to visit all of the *N* countries, and, if it is possible, calculates the minimum number of passports he needs to get.

# Input

Read the following data from the standard input:

```
N
L1 R1
L2 R2
:
LN RN
Q
X1
X2
:
Xρ
```

![](_page_1_Picture_0.jpeg)

# Output

Write *Q* lines to the standard output. The *j*-th line (1 ≤ *j* ≤ *Q*) corresponds to the case where your friend lives in the country *X<sub>j</sub>*. If it is possible for him to visit all of the *N* countries, this line should contain the minimum number of passports he needs to get. Otherwise, this line should contain -1.

# Constraints

- 2 ≤ *N* ≤ 200 000.
- 1 ≤ *L<sub>i</sub>* ≤ *i* ≤ *R<sub>i</sub>* ≤ *N* (1 ≤ *i* ≤ *N*).
- 1 ≤ *Q* ≤ *N*.
- 1 ≤ *X<sub>j</sub>* ≤ *N* (1 ≤ *j* ≤ *Q*).
- *X<sub>j</sub>* < *X<sub>j+1</sub>* (1 ≤ *j* ≤ *Q* − 1).
- Given values are all integers.

# Subtasks

- 1\. (6 points) *Q* = 1, *X*<sub>1</sub> = 1.
- 2\. (16 points) *N* ≤ 300, *Q* = 1.
- 3\. (24 points) *N* ≤ 2 500, *Q* = 1.
- 4\. (8 points) *N* ≤ 2 500.
- 5\. (46 points) No additional constraints.

# Sample Input and Output

| Sample Input 1 | Sample Output 1 |
| :------------- | :------------- |
| 4              | 2              |
| 1 3            |                |
| 2 4            |                |
| 2 3            |                |
| 4 4            |                |
| 1              |                |
| 1              |                |

![](_page_2_Picture_0.jpeg)

Assume that your friend lives in the country *X*<sub>1</sub> = 1. It is possible for him to visit all of the 4 countries if he acts in the following way. Then, he gets 2 passports.

- 1\. He gets a passport issued by the country 1.
- 2\. Using the passport issued by the country 1, he moves to the country 2.
- 3\. He gets a passport issued by the country 2.
- 4\. Using the passport issued by the country 1, he moves to the country 3.
- 5\. Using the passport issued by the country 2, he moves to the country 4.

Since it is impossible to realize the plan if he gets less than or equal to 1 passport, output 2. This sample input satisfies the constraints of all the subtasks.

| Sample Input 2 | Sample Output 2 |
| :------------- | :------------- |
| 5              | 4              |
| 1 5            |                |
| 2 4            |                |
| 2 3            |                |
| 3 5            |                |
| 1 5            |                |
| 1              |                |
| 3              |                |

Assume that your friend lives in the country *X*<sub>1</sub> = 3. It is possible for him to visit all of the 5 countries if he acts in the following way. Then, he gets 4 passports.

- 1\. He gets a passport issued by the country 3.
- 2\. Using the passport issued by the country 3, he moves to the country 2.
- 3\. He gets a passport issued by the country 2.
- 4\. Using the passport issued by the country 2, he moves to the country 4.
- 5\. He gets a passport issued by the country 4.
- 6\. Using the passport issued by the country 4, he moves to the country 5.
- 7\. He gets a passport issued by the country 5.
- 8\. Using the passport issued by the country 5, he moves to the country 1.

Since it is impossible to realize the plan if he gets less than or equal to 3 passports, output 4. This sample input satisfies the constraints of Subtasks 2, 3, 4, 5.

![](_page_3_Picture_0.jpeg)

| Sample Input 3 | Sample Output 3 |
| :------------- | :------------- |
| 5              |                |
| 1 1            | -1             |
| 2 3            | 2              |
| 1 5            | 1              |
| 3 4            | 2              |
| 5 5            | -1             |
| 5              |                |
| 1              |                |
| 2              |                |
| 3              |                |
| 4              |                |
| 5              |                |

For example, if your friend lives in the country *X*<sub>3</sub> = 3, it is possible to realize the plan if he gets a passport issued by the country 3, and uses it to visit the countries 1, 2, 4, 5 in this order. Therefore, output 1 in the third line.

On the other hand, if your friend lives in the country *X*<sub>5</sub> = 5, it is impossible for him to enter other countries even if he gets a passport issued by the country 5. Thus, he cannot realize the plan. Therefore, output -1 in the fifth line.

This sample input satisfies the constraints of Subtasks 4, 5.

| Sample Input 4 | Sample Output 4 |
| :------------- | :------------- |
| 4              |                |
| 1 2            | -1             |
| 1 2            | -1             |
| 3 4            | -1             |
| 3 4            | -1             |
| 4              |                |
| 1              |                |
| 2              |                |
| 3              |                |
| 4              |                |

This sample input satisfies the constraints of Subtasks 4, 5.

Starter code: