| IU Programming Challenge 2024 |
|---|
| Закончено |
It's a new semester, and the first class of the day is in a building you've never been to. Fortunately for you, the building is on the same street as your apartment!
The street has $$$n$$$ bus stops. You live at stop $$$1$$$, and the building is at stop $$$n$$$. There are $$$k$$$ bus routes, where the $$$i$$$th bus route goes back and forth between stops $$$L_i$$$ and $$$R_i$$$. You can get on or off the $$$i$$$th bus at any stop $$$x$$$ such that $$$L_i \leq x \leq R_i$$$. If you are not on any bus, and you are currently at stop $$$x$$$, you can also get to stop $$$y$$$ by walking a distance of $$$|x - y|$$$ blocks.
What is the minimum number of blocks that you'd have to walk to get to the building from your apartment?
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2\cdot10^5$$$, $$$0 \leq k \leq 2\cdot10^5$$$) — the number of bus stops and the number of bus routes.
Each of the following $$$k$$$ lines contains two integers $$$L_i$$$ and $$$R_i$$$ ($$$1 \leq L_i \lt R_i \leq n$$$) — the endpoints of the $$$i$$$th bus route.
For each test case, output the minimum number of blocks that you must walk to go from your apartment to the building.
43 11 35 22 33 410 41 34 65 79 101 0
0 2 3 0
| Название |
|---|


