#P9331. [JOISC 2023 Day1] Passport

[JOISC 2023 Day1] Passport

题目描述

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

In a planet, there are NN countries, numbered from 11 to NN. Each country issues a passport. When a traveler has a passport issued by the country i (1iN)i \ (1 \le i \le N), the traveler can enter the countries Li,Li+1,,RiL_i, L_{i + 1}, \dots, R_i. Here, it is guaranteed that the traveler can enter the country where the passport was issued. Namely, LiiRiL_i \le i \le R_i 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 NN 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 QQ possible countries X1,X2,,XQX_1, X_2, \dots, X_Q 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 NN countries, and, if it is possible, calculates the minimum number of passports he needs to get.

输入格式

Read the following data from the standard input.

NN

L1 R1L_1 \ R_1

L2 R2L_2 \ R_2

\vdots

LN RNL_N \ R_N

QQ

X1X_1

X2X_2

\vdots

XQX_Q

输出格式

Write QQ lines to the standard output. The jj-th line (1jQ)(1 \le j \le Q) corresponds to the case where your friend lives in the country XjX_j. If it is possible for him to visit all of the NN countries, this line should contain the minimum number of passports he needs to get. Otherwise, this line should contain 1-1.

题目大意

题目描述

护照是旅行家进入他国时使用的证件。

在一个星球上有 NN 个国家,从 11NN 编号。每个国家都签发一种护照。当旅行家获得由国家i (1iN)i \ (1 \le i \le N) 签发的护照后,他能够进入国家 Li,Li+1,,RiL_i, L_{i + 1}, \dots, R_i这里保证旅行家能够进入其签证的签发国。形式上地说, LiiRiL_i \le i \le R_i 必然成立。

你有一个爱旅行的朋友。即使他奢望能环游世界,但他最初一种护照也没有。因此,他想通过一下重复以下两项行为来环游这 NN 个国家。

  • 获得他当前所在国家签发的护照。
  • 用他现有的护照进入某个国家。

知道他的计划后,你想知道这个计划的是否可行,和如果可行的话,他最少需要的护照数量。因为你并不清楚他现在身处何国,所以你预测了 QQ 个他可能正居住在那的国家 X1,X2,,XQX_1, X_2, \dots, X_Q

现在给定各国护照的信息 Li,RiL_i, R_i 和他可能居住的 QQ 个国家,您需要写一个程序,对于每一个可能居住的国家,判断他是否可能环游这 NN 个国家,如果可能的话,计算出他需要的最少护照种数。

输入格式

从标准输入读入:

NN

L1 R1L_1 \ R_1

L2 R2L_2 \ R_2

\vdots

LN RNL_N \ R_N

QQ

X1X_1

X2X_2

\vdots

XQX_Q

输出格式

输出 QQ 行至标准输出,第 j (1jQ)j \ (1 \le j \le Q) 行一个整数代表若你的朋友位于国家 XjX_j 的答案。若他能环游这 NN 个国家,则输出他需要的最少护照种数,否则输出 1-1

提示

【样例解释 #1】

假设你的朋友居住在国家 X1=1X_1 = 1,一种可行的方式如下,最后他获得了 22 种护照:

  1. 获得国家 11 签发的护照。
  2. 用国家 11 签发的护照去国家 22 旅行。
  3. 获得国家 22 签发的护照。
  4. 用国家 11 签发的护照去国家 33 旅行。
  5. 用国家 22 签发的护照去国家 44 旅行。

可以证明不存在使用护照种数更小的方案,故输出 22

该样例满足所有子任务的限制。

【样例解释 #2】

假设你的朋友居住在国家 X1=3X_1 = 3。一种可行的方式如下,最后他获得了 44 种护照:

  1. 获得国家 33 签发的护照。
  2. 用国家 33 签发的护照去国家 22 旅行。
  3. 获得国家 22 签发的护照。
  4. 用国家 22 签发的护照去国家 44 旅行。
  5. 获得国家 44 签发的护照。
  6. 用国家 44 签发的护照去国家 55 旅行。
  7. 获得国家 55 签发的护照。
  8. 用国家 55 签发的护照去国家 11 旅行。

可以证明不存在使用护照种数更小的方案,故输出 44

该样例满足子任务 252 \sim 5 的限制。

【样例解释 #3】

例如,如果你的朋友居住在 X3=3X_3 = 3,一种可行的方案书获得国家 33 签发的护照,并用它来依次去国家 1,2,4,51, 2, 4, 5 旅行。故第三行输出 33

但如果你的朋友居住在国家 X5=5X_5 = 5,即使他获得了国家 55 签发的护照,他也不可能进入任何其他国家,因此,他无法实现自己的旅行计划。故第五行输出 1-1

该样例满足子任务 454 \sim 5 的限制。

【样例解释 #4】

该样例满足子任务 454 \sim 5 的限制。

4
1 3
2 4
2 3
4 4
1
1

2

5
1 5
2 4
2 3
3 5
1 5
1
3

4

5
1 1
2 3
1 5
3 4
5 5
5
1
2
3
4
5

-1
2
1
2
-1

4
1 2
1 2
3 4
3 4
4
1
2
3
4

-1
-1
-1
-1

提示

【样例解释 #1】

Assume that your friend lives in the country X1=1X_1 = 1. It is possible for him to visit all of the 44 countries if he acts in the following way. Then, he gets 22 passports.

  1. He gets a passport issued by the country 11.
  2. Using the passport issued by the country 11, he moves to the country 22.
  3. He gets a passport issued by the country 22.
  4. Using the passport issued by the country 11, he moves to the country 33.
  5. Using the passport issued by the country 22, he moves to the country $44.

Since it is impossible to realize the plan if he gets less than or equal to 11 passport, output 22.

该样例满足所有子任务的限制。

【样例解释 #2】

Assume that your friend lives in the country X1=3X_1 = 3. It is possible for him to visit all of the 55 countries if he acts in the following way. Then, he gets 44 passports.

  1. He gets a passport issued by the country 33.
  2. Using the passport issued by the country 33, he moves to the country 22.
  3. He gets a passport issued by the country 22.
  4. Using the passport issued by the country 22, he moves to the country 44.
  5. He gets a passport issued by the country 44.
  6. Using the passport issued by the country 44, he moves to the country 55.
  7. He gets a passport issued by the country 55.
  8. Using the passport issued by the country 55, he moves to the country 11.

Since it is impossible to realize the plan if he gets less than or equal to 33 passports, output 44.

该样例满足子任务 252 \sim 5 的限制。

【样例解释 #3】

For example, if your friend lives in the country X3=3X_3 = 3, it is possible to realize the plan if he gets a passport issued by the country 33, and uses it to visit the countries 1,2,4,51, 2, 4, 5 in this order. Therefore, output 11 in the third line.

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

该样例满足子任务 454 \sim 5 的限制。

【样例解释 #4】

该样例满足子任务 454 \sim 5 的限制。

【数据范围】

对于所有测试数据,满足 2N2×1052 \le N \le 2 \times 10 ^ 51LiiRiN1 \le L_i \le i \le R_i \le N1QN1 \le Q \le N1X1<X2<<XQN1 \le X_1 < X_2 < \dots < X_Q \le N,保证所有输入均为整数。

子任务编号 分值 限制
11 66 Q=1Q = 1X1=1X_1 = 1
22 1616 N300N \le 300Q=1Q = 1
33 2424 N2500N \le 2500Q=1Q = 1
44 88 N2500N \le 2500
55 4646