#SRD0000149. Ốc sên Julia (Julia the Snail)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ốc sên Julia (Julia the Snail)
Julia the Snail
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Julia is a snail on a trunk of height . She may slide downward freely. Igor attaches ropes: rope has lower endpoint at height and upper endpoint at . Julia may climb a rope from its lower endpoint to its upper endpoint but cannot leave it midway. All upper endpoints are pairwise distinct.
There are independent queries. For each , Julia starts at height and is never allowed below or above . Determine the highest trunk height she can reach by combining the permitted downward slides and upward rope climbs.
Input
- First line: integer , the trunk height.
- Second line: integer , the number of ropes.
- The next lines: for rope .
- Next line: integer , the number of queries.
- Next lines: for each query.
Output
Print lines, one maximum reachable trunk height for each query.
Subtasks
- Subtask 1 (20 points): ; , ; ; all distinct; .
- Subtask 2 (30 points): ; , ; ; all distinct; .
- Subtask 3 (50 points): ; , ; ; all distinct; .
Examples
Example 1
Input
8
4
1 2
3 4
2 5
6 7
5
1 2
1 4
1 6
2 7
6 8
Output
2
2
5
5
7
Explanation
At start with ceiling , rope yields . With ceiling , height is unreachable, so the maximum remains . With ceiling , ropes then reach . From with ceiling , Julia can reach but cannot get to , the lower endpoint of . From with ceiling , the last rope reaches .