).
In the next m
lines, each contains two integers xi
and yi
(1≤xi,yi≤n,xi≠yi),
which means i-th
person is in bus station xi
and wants goto bus station yi.
(1≤i≤m)
What else you should know is that for the i-th
person, the bus starts at bus station ((i?1) mod n)+1
and drives to right. When the bus arrives at station
n,
it will turn around and drive from right to left. Similarly, When the bus arrives at station
1,
it will turn around and drive from left to right. You can assume that the bus drives one meter per second. And you should only consider the time that the bus drives and ignore the others.
Output
For each person, you should output one integer which is the minimum time needed before arriving bus station
yi.
Sample Input
1
7 3
2 3 4 3 4 5
1 7
4 5
5 4
Sample Output
21
10
28
HintFor the first person, the bus starts at bus station 1, and the person takes in bus at time 0. After 21 seconds, the bus arrives at bus station 7. So the time needed is 21 seconds. For the second person, the bus starts at bus station 2. After 7 seconds, the bus arrives at bus station 4 and the person takes in the bus. After 3 seconds, the bus arrives at bus station 5. So the time needed is 10 seconds. For the third person, the bus starts at bus station 3. After 7 seconds, the bus arrives at bus station 5 and the person takes in the bus. After 9 seconds, the bus arrives at bus station 7 and the bus turns around. After 12 seconds, the bus arrives at bus station 4. So the time needed is 28 seconds.
Source
BestCoder Round #27
Recommend
hujie | We have carefully selected several similar problems for you: 5165 5164 5161 5160 5157
處理一個前綴和就行
/*************************************************************************
> File Name: bc27b.cpp
> Author: ALex
> Mail: [email protected]
> Created Time: 2015年01月24日 星期六 19時11分51秒
************************************************************************/
#include