Rorororobot (Codeforces 1709D)
Tags: Greedy, binary search

There is a grid, consisting of $$$n$$$ rows and $$$m$$$ columns. The rows are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns are numbered from $$$1$$$ to $$$m$$$ from left to right. The $$$i$$$-th column has the bottom $$$a_i$$$ cells blocked (the cells in rows $$$1, 2, \dots, a_i$$$), the remaining $$$n - a_i$$$ cells are unblocked.A robot is travelling across this grid. You can send it commands — move up, right, down or left. If a robot attempts to move into a blocked cell or outside the grid, it explodes.However, the robot is broken — it executes each received command $$$k$$$ times. So if you tell it to move up, for example, it will move up $$$k$$$ times ($$$k$$$ cells). You can't send it commands while the robot executes the current one.You are asked $$$q$$$ queries about the robot. Each query has a start cell, a finish cell and a value $$$k$$$. Can you send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command $$$k$$$ times?The robot must stop in the finish cell. If it visits the finish cell while still executing commands, it doesn't count.

Input: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^9$$$; $$$1 \le m \le 2 \cdot 10^5$$$) — the number of rows and columns of the grid.The second line contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$0 \le a_i \le n$$$) — the number of blocked cells on the bottom of the $$$i$$$-th column.The third line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries.Each of the next $$$q$$$ lines contain five integers $$$x_s, y_s, x_f, y_f$$$ and $$$k$$$ ($$$a[y_s] < x_s \le n$$$; $$$1 \le y_s \le m$$$; $$$a[y_f] < x_f \le n$$$; $$$1 \le y_f \le m$$$; $$$1 \le k \le 10^9$$$) — the row and the column of the start cell, the row and the column of the finish cell and the number of times each your command is executed. The start and the finish cell of each query are unblocked.

Output: For each query, print "YES" if you can send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command $$$k$$$ times. Otherwise, print "NO".


Sample Inputs:
11 10
9 0 0 10 3 4 8 11 10 8
6
1 2 1 3 1
1 2 1 3 2
4 3 4 5 2
5 3 11 5 3
5 3 11 5 2
11 9 9 10 1

Sample Outputs:
YES
NO
NO
NO
YES
YES

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write

class SparseTable(list):

    def __init__(self, arr, key):
        n = len(arr)
        self.key = key
        super().__init__([[*arr]])
        self.pow2 = [1]
        gap = 1
        oldlst = arr[:]
        while gap < n:
            leave = gap << 1
            self.pow2.append(leave)
            newlst = [key(oldlst[i], oldlst[i + gap]) for i in range(n - leave + 1)]
            self.append(newlst)
            oldlst = newlst
            gap = leave

    def getVal(self, start, stop):
        p = (stop - start + 1).bit_length() - 1
        return self.key(self[p][start], self[p][stop - self.pow2[p] + 1])
NY = ('NO\n', 'YES\n')
(nR, nC) = map(int, input().split())
arr = (*map(int, input().split()),)
stmax = SparseTable(arr, key=max)
ans = []
for _ in range(int(input())):
    (rs, cs, rf, cf, k) = map(int, input().split())
    if abs(rs - rf) % k or abs(cs - cf) % k or rs <= arr[cs - 1] or (rf <= arr[cf - 1]):
        ans.append('NO\n')
    else:
        r0 = nR - (nR - rs) % k
        if cs > cf:
            rm = stmax.getVal(cf - 1, cs - 1)
        else:
            rm = stmax.getVal(cs - 1, cf - 1)
        ans.append(NY[rm < r0])
print(''.join(ans))

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write

class SparseTable(list):

    def __init__(self, arr, key):
        n = len(arr)
        self.key = key
        super().__init__([[*arr]])
        self.pow2 = [1]
        gap = 1
        oldlst = arr[:]
        while gap < n:
            leave = gap << 1
            self.pow2.append(leave)
            newlst = [key(oldlst[i], oldlst[i + gap]) for i in range(n - leave + 1)]
            self.append(newlst)
            oldlst = newlst
            gap = leave

    def getVal(self, start, stop):
        p = (stop - start + 1).bit_length() - 1
        return self.key(self[p][start], self[p][stop - self.pow2[p] + 1])
NY = ('NO\n', 'YES\n')
(nR, nC) = map(int, input().split())
arr = (*map(int, input().split()),)
stmax = SparseTable(arr, key=max)
ans = []
for _ in range(int(input())):
    (rs, cs, rf, cf, k) = map(int, input().split())
    if abs(rs - rf) % k or abs(cs - cf) % k or rs <= arr[cs - 1] or (rf <= arr[cf - 1]):
        ans.append('NO\n')
    else:
        r0 = nR - (nR - rs) % k
        if cs > cf:
            rm = stmax.getVal(cf - 1, cs - 1)
        else:
            rm = stmax.getVal(cs - 1, cf - 1)
        ans.append(NY[rm < r0])
print(''.join(ans))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def sparse_table(a):
    table = []
    (s0, l) = (a, 1)
    table.append(s0)
    while 2 * l <= len(a):
        s = [max(s0[i], s0[i + l]) for i in range(len(s0) - l)]
        table.append(list(s))
        s0 = s
        l *= 2
    return table

def get_max(l, r, table):
    d = (r - l + 1).bit_length() - 1
    ans = max(table[d][l], table[d][r - pow2[d] + 1])
    return ans
(n, m) = map(int, input().split())
a = [0] + list(map(int, input().split()))
table = sparse_table(a)
pow2 = [1]
for _ in range(20):
    pow2.append(2 * pow2[-1])
q = int(input())
ans = []
for _ in range(q):
    (xs, ys, xf, yf, k) = map(int, input().split())
    if abs(xs - xf) % k or abs(ys - yf) % k:
        ans0 = 'NO'
        ans.append(ans0)
        continue
    if ys > yf:
        (xs, xf) = (xf, xs)
        (ys, yf) = (yf, ys)
    d = (n - xs) // k * k
    x = xs + d
    ans0 = 'YES' if x > get_max(ys, yf, table) else 'NO'
    ans.append(ans0)
sys.stdout.write('\n'.join(ans))