New Building for SIS (Codeforces 1020A)
Tags: Math, math

You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.

Input: The first line of the input contains following integers:  n: the number of towers in the building (1 ≤ n ≤ 108),  h: the number of floors in each tower (1 ≤ h ≤ 108),  a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h),  k: total number of queries (1 ≤ k ≤ 104). Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.

Output: For each query print a single integer: the minimum walking time between the locations in minutes.


Sample Inputs:
3 6 2 3 3
1 2 1 3
1 4 3 4
1 2 2 3

Sample Outputs:
1
4
2

Solution (Python):
from sys import stdin
rd = stdin.readline
(n, h, a, b, k) = map(int, rd().split())
for _ in range(k):
    (ta, fa, tb, fb) = map(int, rd().split())
    d = 0
    if ta != tb:
        if fa > b:
            d += fa - b
        elif fa < a:
            d += a - fa
        else:
            pass
        if fb > b:
            d += fb - b
            if fa < a:
                d += b - a
            elif fa < b:
                d += b - fa
            else:
                pass
        elif fb < a:
            d += a - fb
            if fa > b:
                d += b - a
            elif fa > a:
                d += fa - a
            else:
                pass
        elif fa > b:
            d += b - fb
        elif fa < a:
            d += fb - a
        else:
            d += abs(fb - fa)
    else:
        d = abs(fa - fb)
    d += abs(tb - ta)
    print(d)

Solution (Python):
import sys
input = sys.stdin.readline
(n, h, a, b, k) = map(int, input().split())
for _ in range(k):
    (ta, fa, tb, fb) = map(int, input().split())
    c = abs(tb - ta)
    if c == 0:
        c += abs(fa - fb)
    elif fa > b:
        if fb > b:
            c += fa - b + fb - b
        else:
            c += fa - fb
    elif fa < a:
        if fb < a:
            c += a - fa + a - fb
        else:
            c += fb - fa
    else:
        c += abs(fb - fa)
    print(c)

Solution (Python):
ls = list(map(int, input().rstrip().split()))
n = ls[0]
h = ls[1]
a = ls[2]
b = ls[3]
k = ls[4]
hs = []
for i in range(k):
    hs.append(list(map(int, input().rstrip().split())))
for i in hs:
    c = 0
    if i[0] != i[2]:
        if i[1] < a or i[1] > b:
            if b < i[1]:
                c = c + i[1] - b
                i[1] = b
            elif i[1] < a:
                c = c + a - i[1]
                i[1] = a
        c = c + abs(i[0] - i[2])
    c = c + abs(i[1] - i[3])
    print(c)