The Tower is Going Home (Codeforces 1075C)
Tags: Others, two pointers

On a chessboard with a width of $$$10^9$$$ and a height of $$$10^9$$$, the rows are numbered from bottom to top from $$$1$$$ to $$$10^9$$$, and the columns are numbered from left to right from $$$1$$$ to $$$10^9$$$. Therefore, for each cell of the chessboard you can assign the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number and $$$y$$$ is the row number.Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates $$$(1,1)$$$. But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number $$$10^9$$$).Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:   Vertical. Each of these is defined by one number $$$x$$$. Such spells create an infinite blocking line between the columns $$$x$$$ and $$$x+1$$$.  Horizontal. Each of these is defined by three numbers $$$x_1$$$, $$$x_2$$$, $$$y$$$. Such spells create a blocking segment that passes through the top side of the cells, which are in the row $$$y$$$ and in columns from $$$x_1$$$ to $$$x_2$$$ inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.    An example of a chessboard. Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell $$$(r_0,c_0)$$$ into the cell $$$(r_1,c_1)$$$ only under the condition that $$$r_1 = r_0$$$ or $$$c_1 = c_0$$$ and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!

Input: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n,m \le 10^5$$$) — the number of vertical and horizontal spells.Each of the following $$$n$$$ lines contains one integer $$$x$$$ ($$$1 \le x < 10^9$$$) — the description of the vertical spell. It will create a blocking line between the columns of $$$x$$$ and $$$x+1$$$.Each of the following $$$m$$$ lines contains three integers $$$x_1$$$, $$$x_2$$$ and $$$y$$$ ($$$1 \le x_{1} \le x_{2} \le 10^9$$$, $$$1 \le y < 10^9$$$) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number $$$y$$$, in columns from $$$x_1$$$ to $$$x_2$$$ inclusive.It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.

Output: In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell $$$(1,1)$$$ to at least one cell in the row with the number $$$10^9$$$

Note: In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.  Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home. In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.  Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home. In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.  Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home. In the fourth sample, we have no spells, which means that we do not need to remove anything.In the fifth example, we can remove the first vertical and third horizontal spells.  Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. 

Sample Inputs:
2 3
6
8
1 5 6
1 9 4
2 4 2

1 3
4
1 5 3
1 9 4
4 6 6

0 2
1 1000000000 4
1 1000000000 2

0 0

2 3
4
6
1 4 3
1 5 2
1 6 5

Sample Outputs:
1

1

2

0

2

Solution (Python):
MAX = 1000000000

def solve():
    (n, m) = list(map(int, input().split(' ')))
    arr_x1 = []
    for i in range(0, n):
        arr_x1.append(int(input()))
    arr_x1.append(MAX)
    arr_x2 = []
    for i in range(0, m):
        (x1, x2, y) = list(map(int, input().split(' ')))
        if x1 == 1:
            arr_x2.append(x2)
    arr_x1.sort()
    arr_x2.sort()
    j = 0
    ans = MAX
    for i in range(0, len(arr_x1)):
        while j < len(arr_x2) and arr_x2[j] < arr_x1[i]:
            j += 1
        ans = min(ans, i + (len(arr_x2) - j))
    print(ans)
    pass
t = 1
while t > 0:
    solve()
    t -= 1

Solution (Python):
import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os

def values():
    return tuple(map(int, sys.stdin.readline().split()))

def inlst():
    return [int(i) for i in sys.stdin.readline().split()]

def inp():
    return int(sys.stdin.readline())

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

def words():
    return [i for i in sys.stdin.readline().split()]

def chars():
    return list(sys.stdin.readline().strip())

def solve():
    (n, m) = values()
    vert = []
    for _ in range(n):
        vert.append(inp())
    vert = sorted(vert)
    vert.append(1000000000.0)
    hori = []
    for _ in range(m):
        (x1, x2, _) = values()
        if x1 == 1:
            hori.append(x2)
    n = len(vert)
    m = len(hori)
    vert_inc = [0] * n
    hori = sorted(hori)
    (j, i) = (n - 1, m - 1)
    while j >= 0:
        while i >= 0 and hori[i] >= vert[j]:
            i -= 1
        vert_inc[j] = m - 1 - i
        j -= 1
    ans = 2000000.0
    for rv in range(0, n):
        ans = min(ans, rv + vert_inc[rv])
    print(ans)
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from collections import deque

def rall():
    return sys.stdin.readlines()

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

def rl_types(types):
    str_list = [x for x in sys.stdin.readline().strip().split(' ')]
    return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something=''):
    sys.stdout.write(str(something) + '\n')

def pra(array):
    sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
    return array
if __name__ == '__main__':
    (n, m) = map(int, rl().split(' '))
    verts = [int(rl()) for _ in range(n)]
    verts.sort()
    verts = deque(verts)
    hsegs = []
    for _ in range(m):
        (l, r, y) = map(int, rl().split(' '))
        if l == 1:
            hsegs.append(r)
    hsegs.sort()
    hsegs = deque(hsegs)
    hleft = len(hsegs)
    vremoved = 0
    vi = 0
    hi = 0
    min_remove = hleft
    while True:
        if len(hsegs) == 0:
            break
        elif len(verts) == 0:
            while len(hsegs) > 0 and hsegs[0] < 1000000000:
                hsegs.popleft()
                hleft -= 1
            min_remove = min(min_remove, hleft + vremoved)
            break
        elif hsegs[0] >= verts[0]:
            verts.popleft()
            vremoved += 1
        else:
            while len(hsegs) > 0 and hsegs[0] < verts[0]:
                hsegs.popleft()
                hleft -= 1
        min_remove = min(min_remove, hleft + vremoved)
    print(min_remove)