CF117A Elevator

题目描述

**题意简述** 一栋楼共 $m$ 层,当中有一个神奇的电梯,它不受人控制,只会在所有楼层之间来回移动(即从 $1$ 楼一直向上到 $m$ 楼之后又从 $m$ 楼一直向下到 $1$ 楼,反复地无休止地运动),每移动一层需要 $1$ 单位时间。 现有 $n$ 个人,第 $i$ 个人在 $t_i$ 时到达电梯门口,要从 $s_i$ 楼到 $t_i$ 楼。求每个人到达应到楼层的最小时间。

输入格式

第一行包含两个整数 $n,m$($1\le n\le 10^5,2\le m\le 10^8$),意义如上所示。 接下来 $n$ 行,每行包含三个整数 $s_i,f_i,t_i$($1\le s_i,f_i\le m,0\le t_i\le 10^8$),意义同题意简述。

输出格式

输出共 $n$ 行,第 $i$ 行包含一个整数,表示第 $i$ 个人到达指定楼层的最短时间。

说明/提示

Let's consider the first sample. The first participant starts at floor $ s=2 $ by the time equal to $ t=3 $ . To get to the floor $ f=4 $ , he has to wait until the time equals $ 7 $ , that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor $ f $ at time equal to $ 9 $ . The second participant starts at the time $ t=0 $ on the floor $ s=1 $ , enters the elevator immediately, and arrives to the floor $ f=2 $ . The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts.