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: Wrong Answer

Solution code:
class AVLtree:
    """
    参考：http://www.nct9.ne.jp/m_hiroi/light/pyalgo12.html
    insert : 挿入
    erase : 削除
    lower_bound(x) : x <= v なる最小のv、なければ-float("inf")、最小値はlower(-float("inf"))
    upper_bound(x) : v < x なる最大のv、なければfloat("inf")、最大値はupper(float("inf"))
    print(avltree) : デバッグ用
    """
 
    def __init__(self):
        self.root = None

        
    def insert(self, x):
        if self.root is None:
            self.root = Node(x)
            return
        q = []
        node = self.root
        while True:
            if node.val == x:
                return
            elif x < node.val:
                q.append((node, 0))
                if node.left is None:
                    node.left = Node(x)
                    break
                node = node.left
            else:
                q.append((node, 1))
                if node.right is None:
                    node.right = Node(x)
                    break
                node = node.right
                
        # バランス調整
        new_node = None
        while q:
            node, key = q.pop()
            if not key:
                node.bal += 1
            else:
                node.bal -= 1
            bal = node.bal
            if not bal:
                return
            if bal == 2:
                if node.left.bal == -1:
                    node.left = node.left._rotateL()
                    new_node = node._rotateR()
                    new_node._update_bal()
                else:
                    new_node = node._rotateR()
                    new_node.bal = 0
                    node.bal = 0
                break
            elif bal == -2:
                if node.right.bal == 1:
                    node.right = node.right._rotateR()
                    new_node = node._rotateL()
                    new_node._update_bal()
                else:
                    new_node = node._rotateL()
                    new_node.bal = 0
                    node.bal = 0
                break
        if q:
            node, key = q.pop()
            if not key:
                node.left = new_node
            else:
                node.right = new_node
        elif new_node is not None:
            self.root = new_node

            
    def erase(self, x):
        if self.root is None:
            return
        q = []
        node = self.root
        while node is not None:
            if node.val == x:
                break
            if x < node.val:
                q.append((node, 0))
                node = node.left
            else:
                q.append((node, 1))
                node = node.right
        else:
            return
        if node.left is not None and node.right is not None:
            q.append((node, 1))
            min_node = node.right
            while min_node.left is not None:
                q.append((min_node, 0))
                min_node = min_node.left
            node = min_node
        if q:
            pre, key = q[-1]
        else:
            pre = None
        if node.left is None:
            cnode = node.right
        else:
            cnode = node.left
        if pre is None:
            self.root = cnode
            return
        if not key:
            pre.left = cnode
        else:
            pre.right = cnode
        
        # バランス調整
        while q:
            new_node = None
            node, key = q.pop()
            if not key:
                node.bal -= 1
            else:
                node.bal += 1
            bal = node.bal
            if bal == 2:
                if node.left.bal == -1:
                    node.left = node.left._rotateL()
                    new_node = node._rotateR()
                    new_node._update_bal()
                else:
                    new_node = node._rotateR()
                    if not new_node.bal:
                        new_node.bal = -1
                        node.bal = 1
                    else:
                        new_node.bal = 0
                        node.bal = 0
            elif bal == -2:
                if node.right.bal == 1:
                    node.right = node.right._rotateR()
                    new_node = node._rotateL()
                    new_node._update_bal()
                else:
                    new_node = node._rotateL()
                    if not new_node.bal:
                        new_node.bal = 1
                        node.bal = -1
                    else:
                        new_node.bal= 0
                        node.bal = 0
            elif bal:
                break
            if new_node is not None:
                if not q:
                    self.root = new_node
                    return
                node, key = q[-1]
                if not key:
                    node.left = new_node
                else:
                    node.right = new_node
                if new_node.bal:
                    break

                    
    def lower_bound(self, x):
        res = float("inf")
        node = self.root
        while node is not None:
            if x <= node.val:
                res = node.val
                node = node.left
            else:
                node = node.right
        return res
    
    
    def upper_bound(self, x):
        res = -float("inf")
        node = self.root
        while node is not None:
            if x <= node.val:
                node = node.left
            else:
                res = node.val
                node = node.right
        return res
    
    
    def __str__(self):
        if self.root is None:
            return "[]"
        buff = "["
        for x in self.root._dfs():
            buff += str(x)+", "
        buff = buff.rstrip(", ")
        buff += "]"
        return buff


class Node:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
        self.bal = 0 #1:左1, -1:右1
    
    
    def _rotateR(self):
        nodeL = self.left
        self.left = nodeL.right
        nodeL.right = self
        return nodeL
    
    
    def _rotateL(self):
        nodeR = self.right
        self.right = nodeR.left
        nodeR.left = self
        return nodeR
    
    
    def _update_bal(self):
        if self.bal == 1:
            self.right.bal = -1
            self.left.bal = 0
        elif self.bal == -1:
            self.right.bal = 0
            self.left.bal = 1
        else:
            self.right.bal = 0
            self.left.bal = 0
        self.bal = 0
    
    
    def _dfs(self):
        if self is not None:
            if self.left is not None:
                for x in self.left._dfs():
                    yield x
            yield self.val
            if self.right is not None:
                for x in self.right._dfs():
                    yield x

n,q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
e = [(s-x,x,i) for i,(s,_,x) in enumerate(stx)]+[(t-x,x,i) for i,(_,t,x) in enumerate(stx)]
d = [int(input()) for _ in range(q)]
e += [(di,float("inf"),i) for i,di in enumerate(d)]
e.sort(key = lambda x:x[0])
avl = AVLtree()
ans = []
f = [0]*n
for s,x,i in e:
  if x == float("inf"):
    m = avl.lower_bound((-float("inf"),0))
    ans.append(m[0] if m != float("inf") else -1)
  elif f[i]:
    print(f[i])
    avl.erase(f[i])
  else:
    avl.insert((x,i))
    f[i] = (x,i)
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
class AVLtree:
    """
    __len__ : 長さ
    insert : 挿入
    erase : 削除
    lower(x) : x <= v なる最小のv、なければ-float("inf")、最小値はlower(-float("inf"))
    upper(x) : v < x なる最大のv、なければfloat("inf")、最大値はupper(float("inf"))
    display : デバッグ用
    """
 
    def __init__(self, x=-float("inf"), root=1):
        self.val = x
        self.left = None
        self.right = None
        self.h = 1
        self.num = 0
        self.root = root
 
    def __len__(self):
        return self.num
 
    def _adjusth(self):
        hL = self.left.h if self.left is not None else 0
        hR = self.right.h if self.right is not None else 0
        if hL < hR:
            self.h = hR + 1
        else:
            self.h = hL + 1
 
    def _rotateL(self):
        nodeR = self.right
        self.right = nodeR.left
        nodeR.left = self
        self._adjusth()
        nodeR._adjusth()
        return nodeR
 
    def _rotateR(self):
        nodeL = self.left
        self.left = nodeL.right
        nodeL.right = self
        self._adjusth()
        nodeL._adjusth()
        return nodeL
 
    def _bal(self):
        hL = self.left.h if self.left is not None else 0
        hR = self.right.h if self.right is not None else 0
        return hL - hR
 
    def _balance(self):
        res = self
        if self._bal() == 2:
            nodeL = self.left
            if nodeL._bal() == -1:
                self.left = nodeL._rotateL()
            res = self._rotateR()
        elif self._bal() == -2:
            nodeR = self.right
            if nodeR._bal() == 1:
                self.right = nodeR._rotateR()
            res = self._rotateL()
        return res
 
    def insert(self, x):
        node = self
        q = []
        while True:
            if node.val < x:
                q.append((node, 1))
                if node.right is None:
                    node.right = AVLtree(x, 0)
                    break
                node = node.right
            elif x < node.val:
                q.append((node, 0))
                if node.left is None:
                    node.left = AVLtree(x, 0)
                    break
                node = node.left
            else:
                return
        self.num += 1
        pre = None
        for node, key in q[::-1]:
            if pre is not None:
                if not key:
                    node.left = pre
                else:
                    node.right = pre
            node._adjusth()
            if not node.root:
                pre = node._balance()
 
    def _erase_node(self):
        if self.left is None:
            self = self.right
            return self
 
        node = self.left
        q = [(self, 0)]
        while node.right is not None:
            q.append((node, 1))
            node = node.right
        if len(q) == 1 and node.left is not None:
            q[-1][0].left = node._rotateR()
            node = self.left
            q = [(self, 0)]
            while node.right is not None:
                q.append((node, 1))
                node = node.right
        self.val = node.val
        if len(q) > 1:
            q[-1][0].right = None
        else:
            self.left = None
        pre = None
        for node, key in q[::-1]:
            if pre is not None:
                if not key:
                    node.left = pre
                else:
                    node.right = pre
            node._adjusth()
            pre = node._balance()
        return pre
 
    def erase(self, x):
        node = self
        q = []
        while True:
            if node.val < x:
                q.append((node, 1))
                if node.right is None:
                    return
                if node.right.val == x:
                    node.right = node.right._erase_node()
                    break
                node = node.right
            elif x < node.val:
                q.append((node, 0))
                if node.left is None:
                    return
                if node.left.val == x:
                    node.left = node.left._erase_node()
                    break
                node = node.left
        self.num -= 1
        pre = None
        for node, key in q[::-1]:
            if pre is not None:
                if not key:
                    node.left = pre
                else:
                    node.right = pre
            node._adjusth()
            if not node.root:
                pre = node._balance()
    
    def lower(self, x):
        res = float("inf")
        node = self.right
        while node is not None:
            if x <= node.val:
                res = node.val
                node = node.left
            else:
                node = node.right
        return res
    
    def upper(self, x):
        res = -float("inf")
        node = self.right
        while node is not None:
            if x <= node.val:
                node = node.left
            else:
                res = node.val
                node = node.right
        return res
    
    def display(self):
        q = [(self, 0)]
        p = 0
        for node, d in q:
            if d > p:
                print()
            if node.left is not None:
                print("↙",end="")
                q.append((node.left, d+1))
            print(f"({node.val}, {node.h})",end="")
            if node.right is not None:
                print("➘",end="")
                q.append((node.right, d+1))
            p = d
        print()

n,q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
e = [(s-x,x,i) for i,(s,_,x) in enumerate(stx)]+[(t-x,x,i) for i,(_,t,x) in enumerate(stx)]
d = [int(input()) for _ in range(q)]
e += [(di,float("inf"),i) for i,di in enumerate(d)]
e.sort(key = lambda x:x[0])
avl = AVLtree((-float("inf"),0))
ans = []
f = [0]*n
for s,x,i in e:
  if x == float("inf"):
    m = avl.lower((-float("inf"),0))
    ans.append(m[0] if m != float("inf") else -1)
  elif f[i]:
    avl.erase(f[i])
  else:
    avl.insert((x,i))
    f[i] = (x,i)
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
class AVLtree:
    """
    __len__ : 長さ
    insert : 挿入
    erase : 削除
    lower(x) : x <= v なる最小のv、なければ-float("inf")、最小値はlower(-float("inf"))
    upper(x) : v < x なる最大のv、なければfloat("inf")、最大値はupper(float("inf"))
    display : デバッグ用
    """

    def __init__(self, x=-float("inf"), root=1):
        self.val = x
        self.left = None
        self.right = None
        self.h = 1
        self.num = 0
        self.root = root

    def __len__(self):
        return self.num

    def _adjusth(self):
        hL = self.left.h if self.left is not None else 0
        hR = self.right.h if self.right is not None else 0
        if hL < hR:
            self.h = hR + 1
        else:
            self.h = hL + 1

    def _rotateL(self):
        nodeR = self.right
        self.right = nodeR.left
        nodeR.left = self
        self._adjusth()
        nodeR._adjusth()
        return nodeR

    def _rotateR(self):
        nodeL = self.left
        self.left = nodeL.right
        nodeL.right = self
        self._adjusth()
        nodeL._adjusth()
        return nodeL

    def _bal(self):
        hL = self.left.h if self.left is not None else 0
        hR = self.right.h if self.right is not None else 0
        return hL - hR

    def _balance(self):
        res = self
        if self._bal() == 2:
            nodeL = self.left
            if nodeL._bal() == -1:
                self.left = nodeL._rotateL()
            res = self._rotateR()
        elif self._bal() == -2:
            nodeR = self.right
            if nodeR._bal() == 1:
                self.right = nodeR._rotateR()
            res = self._rotateL()
        return res

    def insert(self, x):
        node = self
        q = [node]
        while True:
            if node.val < x:
                if node.right is None:
                    node.right = AVLtree(x, 0)
                    break
                node = node.right
            elif node.val > x:
                if node.left is None:
                    node.left = AVLtree(x, 0)
                    break
                node = node.left
            else:
                return
            q.append(node)
            
        self.num += 1
        pre = None
        for node in q[::-1]:
            node._adjusth()
            if pre is not None:
                if pre.val < node.val:
                    node.left = pre
                else:
                    node.right = pre
            if not node.root:
                pre = node._balance()

    def _erase_node(self):
        if self.left is None:
            self = self.right
            return self

        node = self.left
        q = [self]
        while node.right is not None:
            q.append(node)
            node = node.right
            
        self.val = node.val
        if len(q) > 1:
            q[-1].right = None
        else:
            self.left = None
        pre = None
        for node in q[::-1]:
            node._adjusth()
            if pre is not None:
                if pre.val < node.val:
                    node.left = pre
                else:
                    node.right = pre
                node._adjusth()
            pre = node._balance()
        return pre

    def erase(self, x):
        node = self
        q = [node]
        while True:
            if node.val < x:
                if node.right is None:
                    return
                if node.right.val == x:
                    node.right = node.right._erase_node()
                    pre = node.right
                    break
                node = node.right
            elif node.val > x:
                if node.left is None:
                    return
                if node.left.val == x:
                    node.left = node.left._erase_node()
                    pre = node.left
                    break
                node = node.left
            q.append(node)
        self.num -= 1
        for node in q[::-1]:
            node._adjusth()
            if pre is not None:
                if pre.val < node.val:
                    node.left = pre
                else:
                    node.right = pre
                node._adjusth()
            if not node.root:
                pre = node._balance()
    
    def lower(self, x):
        res = float("inf")
        node = self.right
        while node is not None:
            if x <= node.val:
                res = node.val
                node = node.left
            else:
                node = node.right
        return res
    
    def upper(self, x):
        res = -float("inf")
        node = self.right
        while node is not None:
            if x <= node.val:
                node = node.left
            else:
                res = node.val
                node = node.right
        return res
    
    def display(self):
        q = [(self, 0)]
        p = 0
        for node, d in q:
            if d > p:
                print()
            if node.left is not None:
                print("↙",end="")
                q.append((node.left, d+1))
            print(f"({node.val}, {node.h})",end="")
            if node.right is not None:
                print("➘",end="")
                q.append((node.right, d+1))
            p = d
        print()

n,q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
e = [(s-x,x,i) for i,(s,_,x) in enumerate(stx)]+[(t-x,x,i) for i,(_,t,x) in enumerate(stx)]
d = [int(input()) for _ in range(q)]
e += [(di,float("inf"),i) for i,di in enumerate(d)]
e.sort(key = lambda x:x[0])
avl = AVLtree((-float("inf"),0))
ans = []
f = [0]*n
for s,x,i in e:
  if x == float("inf"):
    m = avl.lower((-float("inf"),0))
    ans.append(m[0] if m != float("inf") else -1)
  elif f[i]:
    avl.erase(f[i])
  else:
    avl.insert((x,i))
    f[i] = (x,i)
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, m = map(int,input().split())
k = []
for i in range(n):
    s, t, x = map(int,input().split())
    s -= x
    t -= x
    k.append([s, t, x])

q = []
for i in range(m):
    q.append(int(input()))

k = sorted(k, key=lambda x:(x[2], x[0], x[1]))

d = deque(k)
flg = False
for i in range(m):
    cq = q[i]
    if flg:
        print(-1)
    if i == 0:
        s, t, x = d.popleft()
    while True:
        # print(cq, s, t, x)
        if cq < s:
            print(-1)
            break
        elif s <= cq < t:
            print(x)
            break
        elif d:
            s, t, x = d.popleft()
        else:
            print(-1)
            flg = True
            break

Submission status: Wrong Answer

Solution code:
class AVLtree:
    """
    __len__ : 長さ
    insert : 挿入
    erase : 削除
    lower(x) : x <= v なる最小のv、なければ-float("inf")、最小値はlower(-float("inf"))
    upper(x) : v < x なる最大のv、なければfloat("inf")、最大値はupper(float("inf"))
    display : デバッグ用
    """
 
    def __init__(self, x=-float("inf")):
        self.val = x
        self.left = None
        self.right = None
        self.h = 1
        self.num = 0
 
    def __len__(self):
        return self.num
 
    def _adjusth(self):
        hL = self.left.h if self.left is not None else 0
        hR = self.right.h if self.right is not None else 0
        if hL < hR:
            self.h = hR + 1
        else:
            self.h = hL + 1
 
    def _rotateL(self):
        nodeR = self.right
        self.right = nodeR.left
        nodeR.left = self
        self._adjusth()
        nodeR._adjusth()
        return nodeR
 
    def _rotateR(self):
        nodeL = self.left
        self.left = nodeL.right
        nodeL.right = self
        self._adjusth()
        nodeL._adjusth()
        return nodeL
 
    def _bal(self):
        hL = self.left.h if self.left is not None else 0
        hR = self.right.h if self.right is not None else 0
        return hL - hR
 
    def _balance(self):
        res = self
        if self._bal() == 2:
            nodeL = self.left
            if nodeL._bal() == -1:
                self.left = nodeL._rotateL()
            res = self._rotateR()
        elif self._bal() == -2:
            nodeR = self.right
            if nodeR._bal() == 1:
                self.right = nodeR._rotateR()
            res = self._rotateL()
        return res
 
    def insert(self, x):
        self.num += 1
        node = self
        q = []
        while True:
            if node.val <= x:
                q.append((node, 1))
                if node.right is None:
                    node.right = AVLtree(x)
                    break
                node = node.right
            else:
                q.append((node, 0))
                if node.left is None:
                    node.left = AVLtree(x)
                    break
                node = node.left
        pre = None
        for node, key in q[::-1]:
            if pre is not None:
                if not key:
                    node.left = pre
                else:
                    node.right = pre
            node._adjusth()
            if node.val != -float("inf"):
                pre = node._balance()
 
    def _erase_node(self):
        if self.left is None:
            self = self.right
            return self
 
        node = self.left
        q = [(self, 0)]
        while node.right is not None:
            q.append((node, 1))
            node = node.right
        if len(q) == 1 and node.left is not None:
            q[-1][0].left = node._rotateR()
            node = self.left
            q = [(self, 0)]
            while node.right is not None:
                q.append((node, 1))
                node = node.right
        self.val = node.val
        if len(q) > 1:
            q[-1][0].right = None
        else:
            self.left = None
        pre = None
        for node, key in q[::-1]:
            if pre is not None:
                if not key:
                    node.left = pre
                else:
                    node.right = pre
            node._adjusth()
            pre = node._balance()
        return pre
 
    def erase(self, x):
        node = self
        q = []
        while True:
            if node.val <= x:
                q.append((node, 1))
                if node.right is None:
                    return
                if node.right.val == x:
                    node.right = node.right._erase_node()
                    break
                node = node.right
            else:
                q.append((node, 0))
                if node.left is None:
                    return
                if node.left.val == x:
                    node.left = node.left._erase_node()
                    break
                node = node.left
        self.num -= 1
        pre = None
        for node, key in q[::-1]:
            if pre is not None:
                if not key:
                    node.left = pre
                else:
                    node.right = pre
            node._adjusth()
            if node.val != -float("inf"):
                pre = node._balance()
    
    def lower(self, x):
        res = float("inf")
        node = self.right
        while node is not None:
            if x <= node.val:
                res = node.val
                node = node.left
            else:
                node = node.right
        return res
    
    def upper(self, x):
        res = -float("inf")
        node = self.right
        while node is not None:
            if x <= node.val:
                node = node.left
            else:
                res = node.val
                node = node.right
        return res
    
    def display(self):
        q = [(self, 0)]
        p = 0
        for node, d in q:
            if d > p:
                print()
            if node.left is not None:
                print("↙",end="")
                q.append((node.left, d+1))
            print(f"({node.val}, {node.h})",end="")
            if node.right is not None:
                print("➘",end="")
                q.append((node.right, d+1))
            p = d
        print()
        
n,q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
e = [(s-x,x,0) for s,_,x in stx]+[(t-x,x,1) for _,t,x in stx]
d = [int(input()) for _ in range(q)]
e += [(di,float("inf"),i) for i,di in enumerate(d)]
e.sort()
avl = AVLtree()
ans = [-1]*q
for s,x,i in e:
  if x == float("inf"):
    m = avl.lower(-float("inf"))
    ans[i] = m
  elif i:
    avl.erase(x)
  else:
    avl.insert(x)
for i in ans:
  print(i if i != float("inf") else -1)