luogu#P8391. [BalticOI 2022] Event Hopping (Day1)

[BalticOI 2022] Event Hopping (Day1)

Problem Description

There are nn intervals. The ii-th interval is [li,ri][l_i, r_i].

You can hop between intervals. When you are at interval xx, you can hop to an interval yy that covers the right endpoint rxr_x, i.e., you can hop from xx to yy if and only if rx[ly,ry]r_x \in [l_y, r_y].

There are qq queries. In each query, you start at interval sis_i and need to hop to interval tit_i. You need to output the minimum number of hops required. If it is impossible to reach it, output impossible.

Input Format

The first line contains two integers n,qn, q.

The next nn lines each contain two integers li,ril_i, r_i.

The next qq lines each contain two integers si,tis_i, t_i.

Output Format

Output qq lines. On the ii-th line, output the answer to the ii-th query. If there is no solution, output impossible.

5 2
1 3
2 4
4 7
7 9
3 7
1 4
3 2
2
impossible

8 5
1 2
3 4
1 5
6 7
5 10
10 20
15 20
999999999 1000000000
1 6
1 7
2 4
3 3
5 8

3
4
impossible
0
impossible

Hint

  • Subtask 11 (1010 points): Each interval can hop to at most one other interval.
  • Subtask 22 (1010 points): n1000n \le 1000, q100q \le 100.
  • Subtask 33 (1515 points): n5000n \le 5000.
  • Subtask 44 (1515 points): q100q \le 100.
  • Subtask 55 (2020 points): There do not exist two intervals i,ji, j such that [li,ri][lj,rj][l_i, r_i] \subseteq [l_j, r_j].
  • Subtask 66 (3030 points): No special constraints.

For all testdata, 1n,q1000001 \le n, q \le 100000, 1li<ri1091 \le l_i < r_i \le 10^9, 1si,tin1 \le s_i, t_i \le n.

Translated by ChatGPT 5