Conveyor Belts (Codeforces 1811B)
Tags: Math, implementation

Conveyor matrix $$$m_n$$$ is matrix of size $$$n \times n$$$, where $$$n$$$ is an even number. The matrix consists of concentric ribbons moving clockwise.In other words, the conveyor matrix for $$$n = 2$$$ is simply a matrix $$$2 \times 2$$$, whose cells form a cycle of length $$$4$$$ clockwise. For any natural $$$k \ge 2$$$, the matrix $$$m_{2k}$$$ is obtained by adding to the matrix $$$m_{2k - 2}$$$ an outer layer forming a clockwise cycle.  The conveyor matrix $$$8 \times 8$$$.   You are standing in a cell with coordinates $$$x_1, y_1$$$ and you want to get into a cell with coordinates $$$x_2, y_2$$$. A cell has coordinates $$$x, y$$$ if it is located at the intersection of the $$$x$$$th row and the $$$y$$$th column.Standing on some cell, every second you will move to the cell next in the direction of movement of the tape on which you are. You can also move to a neighboring cell by spending one unit of energy. Movements happen instantly and you can make an unlimited number of them at any time.Your task is to find the minimum amount of energy that will have to be spent to get from the cell with coordinates $$$x_1, y_1$$$ to the cell with coordinates $$$x_2, y_2$$$.For example, $$$n=8$$$ initially you are in a cell with coordinates $$$1,3$$$ and you want to get into a cell with coordinates $$$6, 4$$$. You can immediately make $$$2$$$ movements, once you are in a cell with coordinates $$$3, 3$$$, and then after $$$8$$$ seconds you will be in the right cell.

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^5$$$) — the number of test cases.The descriptions of the test cases follow.The description of each test case consists of one string containing five integers $$$n$$$, $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$1 \le x_1, y_1, x_2, y_2 \le n \le 10^9$$$) — matrix size and the coordinates of the start and end cells. It is guaranteed that the number $$$n$$$ is even.

Output: For each test case, print one integer in a separate line — the minimum amount of energy that will have to be spent to get from the cell with coordinates $$$x_1, y_1$$$ to the cell with coordinates $$$x_2, y_2$$$.


Sample Inputs:
5
2 1 1 2 2
4 1 4 3 3
8 1 3 4 6
100 10 20 50 100
1000000000 123456789 987654321 998244353 500000004

Sample Outputs:
0
1
2
9
10590032

Solution (Python):
from bisect import bisect_left, bisect_right
from itertools import permutations
from math import ceil, log, factorial
import os
from collections import deque, Counter
import sys
from heapq import heapify, heappop, heappush
from io import BytesIO, IOBase
sys.setrecursionlimit(10000)

def updateBIT(BITree, n, index, val):
    index = index + 1
    while index <= n:
        BITree[index] += val
        index += index & -index

def constructBITree(arr, n):
    BITree = [0] * (n + 1)
    for i in range(n):
        updateBIT(BITree, n, i, arr[i])
    return BITree

def getSum(BITree, index):
    sum = 0
    index = index + 1
    while index > 0:
        sum += BITree[index]
        index -= index & -index
    return sum

def update(BITree, l, r, n, val):
    updateBIT(BITree, n, l, val)
    updateBIT(BITree, n, r + 1, -val)

def gcd(a, b):
    (a, b) = (max(a, b), min(a, b))
    if b == 0:
        return a
    return gcd(b, a % b)

def lcm(a, b):
    (a, b) = (max(a, b), min(a, b))
    return a // gcd(a, b) * b

def power(x, y, p):
    res = 1
    x = x % p
    if x == 0:
        return 0
    while y > 0:
        if y & 1 == 1:
            res = res * x % p
        y = y >> 1
        x = x * x % p
    return res

def primeFactors(n):
    arr = []
    c = 2
    while n > 1:
        if n % c == 0:
            arr.append(c)
            n = n / c
        else:
            c = c + 1
    return arr

def solution(a, b, n):
    i = 0
    while i * a <= n:
        if (n - i * a) % b == 0:
            if i >= 0 and int((n - i * a) / b) >= 0:
                return True
        i = i + 1
    return False

def main():
    for i in range(int(input())):
        (n, x1, y1, x2, y2) = map(int, input().split())
        pos1 = min(x1, y1, n - x1 + 1, n - y1 + 1)
        pos2 = min(x2, y2, n - x2 + 1, n - y2 + 1)
        print(abs(pos1 - pos2))
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import heapq
import math
input = sys.stdin.readline
from collections import Counter, deque, defaultdict

def sieve_of_eratosthenes(n):
    sieve = [True] * (n + 1)
    (sieve[0], sieve[1]) = (False, False)
    primes = []
    for current in range(2, int(n ** 0.5) + 1):
        if sieve[current]:
            primes.append(current)
            for multiple in range(current * current, n + 1, current):
                sieve[multiple] = False
    for current in range(int(n ** 0.5) + 1, n + 1):
        if sieve[current]:
            primes.append(current)
    return primes

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def count_divisible_by(num, limit):
    return limit // num

def sum_range(a, b):
    return b * (b + 1) // 2 - a * (a - 1) // 2

def Yes():
    print('Yes')

def No():
    print('No')

def solve():
    (n, x1, y1, x2, y2) = invr()
    layer_a = 0
    layer_b = 0
    if x1 + y1 <= n + 1:
        a = min(x1, y1)
        layer_a = a
    else:
        a = max(x1, y1)
        layer_a = n - a + 1
    if x2 + y2 <= n + 1:
        b = min(x2, y2)
        layer_b = b
    else:
        b = max(x2, y2)
        layer_b = n - b + 1
    diff = abs(layer_a - layer_b)
    print(diff)

def main():
    for _ in range(inp()):
        solve()
main()

Solution (Python):
import sys
input = sys.stdin.readline
import math
t = int(input())
for _ in range(t):
    (n, x1, y1, x2, y2) = list(map(int, input().split()))
    first = second = 0
    print(abs(min([x1, y1, n + 1 - x1, n + 1 - y1]) - min([x2, y2, n + 1 - x2, n + 1 - y2])))