Elevator (Codeforces 117A)
Tags: Implement, math

And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All n participants who have made it to the finals found themselves in a huge m-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?".The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then — to the 3-rd floor and so on until it reaches the m-th floor. After that the elevator moves to floor m - 1, then to floor m - 2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.For each of the n participant you are given si, which represents the floor where the i-th participant starts, fi, which represents the floor the i-th participant wants to reach, and ti, which represents the time when the i-th participant starts on the floor si.For each participant print the minimum time of his/her arrival to the floor fi. If the elevator stops on the floor si at the time ti, then the i-th participant can enter the elevator immediately. If the participant starts on the floor si and that's the floor he wanted to reach initially (si = fi), then the time of arrival to the floor fi for this participant is considered equal to ti.

Input: The first line contains two space-separated integers n and m (1 ≤ n ≤ 105, 2 ≤ m ≤ 108). Next n lines contain information about the participants in the form of three space-separated integers si fi ti (1 ≤ si, fi ≤ m, 0 ≤ ti ≤ 108), described in the problem statement.

Output: Print n lines each containing one integer — the time of the arrival for each participant to the required floor.

Note: 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.

Sample Inputs:
7 4
2 4 3
1 2 0
2 2 0
1 2 1
4 3 5
1 2 2
4 2 0

5 5
1 5 4
1 3 1
1 3 4
3 1 5
4 2 5

Sample Outputs:
9
1
0
7
10
7
5

12
10
10
8
7

Solution (Python):
(n, m) = map(int, input().split())
k = 2 * (m - 1)
p = [0] * n
for i in range(n):
    (s, f, t) = map(int, input().split())
    d = t % k
    if s < f:
        p[i] = (k if s <= d else 0) + f - 1 + t - d
    elif f < s:
        p[i] = (k if d + s > k + 1 else 0) + k + 1 - f + t - d
    else:
        p[i] = t
print('\n'.join(map(str, p)))

Solution (Python):
(a, b) = map(int, input().split())
ans = 2 * (b - 1)
ok = [0] * a
for i in range(a):
    (x, y, z) = map(int, input().split())
    if x == y:
        ok[i] = z
    elif x < y:
        if z < x:
            ok[i] = y - 1
        else:
            ok[i] = ans * ((z + ans - x) // ans) + y - 1
    else:
        k = b - 1
        x = b + 1 - x
        y = b + 1 - y
        z -= k
        if z < x:
            ok[i] = k + y - 1
        else:
            ok[i] = k + ans * ((z + ans - x) // ans) + y - 1
print(' '.join(map(str, ok)))

Solution (Python):
(n, m) = map(int, input().split())
m -= 1
a = []
for i in range(n):
    (s, f, t) = map(int, input().split())
    s -= 1
    f -= 1
    if s == f:
        a.append(t)
    elif s < f:
        c = s
        while c < t:
            c += 2 * m
        a.append(c + f - s)
    else:
        c = m * 2 - s
        while c < t:
            c += 2 * m
        a.append(c + s - f)
print('\n'.join(map(str, a)))