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:
    """
    __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
            
        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)

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:
                if node.right is None:
                    node.right = AVLtree(x)
                    break
                q.append((node, 1))
                node = node.right
            else:
                if node.left is None:
                    node.left = AVLtree(x)
                    break
                q.append((node, 0))
                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
            
        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:
                if node.right is None:
                    return
                if node.right.val == x:
                    node.right = node.right._erase_node()
                    break
                q.append((node, 1))
                node = node.right
            else:
                if node.left is None:
                    return
                if node.left.val == x:
                    node.left = node.left._erase_node()
                    break
                q.append((node, 0))
                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)

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 = [node]
        while True:
            if node.val <= x:
                if node.right is None:
                    node.right = AVLtree(x)
                    break
                node = node.right
            else:
                if node.left is None:
                    node.left = AVLtree(x)
                    break
                node = node.left
            q.append(node)

        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 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]
        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 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)

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 = [node]
        while True:
            if node.val <= x:
                if node.right is None:
                    node.right = AVLtree(x)
                    break
                node = node.right
            else:
                if node.left is None:
                    node.left = AVLtree(x)
                    break
                node = node.left
            q.append(node)

        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 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]
        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 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
            
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()
avl = AVLtree()
ans = [-1]*q
f = [0]*n
for s,x,i in e:
  if x == float("inf"):
    m = avl.lower(-float("inf"))
    ans[i] = m
  elif f[i]:
    avl.erase(f[i])
  else:
    avl.insert(x)
    f[i] = x
for i in ans:
  print(i if i != float("inf") else -1)

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 = [node]
        while True:
            if node.val < x:
                if node.right is None:
                    node.right = AVLtree(x)
                    break
                node = node.right
            elif node.val > x:
                if node.left is None:
                    node.left = AVLtree(x)
                    break
                node = node.left
            else:
                return
            q.append(node)

        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 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]
        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):
        self.num -= 1
        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)
        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 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
            
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 = [-1]*q
f = [0]*n
for s,x,i in e:
  if x == float("inf"):
    m = avl.lower(-float("inf"))
    ans[i] = m
  elif f[i]:
    avl.erase(f[i])
  else:
    avl.insert(x)
    f[i] = x
for i in ans:
  print(i if i != float("inf") else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappop,heappush
N,Q = map(int,input().split())
event = []
for _ in range(N):
    s,t,x = map(int,input().split())
    event.append((s-x,1,x))
    event.append((t-x,2,x))

for i in range(Q):
    d = int(input())
    event.append((d,3,i))
event.sort()
delMemo = set()
nowStop = []
ans = [-1]*Q
while event:
    t,e,x = heappop(event)
    if e == 1:
        heappush(nowStop,x)
    if e == 2:
        delMemo.add(x)
    if e == 3:
        while nowStop:
            s = nowStop[0]
            if s in delMemo:
                heappop(nowStop)
                delMemo.remove(s)
            else:
                ans[x] = s
                break
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
from heapq import heappop,heappush
N,Q = map(int,input().split())
eventQue = []
for _ in range(N):
    s,t,x = map(int,input().split())
    heappush(eventQue,(s-x,1,x))
    heappush(eventQue,(t-x,2,x))

for _ in range(Q):
    d = int(input())
    heappush(eventQue,(d,3,0))

delMemo = set()
nowStop = []
while eventQue:
    t,e,x = heappop(eventQue)
    if e == 1:
        heappush(nowStop,x)
    if e == 2:
        delMemo.add(x)
    if e == 3:
        while nowStop:
            x = nowStop[0]
            if x in delMemo:
                heappop(nowStop)
                delMemo.remove(x)
            else:
                print(x)
                break
        else:
            print(-1)