Kay and Eternity (Codeforces 685D)
Tags: Implement, sortings

Snow Queen told Kay to form a word "eternity" using pieces of ice. Kay is eager to deal with the task, because he will then become free, and Snow Queen will give him all the world and a pair of skates.Behind the palace of the Snow Queen there is an infinite field consisting of cells. There are n pieces of ice spread over the field, each piece occupying exactly one cell and no two pieces occupying the same cell. To estimate the difficulty of the task Kay looks at some squares of size k × k cells, with corners located at the corners of the cells and sides parallel to coordinate axis and counts the number of pieces of the ice inside them.This method gives an estimation of the difficulty of some part of the field. However, Kay also wants to estimate the total difficulty, so he came up with the following criteria: for each x (1 ≤ x ≤ n) he wants to count the number of squares of size k × k, such that there are exactly x pieces of the ice inside.Please, help Kay estimate the difficulty of the task given by the Snow Queen.

Input: The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 300) — the number of pieces of the ice and the value k, respectively. Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — coordinates of the cell containing i-th piece of the ice. It's guaranteed, that no two pieces of the ice occupy the same cell.

Output: Print n integers: the number of squares of size k × k containing exactly 1, 2, ..., n pieces of the ice.


Sample Inputs:
5 3
4 5
4 6
5 5
5 6
7 7

Sample Outputs:
10 8 1 4 0

Solution (Python):
import sys
import bisect as bs
input = sys.stdin.readline
(n, k) = map(int, input().split())
a = []
for _ in range(n):
    (x, y) = map(int, input().split())
    a.append((x, y))

def sub4():
    (sk, u) = ([], [])
    for (x, y) in a:
        sk.append((x - k + 1, y, 1))
        sk.append((x + 1, y, -1))
        u.append(y - k)
        u.append(y)
    u = sorted(list(set(u)))
    sk.sort()
    (ans, cnt, pr) = ([0] * (n + n + 2), [0] * (n + n + 2), [0] * (n + n + 2))
    for (x, y, flag) in sk:
        l = bs.bisect_left(u, y - k) + 1
        r = bs.bisect_left(u, y)
        for j in range(l, r + 1):
            ans[cnt[j]] += (x - pr[j]) * (u[j] - u[j - 1])
            cnt[j] += flag
            pr[j] = x
    print(' '.join((str(ans[i]) for i in range(1, n + 1))))
sub4()

Solution (Python):
import sys
import bisect as bs
input = sys.stdin.readline
(n, k) = map(int, input().split())
a = []
for _ in range(n):
    (x, y) = map(int, input().split())
    a.append((x, y))

def sub4():
    (sk, u) = ([], [])
    for (x, y) in a:
        sk.append((x - k + 1, y, 1))
        sk.append((x + 1, y, -1))
        u.append(y - k)
        u.append(y)
    u = sorted(list(set(u)))
    sk.sort()
    (ans, cnt, pr) = ([0] * (n + n + 2), [0] * (n + n + 2), [0] * (n + n + 2))
    for (x, y, flag) in sk:
        l = bs.bisect_left(u, y - k) + 1
        r = bs.bisect_left(u, y)
        for j in range(l, r + 1):
            ans[cnt[j]] += (x - pr[j]) * (u[j] - u[j - 1])
            cnt[j] += flag
            pr[j] = x
    for i in range(1, n + 1):
        print(ans[i], end=' ')
sub4()

Solution (Python):
import bisect as bs
(n, k) = map(int, input().split())
a = []
for _ in range(n):
    (x, y) = map(int, input().split())
    a.append((x, y))

def sub4():
    (sk, u) = ([], [])
    for (x, y) in a:
        sk.append((x - k + 1, y, 1))
        sk.append((x + 1, y, -1))
        u.append(y - k)
        u.append(y)
    u = sorted(list(set(u)))
    sk.sort()
    (ans, cnt, pr) = ([0] * (n + n + 2), [0] * (n + n + 2), [0] * (n + n + 2))
    for (x, y, flag) in sk:
        l = bs.bisect_left(u, y - k) + 1
        r = bs.bisect_left(u, y)
        for j in range(l, r + 1):
            ans[cnt[j]] += (x - pr[j]) * (u[j] - u[j - 1])
            cnt[j] += flag
            pr[j] = x
    for i in range(1, n + 1):
        print(ans[i], end=' ')
sub4()