Pawns (Codeforces 1373G)
Tags: Greedy, divide and conquer

You are given a chessboard consisting of $$$n$$$ rows and $$$n$$$ columns. Rows are numbered from bottom to top from $$$1$$$ to $$$n$$$. Columns are numbered from left to right from $$$1$$$ to $$$n$$$. The cell at the intersection of the $$$x$$$-th column and the $$$y$$$-th row is denoted as $$$(x, y)$$$. Furthermore, the $$$k$$$-th column is a special column. Initially, the board is empty. There are $$$m$$$ changes to the board. During the $$$i$$$-th change one pawn is added or removed from the board. The current board is good if we can move all pawns to the special column by the followings rules:  Pawn in the cell $$$(x, y)$$$ can be moved to the cell $$$(x, y + 1)$$$, $$$(x - 1, y + 1)$$$ or $$$(x + 1, y + 1)$$$;  You can make as many such moves as you like;  Pawns can not be moved outside the chessboard;  Each cell can not contain more than one pawn. The current board may not always be good. To fix it, you can add new rows to the board. New rows are added at the top, i. e. they will have numbers $$$n+1, n+2, n+3, \dots$$$.After each of $$$m$$$ changes, print one integer — the minimum number of rows which you have to add to make the board good.

Input: The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5; 1 \le k \le n$$$) — the size of the board, the index of the special column and the number of changes respectively.Then $$$m$$$ lines follow. The $$$i$$$-th line contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le n$$$) — the index of the column and the index of the row respectively. If there is no pawn in the cell $$$(x, y)$$$, then you add a pawn to this cell, otherwise — you remove the pawn from this cell.

Output: After each change print one integer — the minimum number of rows which you have to add to make the board good.


Sample Inputs:
5 3 5
4 4
3 5
2 4
3 4
3 5

Sample Outputs:
0
1
2
2
1

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

def f(i):
    if not lazy[i]:
        return
    tree[i] += lazy[i]
    if i < l1:
        lazy[i << 1] += lazy[i]
        lazy[i << 1 ^ 1] += lazy[i]
    lazy[i] = 0
    return

def update(l, r, s):
    (q, ll, rr, i) = ([1], [0], [l1 - 1], 0)
    while len(q) ^ i:
        j = q[i]
        (l0, r0) = (ll[i], rr[i])
        if l <= l0 and r0 <= r:
            if s:
                lazy[j] += s
            f(j)
            i += 1
            continue
        f(j)
        m0 = l0 + r0 >> 1
        if l <= m0 and l0 <= r:
            q.append(j << 1)
            ll.append(l0)
            rr.append(m0)
        if l <= r0 and m0 + 1 <= r:
            q.append(j << 1 ^ 1)
            ll.append(m0 + 1)
            rr.append(r0)
        i += 1
    for i in q[::-1]:
        if i < l1:
            (j, k) = (i << 1, i << 1 ^ 1)
            f(j)
            f(k)
            tree[i] = max(tree[j], tree[k])
    return

def get_max(s, t):
    update(s, t, 0)
    s += l1
    t += l1
    ans = 0
    while s <= t:
        if s % 2:
            ans = max(ans, tree[s])
            s += 1
        s >>= 1
        if not t % 2:
            ans = max(ans, tree[t])
            t -= 1
        t >>= 1
    return ans
(n, k, m) = map(int, input().split())
l = 2 * n + 5
l1 = pow(2, l.bit_length())
l2 = 2 * l1
(tree, lazy) = ([0] * l2, [0] * l2)
for i in range(l1):
    tree[i + l1] = i
for i in range(l1 - 1, 0, -1):
    tree[i] = max(tree[2 * i], tree[2 * i + 1])
h = []
cnt = [0] * l
s = set()
ma = 0
ans = []
for _ in range(m):
    (x, y) = map(int, input().split())
    u = y + abs(x - k)
    if not (x, y) in s:
        cnt[u] += 1
        update(1, u, 1)
        s.add((x, y))
        heapq.heappush(h, -u)
    else:
        cnt[u] -= 1
        update(1, u, -1)
        s.remove((x, y))
    while h and (not cnt[-h[0]]):
        heapq.heappop(h)
    ma = 0 if not h else -h[0]
    ans0 = 0 if not ma else max(get_max(1, ma) - n - 1, 0)
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import math
(n, k, m) = [int(i) for i in input().split()]
l = math.ceil(math.log(2 * n) / math.log(2))
p = 2 ** l
memo = [0] * (2 * p)
allres = [0] * (2 * p)
exist = set()
for _i in range(m):
    (x, y) = [int(i) for i in input().split()]
    l = abs(x - k) + y
    index = l + p
    if (x, y) in exist:
        exist.remove((x, y))
        while index != 0:
            memo[index] -= 1
            index = index // 2
    else:
        exist.add((x, y))
        while index != 0:
            memo[index] += 1
            index = index // 2
    index = (l + p) // 2
    allres[l + p] = l + memo[l + p] - 1
    if memo[l + p] == 0:
        allres[l + p] = 0
    while index != 0:
        allres[index] = max(allres[index * 2] + memo[index * 2 + 1], allres[index * 2 + 1])
        index = index // 2
    print(max(allres[1] - n, 0))

Solution (Python):
import collections
import math
(n, k, m) = [int(i) for i in input().split()]
l = math.ceil(math.log(2 * n) / math.log(2))
p = 2 ** l
memo = [0] * (2 * p)
allres = [0] * (2 * p)
exist = set()
for _i in range(m):
    (x, y) = [int(i) for i in input().split()]
    l = abs(x - k) + y
    index = l + p
    if (x, y) in exist:
        exist.remove((x, y))
        while index != 0:
            memo[index] -= 1
            index = index // 2
    else:
        exist.add((x, y))
        while index != 0:
            memo[index] += 1
            index = index // 2
    index = (l + p) // 2
    allres[l + p] = l + memo[l + p] - 1
    if memo[l + p] == 0:
        allres[l + p] = 0
    while index != 0:
        allres[index] = max(allres[index * 2] + memo[index * 2 + 1], allres[index * 2 + 1])
        index = index // 2
    print(max(allres[1] - n, 0))