#P9166. [省选联考 2023] 火车站

[省选联考 2023] 火车站

Problem Description

There are nn train stations in a straight line, numbered from 11 to nn. There are mm railway tracks in total, and each track covers an interval of stations [li,ri][l_i, r_i].

At a station covered by multiple tracks, when a train passes through, it may switch tracks there. However, the train cannot turn around, and can only run in one direction (that is, it can only keep moving toward station 11 or keep moving toward station nn).

Person A starts from station xx, meaning they board any train that passes through xx (this train may also depart from station xx). This train may run on any track that covers station xx, and its direction may be toward 11 or toward nn. After boarding, Person A falls asleep, and only wakes up when the train stops at the terminal station of some track. Ask which stations Person A may finally reach.

Note: The train must travel through at least one station. Also, after switching tracks, the train will not stop immediately; instead, it will continue moving along the current track.

Input Format

The first line contains three positive integers n,m,xn, m, x, representing the number of stations, the number of tracks, and Person A’s starting station.

The next mm lines each contain two positive integers li,ril_i, r_i, representing the operating interval of a track.

Output Format

Output one line containing several positive integers separated by single spaces, representing the stations that Person A may finally reach. Output them in increasing order of station number.

7 5 4
3 4
4 6
1 3
5 7
4 6

1 3 6 7

Hint

Sample 1 Explanation

Starting from station 44, if the train takes the first track, it can reach the terminal station 33, or it can then continue along the third track to reach the terminal station 11.

Starting from station 44, if the train takes the second track, it can reach the terminal station 66, or it can switch to the fourth track at station 55 and reach the terminal station 77.

So the final output in order is 1,3,6,71, 3, 6, 7.

Constraints

For all testdata, it is guaranteed that 1n,m2×1051 \le n, m \le 2 \times 10^5, 1xn1 \le x \le n, 1li<rin1 \le l_i < r_i \le n.

Test Point n,mn, m \le Special Property
1 5050 None
2
3 50005000
4
5
6 2×1052 \times 10^5 A
7
8 None
9
10

Special Property A: It is guaranteed that x=1x = 1.

Translated by ChatGPT 5