A Game with Traps (Codeforces 1260D)
Tags: Greedy, binary search

You are playing a computer game, where you lead a party of $$$m$$$ soldiers. Each soldier is characterised by his agility $$$a_i$$$.The level you are trying to get through can be represented as a straight line segment from point $$$0$$$ (where you and your squad is initially located) to point $$$n + 1$$$ (where the boss is located).The level is filled with $$$k$$$ traps. Each trap is represented by three numbers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$. $$$l_i$$$ is the location of the trap, and $$$d_i$$$ is the danger level of the trap: whenever a soldier with agility lower than $$$d_i$$$ steps on a trap (that is, moves to the point $$$l_i$$$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $$$r_i$$$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.You have $$$t$$$ seconds to complete the level — that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:  if your location is $$$x$$$, you may move to $$$x + 1$$$ or $$$x - 1$$$. This action consumes one second;  if your location is $$$x$$$ and the location of your squad is $$$x$$$, you may move to $$$x + 1$$$ or to $$$x - 1$$$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $$$d_i$$$ greater than agility of some soldier from the squad). This action consumes one second;  if your location is $$$x$$$ and there is a trap $$$i$$$ with $$$r_i = x$$$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers.You have to choose the maximum number of soldiers such that they all can be brought from the point $$$0$$$ to the point $$$n + 1$$$ (where the boss waits) in no more than $$$t$$$ seconds.

Input: The first line contains four integers $$$m$$$, $$$n$$$, $$$k$$$ and $$$t$$$ ($$$1 \le m, n, k, t \le 2 \cdot 10^5$$$, $$$n < t$$$) — the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.The second line contains $$$m$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_m$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the agility of the $$$i$$$-th soldier.Then $$$k$$$ lines follow, containing the descriptions of traps. Each line contains three numbers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$ ($$$1 \le l_i \le r_i \le n$$$, $$$1 \le d_i \le 2 \cdot 10^5$$$) — the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.

Output: Print one integer — the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $$$t$$$ seconds.

Note: In the first example you may take soldiers with agility $$$3$$$, $$$4$$$ and $$$5$$$ with you. The course of action is as follows:  go to $$$2$$$ without your squad;  disarm the trap $$$2$$$;  go to $$$3$$$ without your squad;  disartm the trap $$$3$$$;  go to $$$0$$$ without your squad;  go to $$$7$$$ with your squad. The whole plan can be executed in $$$13$$$ seconds.

Sample Inputs:
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3

Sample Outputs:
3

Solution (Python):
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

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

def inpl():
    return list(map(int, sys.stdin.readline().split()))

def inpl_str():
    return list(sys.stdin.readline().split())
(M, N, K, T) = inpl()
AA = inpl()
DD_di = []
LR = []
for i in range(K):
    (L, R, D) = inpl()
    LR.append((L, R))
    DD_di.append((D, i))
DD_di.sort()
DD_d = [D for (D, i) in DD_di]

def solve(x):
    idx = bisect.bisect_right(DD_d, x)
    path = [0] * (N + 2)
    for (D, i) in DD_di[idx:]:
        (L, R) = LR[i]
        path[L] += 1
        path[R + 1] -= 1
    t = N + 1
    bp = path[0]
    for i in range(1, N + 2):
        p = bp + path[i]
        if p > 0:
            t += 2
        bp = p
    return t <= T
OK = 10 ** 5 * 2 + 10
NG = -1
while OK - NG > 1:
    mid = (OK + NG) // 2
    if solve(mid):
        OK = mid
    else:
        NG = mid
ans = 0
for A in AA:
    if A >= OK:
        ans += 1
print(ans)

Solution (Python):
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

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

def inpl():
    return list(map(int, sys.stdin.readline().split()))

def inpl_str():
    return list(sys.stdin.readline().split())
(m, n, k, T) = list(map(int, input().split()))
a = list(map(int, input().split()))
DD = []
LR = []
for i in range(k):
    (L, R, D) = map(int, input().split())
    LR.append((L, R))
    DD.append((D, i))
DD.sort()
DD_id = [D for (D, i) in DD]

def Solve(x):
    idx = bisect.bisect_right(DD_id, x)
    path = [0] * (n + 2)
    for (X, i) in DD[idx:]:
        (LL, RR) = LR[i]
        path[LL] += 1
        path[RR + 1] -= 1
    t = n + 1
    bp = path[0]
    for i in range(1, n + 2):
        p = bp + path[i]
        if p > 0:
            t += 2
        bp = p
    return t <= T
L = -1
R = 2 * 10 ** 5 + 2
while R - L > 1:
    mid = (L + R) // 2
    if Solve(mid):
        R = mid
    else:
        L = mid
ans = 0
for i in a:
    if i >= R:
        ans += 1
print(ans)

Solution (Python):
import sys
from bisect import bisect_left
(m, n, k, t) = map(int, input().split())
a = sorted(map(int, input().split()))
traps = sorted((list(map(int, l.split())) for l in sys.stdin))
(ok, ng) = (2 * 10 ** 5, 0)
while ok - ng > 1:
    mid = (ok + ng) // 2
    (l, r) = (0, 0)
    dist = n + 1
    for i in range(k):
        if mid < traps[i][2]:
            if r < traps[i][0]:
                dist += (r - l) * 2
                (l, r) = (traps[i][0] - 1, traps[i][1])
            else:
                r = max(r, traps[i][1])
    dist += (r - l) * 2
    if dist <= t:
        ok = mid
    else:
        ng = mid
print(m - bisect_left(a, ok))