Divide Square (Codeforces 1401E)
Tags: Implement, geometry

There is a square of size $$$10^6 \times 10^6$$$ on the coordinate plane with four points $$$(0, 0)$$$, $$$(0, 10^6)$$$, $$$(10^6, 0)$$$, and $$$(10^6, 10^6)$$$ as its vertices.You are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square.Now you are wondering how many pieces this square divides into after drawing all segments. Write a program calculating the number of pieces of the square.

Input: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n, m \le 10^5$$$) — the number of horizontal segments and the number of vertical segments.The next $$$n$$$ lines contain descriptions of the horizontal segments. The $$$i$$$-th line contains three integers $$$y_i$$$, $$$lx_i$$$ and $$$rx_i$$$ ($$$0 < y_i < 10^6$$$; $$$0 \le lx_i < rx_i \le 10^6$$$), which means the segment connects $$$(lx_i, y_i)$$$ and $$$(rx_i, y_i)$$$.The next $$$m$$$ lines contain descriptions of the vertical segments. The $$$i$$$-th line contains three integers $$$x_i$$$, $$$ly_i$$$ and $$$ry_i$$$ ($$$0 < x_i < 10^6$$$; $$$0 \le ly_i < ry_i \le 10^6$$$), which means the segment connects $$$(x_i, ly_i)$$$ and $$$(x_i, ry_i)$$$.It's guaranteed that there are no two segments on the same line, and each segment intersects with at least one of square's sides.

Output: Print the number of pieces the square is divided into after drawing all the segments.

Note:  The sample is like this:  

Sample Inputs:
3 3
2 3 1000000
4 0 4
3 0 1000000
4 0 1
2 0 5
3 1 1000000

Sample Outputs:
7

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

class BIT:

    def __init__(self, n):
        self.bit = [0] * (n + 1)
        self.n = n

    def update(self, idx, val):
        idx += 1
        while idx < self.n:
            self.bit[idx] += val
            idx += idx & -idx

    def prefixSum(self, idx):
        idx += 1
        ans = 0
        while idx > 0:
            ans += self.bit[idx]
            idx -= idx & -idx
        return ans

def solve():
    (n, m) = readInts()
    (H, V) = ([readList() for _ in range(n)], [readList() for _ in range(m)])
    H.sort()
    ans = 1
    for i in range(n):
        if H[i][1] == 0 and H[i][2] == 10 ** 6:
            ans += 1
    V.sort(key=lambda x: (x[2], -x[1]))
    (bit_l, bit_r) = (BIT(10 ** 6 + 1), BIT(10 ** 6 + 1))
    V2 = []
    (j, cnt) = (0, 0)
    for i in range(m):
        if V[i][2] == 10 ** 6 and V[i][1] > 0:
            V2.append(V[i])
            continue
        elif V[i][2] == 10 ** 6 and V[i][1] == 0:
            ans += 1
        while j < n and H[j][0] <= V[i][2]:
            if H[j][2] == 10 ** 6:
                bit_l.update(H[j][1], 1)
            elif H[j][1] == 0:
                bit_r.update(H[j][2], 1)
                cnt += 1
            j += 1
        ans += bit_l.prefixSum(V[i][0]) + cnt - bit_r.prefixSum(V[i][0] - 1)
    (bit_l, bit_r) = (BIT(10 ** 6 + 1), BIT(10 ** 6 + 1))
    H = H[::-1]
    j = cnt = 0
    m2 = len(V2)
    for i in range(m2):
        while j < n and H[j][0] >= V2[i][1]:
            if H[j][2] == 10 ** 6:
                bit_l.update(H[j][1], 1)
            elif H[j][1] == 0:
                bit_r.update(H[j][2], 1)
                cnt += 1
            j += 1
        ans += bit_l.prefixSum(V2[i][0]) + cnt - bit_r.prefixSum(V2[i][0] - 1)
    return ans
print(solve())

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

class BIT:

    def __init__(self, n):
        self.bit = [0] * (n + 1)
        self.n = n

    def update(self, idx, val):
        idx += 1
        while idx < self.n:
            self.bit[idx] += val
            idx += idx & -idx

    def prefixSum(self, idx):
        idx += 1
        ans = 0
        while idx > 0:
            ans += self.bit[idx]
            idx -= idx & -idx
        return ans

def solve():
    (n, m) = readInts()
    (H, V) = ([readList() for _ in range(n)], [readList() for _ in range(m)])
    bit_l = BIT(10 ** 6 + 1)
    bit_r = BIT(10 ** 6 + 1)
    V2 = []
    H.sort()
    H.append([10 ** 6, 0, 10 ** 6])
    n += 1
    ans = 0
    for i in range(n):
        if H[i][1] == 0 and H[i][2] == 10 ** 6:
            ans += 1
    V.sort(key=lambda x: (x[2], -x[1]))
    (j, cnt) = (0, 0)
    for i in range(m):
        if V[i][2] == 10 ** 6 and V[i][1] > 0:
            V2.append(V[i])
            continue
        while j < n and H[j][0] <= V[i][2]:
            if H[j][2] == 10 ** 6:
                bit_l.update(H[j][1], 1)
            elif H[j][1] == 0:
                bit_r.update(H[j][2], 1)
                cnt += 1
            j += 1
        ans += bit_l.prefixSum(V[i][0]) + cnt - bit_r.prefixSum(V[i][0] - 1)
    (bit_l, bit_r) = (BIT(10 ** 6 + 1), BIT(10 ** 6 + 1))
    H.pop()
    H = H[::-1] + [[0, 0, 10 ** 6]]
    j = cnt = 0
    m2 = len(V2)
    for i in range(m2):
        while j < n and H[j][0] >= V2[i][1]:
            if H[j][2] == 10 ** 6:
                bit_l.update(H[j][1], 1)
            elif H[j][1] == 0:
                bit_r.update(H[j][2], 1)
                cnt += 1
            j += 1
        ans += bit_l.prefixSum(V2[i][0]) + cnt - bit_r.prefixSum(V2[i][0] - 1)
    return ans
print(solve())

Solution (Python):
import sys
from collections import defaultdict
input = sys.stdin.buffer.readline
(fw, n) = ([0] * 1000002, 1000001)

def fw_add(p, val):
    while p <= n:
        fw[p] += val
        p += p & -p

def fw_sum(p):
    sm = 0
    while p > 0:
        sm += fw[p]
        p -= p & -p
    return sm

def fw_rsum(p1, p2):
    return fw_sum(p2) - fw_sum(p1 - 1)
(N, M) = map(int, input().split())
fw_add(1, 1)
fw_add(1000001, 1)
(rmv, add) = (defaultdict(list), defaultdict(list))
for _ in range(N):
    (y, x1, x2) = map(int, input().split())
    (y, x1, x2) = (y + 1, x1 + 1, x2 + 1)
    if x1 == 1:
        fw_add(y, 1)
        rmv[x2].append(y)
    else:
        add[x1].append(y)
query = {}
for _ in range(M):
    (x, y1, y2) = map(int, input().split())
    (x, y1, y2) = (x + 1, y1 + 1, y2 + 1)
    query[x] = (y1, y2)
query[1000001] = (1, 1000001)
cc = 0
for p in range(1, 1000002):
    if p in add:
        for y in add[p]:
            fw_add(y, 1)
    if p in query:
        (y1, y2) = query[p]
        cc += fw_rsum(y1, y2) - 1
    if p in rmv:
        for y in rmv[p]:
            fw_add(y, -1)
print(cc - sum((len(u) for u in add.values())))