No.1742 Binary Indexed Train
タグ : / 解いたユーザー数 98
作問者 :
nok0
👑
Kazun
Problem Statement
On the line of "Yuki Railway", which connects the east and west of the Country of YukiCoder, $2^N + 1$ stations are lined up in a straight line from west to east, and the stations are numbered $0, 1, 2, \dots 2^N$ from the west.
Yuki Railway operates $N + 1$ types of trains, which are named Train $0$, Train $1$, $\dots$, Train $N$.
For $i$ such that $ 0 \leq i \leq N $, Train $i$ stops only at stations whose numbers are multiples of $2^i$.
All trains run from west to east, starting at station $0$ and terminating at station $2^N$. There are no trains running from east to west.
Passengers of Yuki Railway can board any type of train that stops at their current station. They can also get off the train they are on when it stops. They may board and get off any number of times.
Today, $Q$ passengers will use Yuki Railway. For $i$ such that $ 1 \leq i \leq Q $, the $i$-th passenger wants to travel from station $S_i$ to station $T_i$.
For each passenger, solve the following problem.
-
Problem: When transferring optimally from the departure station $S_i$ to the destination station $T_i$, find the minimum possible number of stations stopped at during the trip, including the destination.
Constraints
- All inputs are integers.
- $ 1 \leq N \leq 60 $
- $ 1 \leq Q \leq 10^5$
- $ 0 \leq S_i < T_i \leq 2^N $
Input
The input is given from standard input in the following format.$N$ $Q$ $S_1$ $T_1$ $S_2$ $T_2$ $\vdots$ $S_Q$ $T_Q$
The $1$st line contains $N, Q$ separated by a space.
The following $Q$ lines, starting from line $2$, contain $S_i, T_i$ separated by a space.
Output
Output $Q$ lines. The $i$-th line should contain the answer to the problem for the $i$-th passenger. Print a newline at the end.
Sample
Sample 1
Input
3 4 1 7 3 6 2 3 5 8
Output
4 2 1 2
The figure above shows stations $0, 1, 2 ... 8$ from left to right, and, from bottom to top, the stations where Trains $0$, $1$, $2$, and $3$ stop are painted black.
The optimal routes of passengers $1, 2, 3, 4$ are shown by the orange, light blue, yellow, and purple lines, respectively.
The route of each passenger when traveling optimally is shown below.
- Passenger $1$ (orange): station $1$ → station $2$ → station $4$ → station $6$ → station $7$
- Passenger $2$ (light blue): station $3$ → station $4$ → station $6$
- Passenger $3$ (yellow): station $2$ → station $3$
- Passenger $4$ (purple): station $5$ → station $6$ → station $8$
Note that the departure station is not included in the answer, and that there is no way to move to a station with a smaller number than the current station.
Sample 2
Input
1 2 0 2 1 2
Output
1 1
Sample 3
Input
32 5 510479020 902533247 494473032 2564665701 1753815 4290493471 3925045722 4058246871 1578437576 3200987099
Output
29 30 43 29 36
The input may not fit in a 32-bit integer type. Please be careful.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。