QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#6311. Train Station

Statistics

There are $n$ train stations arranged in a straight line, numbered from $1$ to $n$. There are $m$ train tracks, each covering a range of stations $[l_i, r_i]$.

For a station covered by multiple train tracks, a train passing through it can change tracks. However, trains cannot turn around; they can only travel in one direction (i.e., either continuously towards $1$ or continuously towards $n$).

Little A starts at station $x$, meaning they board any train passing through $x$ (this train could also be starting from station $x$). This train may be traveling on any track that covers station $x$, and its direction of travel can be either towards $1$ or towards $n$. After boarding, Little A falls asleep and only wakes up when the train reaches the terminal station of the current line. Determine all possible stations where Little A could end up.

Note: A train must travel through at least one station, and it does not stop immediately after switching tracks; it continues to move along the current track.

Input

The first line contains three positive integers $n, m, x$, representing the number of train stations, the number of train tracks, and Little A's initial starting point, respectively.

The next $m$ lines each contain two positive integers $l_i, r_i$, representing the range of stations covered by a train track.

Output

Output a single line containing several positive integers separated by single spaces, representing the possible stations where Little A could end up, sorted in ascending order.

Examples

Input 1

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

Output 1

1 3 6 7

Note 1

Starting from station $4$, the train can travel along the first track to reach terminal $3$, or continue along the third track to reach terminal $1$.

Starting from station $4$, the train can travel along the second track to reach terminal $6$, or switch to the fourth track at station $5$ to reach terminal $7$.

Thus, the final output in ascending order is $1, 3, 6, 7$.

Examples 2-4

See the files station/station2.in and station/station2.ans, station/station3.in and station/station3.ans, and station/station4.in and station/station4.ans in the contestant's directory.

Constraints

For all data, it is guaranteed that $1 \le n, m \le 2 \times 10^5$; $1 \le x \le n$; $1 \le l_i < r_i \le n$.

Test Case $n, m \le$ Special Property
1 $50$ None
2
3
4 $5000$ None
5
6 $2 \times 10^5$ A
7
8 None
9
10

Special Property A: Guaranteed $x = 1$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.