Logo
(追記) (追記ここまで)

27994번 - Passport 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB116373536.082%

문제

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_i , L_{i + 1}, \dots , R_i$. Here, it is guaranteed that the traveler can enter the country where the passport was issued. Namely, $L_i ≤ i ≤ 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 $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_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 $N$ countries, and, if it is possible, calculates the minimum number of passports he needs to get.

입력

Read the following data from the standard input.

$N$

$L_1$ $R_1$

$L_2$ $R_2$

$\vdots$

$L_N$ $R_N$

$Q$

$X_1$

$X_2$

$\vdots$

$X_Q$

출력

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_j$. 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.

제한

  • 2ドル ≤ N ≤ 200,000円$.
  • 1ドル ≤ L_i ≤ i ≤ R_i ≤ N$ (1ドル ≤ i ≤ N$).
  • 1ドル ≤ Q ≤ N$.
  • 1ドル ≤ X_j ≤ N$ (1ドル ≤ j ≤ Q$).
  • $X_j < X_{j+1}$ (1ドル ≤ j ≤ Q - 1$).
  • Given values are all integers.

서브태스크

번호배점제한
16

$Q = 1,ドル $X_1 = 1$.

216

$N ≤ 300,ドル $Q = 1$.

324

$N ≤ 2,500円,ドル $Q = 1$.

48

$N ≤ 2,500円$.

546

No additional constraints.

예제 입력 1

4
1 3
2 4
2 3
4 4
1
1

예제 출력 1

2

Assume that your friend lives in the country $X_1 = 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.

예제 입력 2

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

예제 출력 2

4

Assume that your friend lives in the country $X_1 = 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.

예제 입력 3

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

예제 출력 3

-1
2
1
2
-1

For example, if your friend lives in the country $X_3 = 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_5 = 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.

예제 입력 4

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

예제 출력 4

-1
-1
-1
-1

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

힌트

출처

Camp > JOI Spring Training Camp > JOI 2022/2023 Spring Training Camp 1-3번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /