#P9026. [CCC 2021 S4] Daily Commute
[CCC 2021 S4] Daily Commute
Problem Description
There are subway stations. Your home is at , and your school is at .
There are one-way walking paths. Each path takes one minute to travel.
In addition, there is a circular subway line that visits in order, and it is guaranteed that . Every day, there is exactly one subway train that departs from at time , and it arrives at at exactly minute .
Over the next days:
- Swap and . Note that the change is permanent.
- Query the shortest time from to . When you depart, the subway is at .
Input Format
The first line: .
The next lines: represent a one-way walking path.
The next line contains numbers: .
The next lines: , and it is guaranteed that .
Output Format
Output lines, the answer for each day.
4 3 3
1 2
3 4
4 1
1 4 3 2
3 4
4 2
3 2
1
2
3
Hint
$$3\leq N\leq 200000,0\leq W\leq 200000,1\leq D\leq 200000$$Translated from CCC2021 S4。
Please pay attention to constants.
Translated by ChatGPT 5