Problem:


Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

1 \leq N, Q \leq 2 \times 10^5

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.



Sample input:
4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8


Sample output:
2
2
10
-1
13
-1

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
input = sys.stdin.readline


class LazyPropSegmentTree:
    def __init__(self, a):
        self.padding = float('inf')        
        self.n = len(a)
        self.N = 1 << (self.n-1).bit_length()
        self.seg_data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-self.n)
        self.lazy = [None] * (2*self.N - 1)
        for i in range(2*self.N-2, 0, -2):
            self.seg_data[(i-1)>>1] = min(self.seg_data[i], self.seg_data[i-1])
    
    def __len__(self):
        return self.n
    
    def indices_generator(self, i, j):
        # [i, j)
        # [idx1, idx2)を部分的に含むインデックスを降順にyieldするジェネレータ
        idx1 = self.N + i
        idx2 = self.N + j
        lm = (idx1 // (idx1 & -idx1)) >> 1
        rm = (idx2 // (idx2 & -idx2)) >> 1
        while 0 < idx1 < idx2:
            if idx2 <= rm:
                yield idx2-1
            if idx1 <= lm:
                yield idx1-1
            idx1 >>= 1
            idx2 >>= 1
        while idx1:
            yield idx1-1
            idx1 >>= 1
    
    def propagate_down(self, indices):
        # 遅延させていた値を伝搬させる
        # indicesは昇順
        for idx in indices:
            v = self.lazy[idx]
            if v is not None:
                idx1, idx2 = 2*idx + 1, 2*idx + 2
                self.lazy[idx1] = self.lazy[idx2] = v
                self.seg_data[idx1] = self.seg_data[idx2] = v
                self.lazy[idx] = None
    
    def propagate_up(self, indices):
        for idx in indices:
            idx1, idx2 = 2*idx + 1, 2*idx + 2
            self.seg_data[idx] = min(self.seg_data[idx1], self.seg_data[idx2])

    def propagate_all(self):
        indices = [i for i in range(self.N-1)]
        self.propagate_down(indices)
        self.propagate_up(indices[::-1])
    
    def update(self, i, j, x):
        # [i, j)の区間の値をxに変更する
        *should_prop, = self.indices_generator(i, j)
        self.propagate_down(should_prop[::-1])
        idx1 = self.N - 1 + i
        idx2 = self.N - 2 + j
        while idx1 < idx2 + 1:
            if idx1&1 == 0: # idx1が偶数
                self.lazy[idx1] = self.seg_data[idx1] = x
            if idx2&1 == 1: # idx2が奇数
                self.lazy[idx2] = self.seg_data[idx2] = x
            idx1 >>= 1
            idx2 >>= 1
            idx2 -= 1
        self.propagate_up(should_prop)
    
    def query(self, i, j):
        # [i, j)
        *should_prop, = self.indices_generator(i, j)
        self.propagate_down(should_prop[::-1])
        idx1 = self.N - 1 + i
        idx2 = self.N - 2 + j
        result = self.padding
        while idx1 < idx2 + 1:
                if idx1&1 == 0: # idx1が偶数
                    result = min(result, self.seg_data[idx1])
                if idx2&1 == 1: # idx2が奇数
                    result = min(result, self.seg_data[idx2])                
                idx1 >>= 1
                idx2 >>= 1
                idx2 -= 1
        return result
    
    @property
    def data(self):
        return self.seg_data[self.N-1:self.N-1+self.n]
    

INF = float('inf')
N, Q = map(int, input().split())
STX = [list(map(int, input().split())) for i in range(N)]
STX.sort(key=lambda x:x[2], reverse=True)
st = LazyPropSegmentTree([INF]*Q)
d = [int(input()) for i in range(Q)]

for s, t, x in STX:
  il = bisect.bisect_left(d, s-x)
  ir = bisect.bisect_right(d, t-x-1)
  if il == ir:
    continue
  st.update(il, ir, x)

st.propagate_down([i for i in range(st.N-1)])
ans = [x if x<INF else -1 for x in st.data]
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
input = sys.stdin.readline


def initialize(a, value=float('inf')):
    n = len(a)
    N = 1 << (n-1).bit_length()
    seg_data = [value]*(N-1) + a + [value]*(N-n)
    lazy = [None] * (2*N - 1)
    for i in range(2*N-2, 0, -2):
        seg_data[(i-1)>>1] = min(seg_data[i], seg_data[i-1])
    return seg_data, lazy, N

def indices_generator(i, j, N):
    # [i, j)
    # [idx1, idx2)を部分的に含むインデックスを降順にyieldするジェネレータ
    idx1 = N + i
    idx2 = N + j
    lm = (idx1 // (idx1 & -idx1)) >> 1
    rm = (idx2 // (idx2 & -idx2)) >> 1
    while idx1 < idx2:
        if idx2 <= rm:
            yield idx2-1
        if idx1 <= lm:
            yield max(0, idx1-1)
        idx1 >>= 1
        idx2 >>= 1
    while idx1:
        yield idx1-1
        idx1 >>= 1
    
def propagate(indices):
    # 遅延させていた値を伝搬させる
    # indicesは昇順
    for idx in indices:
        v = lazy[idx]
        if v is None:
            continue
        _idx = 2*idx + 1
        lazy[_idx] = lazy[_idx+1] = v
        seg_data[_idx] = seg_data[_idx+1] = v
        lazy[idx] = None
        
def update(i, j, x, N):
    # [i, j)の区間の値をxに変更する
    *should_prop, = indices_generator(i, j, N)
    propagate(should_prop[::-1])
    idx1 = N - 1 + i
    idx2 = N - 2 + j
    while idx1 < idx2 + 1:
        if idx1&1 == 0: # idx1が偶数
            lazy[idx1] = seg_data[idx1] = x
        if idx2&1 == 1: # idx2が奇数
            lazy[idx2] = seg_data[idx2] = x
        idx1 >>= 1
        idx2 >>= 1
        idx2 -= 1        
    for idx in should_prop:
        _idx = 2*idx + 1
        seg_data[idx] = min(seg_data[_idx], seg_data[_idx+1])
    

INF = float('inf')
N, Q = map(int, input().split())
STX = [list(map(int, input().split())) for i in range(N)]
STX.sort(key=lambda x:x[2], reverse=True)
seg_data, lazy, N0 = initialize([INF]*Q)
d = [int(input()) for i in range(Q)]

for s, t, x in STX:
  il = bisect.bisect_left(d, s-x)
  ir = bisect.bisect_right(d, t-x-1)
  if il == ir:
    continue
  update(il, ir, x, N0)

propagate([i for i in range(N0-1)])
ans = [x if x<INF else -1 for x in seg_data[N0-1:N0-1+Q]]
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
input = sys.stdin.readline

class LazyPropSegmentTree:
    def __init__(self, a):
        self.padding = float('inf')        
        self.n = len(a)
        self.N = 1 << (self.n-1).bit_length()
        self.seg_data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-self.n)
        self.lazy = [None] * (2*self.N - 1)
        for i in range(2*self.N-2, 0, -2):
            self.seg_data[(i-1)>>1] = min(self.seg_data[i], self.seg_data[i-1])

    def indices_generator(self, i, j):
        # [i, j)
        # [idx1, idx2)を部分的に含むインデックスを降順にyieldするジェネレータ
        idx1 = self.N + i
        idx2 = self.N + j
        lm = (idx1 // (idx1 & -idx1)) >> 1
        rm = (idx2 // (idx2 & -idx2)) >> 1
        while idx1 < idx2:
            if idx2 <= rm:
                yield idx2-1
            if idx1 <= lm:
                yield max(0, idx1-1)
            idx1 >>= 1
            idx2 >>= 1
        while idx1:
            yield idx1-1
            idx1 >>= 1
    
    def propagate(self, indices):
        # 遅延させていた値を伝搬させる
        # indicesは昇順
        for idx in indices:
            v = self.lazy[idx]
            if v is None:
                continue
            _idx = 2*idx + 1
            self.lazy[_idx] = self.lazy[_idx+1] = v
            self.seg_data[_idx] = self.seg_data[_idx+1] = v
            self.lazy[idx] = None
        
    def update(self, i, j, x):
        # [i, j)の区間の値をxに変更する
        *should_prop, = self.indices_generator(i, j)
        self.propagate(should_prop[::-1])
        idx1 = self.N - 1 + i
        idx2 = self.N - 2 + j
        while idx1 < idx2 + 1:
            if idx1&1 == 0: # idx1が偶数
                self.lazy[idx1] = self.seg_data[idx1] = x
            if idx2&1 == 1: # idx2が奇数
                self.lazy[idx2] = self.seg_data[idx2] = x
            idx1 >>= 1
            idx2 >>= 1
            idx2 -= 1        
        for idx in should_prop:
            _idx = 2*idx + 1
            self.seg_data[idx] = min(self.seg_data[_idx], self.seg_data[_idx+1])
    
    @property
    def data(self):
        return self.seg_data[self.N-1:self.N-1+self.n]


INF = float('inf')
N, Q = map(int, input().split())
STX = [list(map(int, input().split())) for i in range(N)]
STX.sort(key=lambda x:x[2], reverse=True)
st = LazyPropSegmentTree([INF]*Q)
d = [int(input()) for i in range(Q)]

for s, t, x in STX:
  il = bisect.bisect_left(d, s-x)
  ir = bisect.bisect_right(d, t-x-1)
  if il == ir:
    continue
  st.update(il, ir, x)

st.propagate([i for i in range(st.N-1)])
ans = [x if x<INF else -1 for x in st.data]
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
input = sys.stdin.readline

class LazyPropSegmentTree:
    def __init__(self, a):
        self.padding = float('inf')        
        self.n = len(a)
        self.N = 1 << (self.n-1).bit_length()
        self.seg_data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-self.n)
        self.lazy = [None] * (2*self.N - 1)
        for i in range(2*self.N-2, 0, -2):
            self.seg_data[(i-1)>>1] = min(self.seg_data[i], self.seg_data[i-1])

    def indices_generator(self, i, j):
        # [i, j)
        # [idx1, idx2)を部分的に含むインデックスを降順にyieldするジェネレータ
        idx1 = self.N + i
        idx2 = self.N + j
        lm = (idx1 // (idx1 & -idx1)) >> 1
        rm = (idx2 // (idx2 & -idx2)) >> 1
        while idx1 < idx2:
            if idx2 <= rm:
                yield idx2-1
            if idx1 <= lm:
                yield max(0, idx1-1)
            idx1 >>= 1
            idx2 >>= 1
        while idx1:
            yield idx1-1
            idx1 >>= 1
    
    def propagate(self, indices):
        # 遅延させていた値を伝搬させる
        # indicesは昇順
        for idx in indices:
            v = self.lazy[idx]
            if v is not None:
                idx1, idx2 = 2*idx + 1, 2*idx + 2
                self.lazy[idx1] = self.lazy[idx2] = v
                self.seg_data[idx1] = self.seg_data[idx2] = v
                self.lazy[idx] = None
        
    def update(self, i, j, x):
        # [i, j)の区間の値をxに変更する
        *should_prop, = self.indices_generator(i, j)
        self.propagate(should_prop[::-1])
        idx1 = self.N - 1 + i
        idx2 = self.N - 2 + j
        while idx1 < idx2 + 1:
            if idx1&1 == 0: # idx1が偶数
                self.lazy[idx1] = self.seg_data[idx1] = x
            if idx2&1 == 1: # idx2が奇数
                self.lazy[idx2] = self.seg_data[idx2] = x
            idx1 >>= 1
            idx2 >>= 1
            idx2 -= 1        
        for idx in should_prop:
            idx1, idx2 = 2*idx + 1, 2*idx + 2
            self.seg_data[idx] = min(self.seg_data[idx1], self.seg_data[idx2])
    
    @property
    def data(self):
        return self.seg_data[self.N-1:self.N-1+self.n]


INF = float('inf')
N, Q = map(int, input().split())
STX = [list(map(int, input().split())) for i in range(N)]
STX.sort(key=lambda x:x[2], reverse=True)
st = LazyPropSegmentTree([INF]*Q)
d = [int(input()) for i in range(Q)]

for s, t, x in STX:
  il = bisect.bisect_left(d, s-x)
  ir = bisect.bisect_right(d, t-x-1)
  if il == ir:
    continue
  st.update(il, ir, x)

st.propagate([i for i in range(st.N-1)])
ans = [x if x<INF else -1 for x in st.data]
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
input = sys.stdin.readline

class LazyPropSegmentTree:
    def __init__(self, a):
        self.padding = float('inf')        
        self.n = len(a)
        self.N = 1 << (self.n-1).bit_length()
        self.seg_data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-self.n)
        self.lazy = [None] * (2*self.N - 1)
        for i in range(2*self.N-2, 0, -2):
            self.seg_data[(i-1)>>1] = min(self.seg_data[i], self.seg_data[i-1])
    
    def __len__(self):
        return self.n
    
    def indices_generator(self, i, j):
        # [i, j)
        # [idx1, idx2)を部分的に含むインデックスを降順にyieldするジェネレータ
        idx1 = self.N + i
        idx2 = self.N + j
        lm = (idx1 // (idx1 & -idx1)) >> 1
        rm = (idx2 // (idx2 & -idx2)) >> 1
        while idx1 < idx2:
            if idx2 <= rm:
                yield idx2-1
            if idx1 <= lm:
                yield max(0, idx1-1)
            idx1 >>= 1
            idx2 >>= 1
        while idx1:
            yield idx1-1
            idx1 >>= 1
    
    def propagate(self, indices):
        # 遅延させていた値を伝搬させる
        # indicesは昇順
        for idx in indices:
            v = self.lazy[idx]
            if v is not None:
                idx1, idx2 = 2*idx + 1, 2*idx + 2
                self.lazy[idx1] = self.lazy[idx2] = v
                self.seg_data[idx1] = self.seg_data[idx2] = v
                self.lazy[idx] = None
    
    def propagate_all(self):
        indices = [i for i in range(self.N-1)]
        self.propagate(indices)
        for idx in indices[::-1]:
            idx1, idx2 = 2*idx + 1, 2*idx + 2
            self.seg_data[idx] = min(self.seg_data[idx1], self.seg_data[idx2])
    
    def update(self, i, j, x):
        # [i, j)の区間の値をxに変更する
        *should_prop, = self.indices_generator(i, j)
        self.propagate(should_prop[::-1])
        idx1 = self.N - 1 + i
        idx2 = self.N - 2 + j
        while idx1 < idx2 + 1:
            if idx1&1 == 0: # idx1が偶数
                self.lazy[idx1] = self.seg_data[idx1] = x
            if idx2&1 == 1: # idx2が奇数
                self.lazy[idx2] = self.seg_data[idx2] = x
            idx1 >>= 1
            idx2 >>= 1
            idx2 -= 1        
        for idx in should_prop:
            idx1, idx2 = 2*idx + 1, 2*idx + 2
            self.seg_data[idx] = min(self.seg_data[idx1], self.seg_data[idx2])
    
    def query(self, i, j):
        # [i, j)
        *should_prop, = self.indices_generator(i, j)
        self.propagate(should_prop[::-1])
        idx1 = self.N - 1 + i
        idx2 = self.N - 2 + j
        result = self.padding
        while idx1 + 1 < idx2:
                if idx1&1 == 0: # idx1が偶数
                    result = min(result, self.seg_data[idx1])
                if idx2&1 == 1: # idx2が奇数
                    result = min(result, self.seg_data[idx2])                
                idx1 >>= 1
                idx2 >>= 1
                idx2 -= 1
        if idx1 == idx2:
            result = min(result, self.seg_data[idx1])
        else: # idx1 + 1 == idx2
            result = min(result, self.seg_data[idx1], self.seg_data[idx2])
        return result
    
    @property
    def data(self):
        return self.seg_data[self.N-1:self.N-1+self.n]


N, Q = map(int, input().split())
STX = [list(map(int, input().split())) for i in range(N)]
STX.sort(key=lambda x:x[2], reverse=True)
st = LazyPropSegmentTree([float('inf')]*Q)
d = [int(input()) for i in range(Q)]

for s, t, x in STX:
  il = bisect.bisect_left(d, s-x)
  ir = bisect.bisect_right(d, t-x-1)
  if il == ir:
    continue
  st.update(il, ir, x)

st.propagate_all()
for x in st.data:
  if x < float('inf'):
    print(x)
  else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,Q = LI()
    q = LIR(n)
    q = [(s-x,x,i) for (i,(s,t,x)) in enumerate(q)]+[(t-x,x,i) for (i,(s,t,x)) in enumerate(q)]
    q.sort()
    m = [(float("inf"),n)]
    f = [1]*(n+1)
    f[n] = 0
    l = 0
    for _ in range(Q):
        d = I()
        while l < len(q) and q[l][0] <= d:
            t,x,i = q[l]
            f[i] ^= 1
            if not f[i]:
                heappush(m,(x,i))
            l += 1
        while f[m[0][1]]:
            heappop(m)
        ans,i = heappop(m)
        if ans == float("inf"):
            print(-1)
        else:
            print(ans)
        heappush(m,(ans,i))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
sys.setrecursionlimit(10 ** 7)

input = sys.stdin.readline

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for i in range(Q):
    d = int(input())
    PEOPLE.append((d, i))

ans = [-1] * Q

STX.sort(key=lambda x: x[2])

for s, t, x in STX:
    if len(PEOPLE) == 0:
        break

    i = bisect.bisect_left(PEOPLE, (s - x, -1))
    j = bisect.bisect_left(PEOPLE, (t - x, -1))

    if i == j:
        continue

    use = [PEOPLE[x][1] for x in range(i, j)]

    for k in use:
        ans[k] = x

    del PEOPLE[i:j]


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for i in range(Q):
    d = int(input())
    PEOPLE.append((d, i))

ans = [-1] * Q

STX.sort(key=lambda x: x[2])

for s, t, x in STX:
    if len(PEOPLE) == 0:
        break

    i = bisect.bisect_left(PEOPLE, (s - x, -1))
    j = bisect.bisect_left(PEOPLE, (t - x, -1))

    if i == j:
        continue

    use = [PEOPLE[x][1] for x in range(i, j)]

    for k in use:
        ans[k] = x

    del PEOPLE[i:j]


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()


STOP = set()
h = []
for t, op, x in EVENT:
    if not op:
        STOP.remove(x)
    elif op & 1:
        STOP.add(x)
        heappush(h, x)
    else:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if not op:
        STOP.remove(x)
    elif op & 1:
        STOP.add(x)
        heappush(h, x)
    else:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1


'''
    if op == 2:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    elif op == 0:
        STOP.remove(x)
'''


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if not op:
        STOP.remove(x)
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    else:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if not op:
        STOP.remove(x)
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    else:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1


'''
    if op == 2:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    elif op == 0:
        STOP.remove(x)
'''


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if op == 2:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0] if h else -1
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    elif op == 0:
        STOP.remove(x)


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if op == 2 and STOP:
        while h[0] not in STOP:
            heappop(h)
        ans[x] = h[0]
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    elif op == 0:
        STOP.remove(x)


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
import heapq
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if op == 2 and STOP:
        while h[0] not in STOP:
            heapq.heappop(h)
        ans[x] = h[0]
    elif op == 1:
        STOP.add(x)
        heapq.heappush(h, x)
    elif op == 0:
        STOP.remove(x)


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
import heapq
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
h = []
for t, op, x in EVENT:
    if op == 2 and STOP:
        ans[x] = min(list(STOP))
    elif op == 1:
        STOP.add(x)
    elif op == 0:
        STOP.remove(x)


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
import heapq
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 1, x))
    EVENT.append((t-x, 0, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = set()
for t, op, x in EVENT:
    if op == 2 and STOP:
        ans[x] = min(list(STOP))
    elif op == 1:
        #heapq.heappush(STOP, x)
        STOP.add(x)
    elif op == 0:
        if x in STOP:
            STOP.remove(x)
        # heapq.heapify(STOP)


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
import heapq
sys.setrecursionlimit(10 ** 7)

# TLE

N, Q = map(int, input().split())
STX = [[int(x) for x in input().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s-x, 0, x))
    EVENT.append((t-x, 1, x))

for i in range(Q):
    d = int(input())
    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

# print(EVENT)

STOP = []
for t, op, x in EVENT:
    if op == 2 and STOP:
        ans[x] = STOP[0]
    elif op == 0:
        heapq.heappush(STOP, x)
    elif op == 1:
        STOP.remove(x)
        heapq.heapify(STOP)


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#import numpy as np
import bisect
sys.setrecursionlimit(10 ** 7)

# TLE
# PyPy3 (2.4.0) 2112 ms

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for i in range(Q):
    d = int(input())
    PEOPLE.append((d, i))

ans = [-1] * Q

STX.sort(key=lambda x: x[2])

for s, t, x in STX:
    if len(PEOPLE) == 0:
        break

    i = bisect.bisect_left(PEOPLE, (s - x, -1))
    j = bisect.bisect_left(PEOPLE, (t - x, -1))

    if i == j:
        continue

    use = [PEOPLE[x][1] for x in range(i, j)]

    for k in use:
        ans[k] = x

    del PEOPLE[i:j]
    #PEOPLE = [PEOPLE[x] for x in range(len(PEOPLE)) if not(i <= x < j)]


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#import numpy as np
import bisect
sys.setrecursionlimit(10 ** 7)

# TLE
# PyPy3 (2.4.0) 2112 ms

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for i in range(Q):
    d = int(input())
    PEOPLE.append((d, i))

ans = [-1] * Q

STX.sort(key=lambda x: x[2])

for s, t, x in STX:
    if len(PEOPLE) == 0:
        break

    i = bisect.bisect_left(PEOPLE, (s - x, -1))
    j = bisect.bisect_left(PEOPLE, (t - x, -1))

    if i == j:
        continue

    use = [PEOPLE[x][1] for x in range(i, j)]

    #print(len(PEOPLE), use)
    for k in use:
        ans[k] = x

    del PEOPLE[i:j]


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#import numpy as np
import bisect
sys.setrecursionlimit(10 ** 7)

# TLE
# PyPy3 (2.4.0) 2112 ms

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for i in range(Q):
    d = int(input())
    PEOPLE.append((d, i))

ans = [-1] * Q

STX.sort(key=lambda x: x[2])

for s, t, x in STX:
    if len(PEOPLE) == 0:
        break

    i = bisect.bisect_left(PEOPLE, (s - x, -1))
    j = bisect.bisect_left(PEOPLE, (t - x, -1))

    if i == j:
        continue

    use = [PEOPLE[x][1] for x in range(i, j)]

    #print(len(PEOPLE), use)
    for k in use:
        if ans[k] == -1:
            ans[k] = x

    del PEOPLE[i:j]


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#import numpy as np
import bisect
sys.setrecursionlimit(10 ** 7)

# TLE
# PyPy3 (2.4.0) 2112 ms

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for i in range(Q):
    d = int(input())
    PEOPLE.append((d, i))

ans = [-1] * Q

STX.sort(key=lambda x: x[2])

for s, t, x in STX:
    if len(PEOPLE) == 0:
        break

    i = bisect.bisect_left(PEOPLE, (s - x, -1))
    j = bisect.bisect_left(PEOPLE, (t - x, -1))

    if i == j:
        continue

    use = [PEOPLE[x][1] for x in range(i, j)]

    #print(len(PEOPLE), use)

    for k in use:
        if ans[k] == -1:
            ans[k] = x

    PEOPLE = [PEOPLE[x] for x in range(len(PEOPLE)) if not(i <= x < j)]


print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#import numpy as np
import bisect
sys.setrecursionlimit(10 ** 7)

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for _ in range(Q):
    d = int(input())
    PEOPLE.append(d)

ans = [-1] * Q

STX.sort(key=lambda x: -x[2])

for s, t, x in STX:
    #print("s,t,x", s, t, x)
    #i = np.searchsorted(PEOPLE, s - x)
    #j = np.searchsorted(PEOPLE, t - x)
    i = bisect.bisect_left(PEOPLE, s - x)
    j = bisect.bisect_left(PEOPLE, t - x)
    #print("i,j", i, j)

    ans[i:j] = [x] * (j-i)

    # for k in range(i, j):
    # if ans[k] == '-1':
    #ans[k] = str(x)

    # print(ans)

print("\n".join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#import numpy as np
import bisect
sys.setrecursionlimit(10 ** 7)

N, Q = map(int, input().split())
STX = []
for _ in range(N):
    s, t, x = map(int, input().split())
    STX.append((s, t, x))

PEOPLE = []
for _ in range(Q):
    d = int(input())
    PEOPLE.append(d)

ans = [str(-1)] * Q

STX.sort(key=lambda x: -x[2])

for s, t, x in STX:
    #print("s,t,x", s, t, x)
    #i = np.searchsorted(PEOPLE, s - x)
    #j = np.searchsorted(PEOPLE, t - x)
    i = bisect.bisect_left(PEOPLE, s - x)
    j = bisect.bisect_left(PEOPLE, t - x)
    #print("i,j", i, j)

    ans[i:j] = [str(x)] * (j-i)

    # for k in range(i, j):
    # if ans[k] == '-1':
    #ans[k] = str(x)

    # print(ans)

print("\n".join(ans))