Case of Chocolate (Codeforces 556E)
Tags: Others, binary search

Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.A bar of chocolate can be presented as an n × n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.After each action, he wants to know how many pieces he ate as a result of this action.

Input: The first line contains integers n (1 ≤ n ≤ 109) and q (1 ≤ q ≤ 2·105) — the size of the chocolate bar and the number of actions.Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≤ xi, yi ≤ n, xi + yi = n + 1) — the numbers of the column and row of the chosen cell and the character that represents the direction (L — left, U — up).

Output: Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.

Note: Pictures to the sample tests:The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.

Sample Inputs:
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U

10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U

Sample Outputs:
4
3
2
1
2

9
1
10
6
0
2

Solution (Python):
import sys
from bisect import bisect

def input():
    return sys.stdin.readline().strip()

def solve():
    (n, q) = map(int, input().split())
    was = set()
    Q = [None] * q
    all = [0] * (2 * q)
    for i in range(q):
        (x, y, t) = input().split()
        (x, y) = (int(x), int(y))
        Q[i] = (x, y, t)
        all[2 * i] = x
        all[2 * i + 1] = y
    all.sort()
    i = 0
    p = -1
    for j in range(2 * q):
        v = all[j]
        if v != p:
            all[i] = v
            i += 1
        p = v
    sz = i
    all = all[:sz]
    V = [0] * (2 * sz)
    H = [0] * (2 * sz)
    for (x, y, t) in Q:
        if (x, y) in was:
            print(0)
        else:
            was.add((x, y))
            if t == 'L':
                TA = H
                TB = V
            else:
                (x, y) = (y, x)
                TA = V
                TB = H
            v = bisect(all, y) - 1 + sz
            r = 0
            while v > 0:
                r = max(r, TA[v])
                v //= 2
            c = x - r
            print(c)
            r = bisect(all, x) - 1 + sz
            l = bisect(all, x - c) + sz
            while l <= r:
                if l % 2 == 1:
                    TB[l] = max(TB[l], y)
                if r % 2 == 0:
                    TB[r] = max(TB[r], y)
                l = (l + 1) // 2
                r = (r - 1) // 2
solve()

Solution (Python):
import sys
from bisect import bisect

def input():
    return sys.stdin.readline().strip()

def solve():
    (n, q) = map(int, input().split())
    was = set()
    Q = [None] * q
    all = [0] * (2 * q)
    for i in range(q):
        (x, y, t) = input().split()
        (x, y) = (int(x), int(y))
        Q[i] = (x, y, t)
        all[2 * i] = x
        all[2 * i + 1] = y
    all.sort()
    V = [0] * (4 * q)
    H = [0] * (4 * q)
    for (x, y, t) in Q:
        if (x, y) in was:
            print(0)
        else:
            was.add((x, y))
            if t == 'L':
                TA = H
                TB = V
            else:
                (x, y) = (y, x)
                TA = V
                TB = H
            v = bisect(all, y) - 1 + q + q
            r = 0
            while v > 0:
                r = max(r, TA[v])
                v //= 2
            c = x - r
            print(c)
            r = bisect(all, x) - 1 + q + q
            l = bisect(all, x - c) + q + q
            while l <= r:
                if l % 2 == 1:
                    TB[l] = max(TB[l], y)
                if r % 2 == 0:
                    TB[r] = max(TB[r], y)
                l = (l + 1) // 2
                r = (r - 1) // 2
solve()

Solution (Python):
import sys
from bisect import bisect

def input():
    return sys.stdin.readline().strip()

def solve():
    (n, q) = map(int, input().split())
    was = set()
    Q = [None] * q
    all = [0] * (2 * q)
    for i in range(q):
        (x, y, t) = input().split()
        (x, y) = (int(x), int(y))
        Q[i] = (x, y, t)
        all[2 * i] = x
        all[2 * i + 1] = y
    all.sort()
    i = 0
    p = -1
    F = dict()
    for j in range(2 * q):
        v = all[j]
        if v != p:
            all[i] = v
            F[v] = i
            i += 1
        p = v
    sz = i
    all = all[:sz]
    V = [0] * (2 * sz)
    H = [0] * (2 * sz)
    for (x, y, t) in Q:
        if (x, y) in was:
            print(0)
        else:
            was.add((x, y))
            if t == 'L':
                TA = H
                TB = V
            else:
                (x, y) = (y, x)
                TA = V
                TB = H
            v = F[y] + sz
            r = 0
            while v > 0:
                r = max(r, TA[v])
                v //= 2
            c = x - r
            print(c)
            r = F[x] + sz
            l = bisect(all, x - c) + sz
            while l <= r:
                if l % 2 == 1:
                    TB[l] = max(TB[l], y)
                if r % 2 == 0:
                    TB[r] = max(TB[r], y)
                l = (l + 1) // 2
                r = (r - 1) // 2
solve()