Monitor
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).
The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.Each of next q lines contain three integer numbers xi, yi, ti (1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 0 ≤ t ≤ 109) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.We consider that pixel is already broken at moment ti.
Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.
from collections import deque
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def slide_max(n, k, a):
    ans = []
    s = deque()
    for i in range(n):
        ai = a[i]
        while s:
            if a[s[-1]] <= ai:
                s.pop()
            else:
                break
        s.append(i)
        while s:
            if s[0] + k <= i:
                s.popleft()
            else:
                break
        if (i + 1) // k:
            ans.append(a[s[0]])
    return ans
(n, m, k, q) = map(int, input().split())
inf = pow(10, 9) + 5
a = [[inf] * m for _ in range(n)]
for _ in range(q):
    (x, y, t) = map(int, input().split())
    a[x - 1][y - 1] = t
b = [[] for _ in range(m - k + 1)]
for i in range(n):
    ai = a[i]
    s = deque()
    b0 = slide_max(m, k, ai)
    for j in range(m - k + 1):
        b[j].append(b0[j])
ans = inf
for i in range(m - k + 1):
    bi = b[i]
    s = deque()
    ans = min(ans, min(slide_max(n, k, bi)))
ans = (ans + 1) % (inf + 1) - 1
print(ans)
import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque

def SlidingMin(A: list, k: int):
    n = len(A)
    B = []
    d = deque([])
    for i in range(n):
        while d:
            if d[0] <= i - k:
                d.popleft()
            else:
                break
        while d:
            if A[d[-1]] > A[i]:
                d.pop()
            else:
                break
        d.append(i)
        if i - k + 1 >= 0:
            B.append(A[d[0]])
    return B

def SlidingMax(A: list, k: int):
    n = len(A)
    B = []
    d = deque([])
    for i in range(n):
        while d:
            if d[0] <= i - k:
                d.popleft()
            else:
                break
        while d:
            if A[d[-1]] < A[i]:
                d.pop()
            else:
                break
        d.append(i)
        if i - k + 1 >= 0:
            B.append(A[d[0]])
    return B
INF = 10 ** 18

def main():
    (n, m, k, q) = map(int, input().split())
    Q = []
    for i in range(q):
        (x, y, t) = map(int, input().split())
        (x, y) = (x - 1, y - 1)
        Q.append((x, y, t))
    S = [[-1] * m for i in range(n)]
    for (x, y, t) in Q:
        S[x][y] = t
    Max = [SlidingMax(l, k) for l in S]
    Min = [SlidingMin(l, k) for l in S]
    ans = INF
    for i in range(n - k + 1):
        for j in range(m - k + 1):
            tmax = -1
            flag = True
            for l in range(i, i + k):
                if Min[l][j] == -1:
                    flag = False
                    break
                tmax = max(tmax, Max[l][j])
            if flag:
                ans = min(ans, tmax)
    if ans != INF:
        print(ans)
    else:
        print(-1)
if __name__ == '__main__':
    main()
import sys
readline = sys.stdin.readline

class Cumsum2:

    def __init__(self, lst):
        self.H = len(lst)
        self.W = len(lst[0]) if lst else 0
        self.cumsum2 = [[0] * (self.W + 1) for i in range(self.H + 1)]
        for i in range(1, self.H + 1):
            for j in range(1, self.W + 1):
                self.cumsum2[i][j] = lst[i - 1][j - 1]
        for i in range(self.H + 1):
            for j in range(1, self.W + 1):
                self.cumsum2[i][j] += self.cumsum2[i][j - 1]
        for i in range(1, self.H + 1):
            for j in range(self.W + 1):
                self.cumsum2[i][j] += self.cumsum2[i - 1][j]

    def __getitem__(self, tpl):
        (ab, cd) = tpl

        def make_slice(ij, N):
            if type(ij) == int:
                (i, j) = (ij, ij + 1)
            else:
                (i, j) = (ij.start, ij.stop)
                if i == None or i < -N:
                    i = 0
                elif N <= i:
                    i = N
                elif i < 0:
                    i += N
                if j == None or N <= j:
                    j = N
                elif j < -N:
                    j = 0
                elif j < 0:
                    j += N
            return (i, j)
        (a, b) = make_slice(ab, self.H)
        (c, d) = make_slice(cd, self.W)
        return self.cumsum2[b][d] + self.cumsum2[a][c] - self.cumsum2[a][d] - self.cumsum2[b][c]

def Bisect_Int(ok, ng, is_ok):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok
(N, M, K, Q) = map(int, readline().split())
(X, Y, T) = ([], [], [])
for _ in range(Q):
    (x, y, t) = map(int, readline().split())
    x -= 1
    y -= 1
    X.append(x)
    Y.append(y)
    T.append(t)

def is_ok(t):
    grid = [[0] * M for i in range(N)]
    for i in range(Q):
        if T[i] <= t:
            grid[X[i]][Y[i]] = 1
    grid = Cumsum2(grid)
    for n in range(N - K + 1):
        for m in range(M - K + 1):
            if grid[n:n + K, m:m + K] == K ** 2:
                return True
    return False
inf = 1 << 30
ans = Bisect_Int(inf, -1, is_ok)
if ans == inf:
    ans = -1
print(ans)