Under construction

Still being built. Things may change

Passport

Time limit2sMemory limit1024 MB

Summary
Given each country's passport range [L_i, R_i], for each start country X_j, find the fewest passports needed to visit all N countries, or -1 if impossible.
Level

Medium7 of 10

Topics
Intervals, Greedy, Graph
Solved
No attempts yet

Problem

A passport is a certificate that travelers use to enter foreign countries around the world.

There are NN countries on a planet, numbered from 11 to NN. Each country issues its own passport. A traveler holding the passport issued by country ii can enter countries Li,Li+1,…,RiL_i, L_{i+1}, \dots, R_i. It is guaranteed that a traveler can enter the country that issued the passport. That is, Li≤i≤RiL_i \le i \le R_i.

Your friend likes to travel. At first he has no passport. He wants to visit all NN countries by repeating the following two actions.

  • Get a passport issued by the country where he is currently staying.
  • Move to a country he can enter with a passport he currently holds.

You do not know where he lives, so there are QQ candidate countries X1,X2,…,XQX_1, X_2, \dots, X_Q. For each candidate, determine whether he can visit all NN countries. If he can, find the minimum number of passports he needs to get.

Input

The first line contains NN. Each of the next NN lines contains LiL_i and RiR_i. The next line contains QQ. Each of the last QQ lines contains XjX_j.

Output

Print QQ lines. The jj-th line is the minimum number of passports needed when he lives in XjX_j, or −1-1 if he cannot visit all NN countries.

Constraints

2≤N≤200 0002 \le N \le 200\,000

1≤Li≤i≤Ri≤N1 \le L_i \le i \le R_i \le N (1≤i≤N1 \le i \le N)

1≤Q≤N1 \le Q \le N

1≤Xj≤N1 \le X_j \le N (1≤j≤Q1 \le j \le Q)

Xj<Xj+1X_j < X_{j+1} (1≤j≤Q−11 \le j \le Q-1)

All given values are integers.

Examples4

  1. Example 1

    Input
    4
    1 3
    2 4
    2 3
    4 4
    1
    1
    
    Expected output
    2
    
  2. Example 2

    Input
    5
    1 5
    2 4
    2 3
    3 5
    1 5
    1
    3
    
    Expected output
    4
    
  3. Example 3

    Input
    5
    1 1
    2 3
    1 5
    3 4
    5 5
    5
    1
    2
    3
    4
    5
    
    Expected output
    -1
    2
    1
    2
    -1
    
  4. Example 4

    Input
    4
    1 2
    1 2
    3 4
    3 4
    4
    1
    2
    3
    4
    
    Expected output
    -1
    -1
    -1
    -1