In Love (Codeforces 1883D)
Tags: Greedy, data structures

Initially, you have an empty multiset of segments. You need to process $$$q$$$ operations of two types: $$$+$$$ $$$l$$$ $$$r$$$ — Add the segment $$$(l, r)$$$ to the multiset, $$$-$$$ $$$l$$$ $$$r$$$ — Remove exactly one segment $$$(l, r)$$$ from the multiset. It is guaranteed that this segment exists in the multiset.After each operation, you need to determine if there exists a pair of segments in the multiset that do not intersect. A pair of segments $$$(l, r)$$$ and $$$(a, b)$$$ do not intersect if there does not exist a point $$$x$$$ such that $$$l \leq x \leq r$$$ and $$$a \leq x \leq b$$$.

Input: The first line of each test case contains an integer $$$q$$$ ($$$1 \leq q \leq 10^5$$$) — the number of operations.The next $$$q$$$ lines describe two types of operations. If it is an addition operation, it is given in the format $$$+$$$ $$$l$$$ $$$r$$$. If it is a deletion operation, it is given in the format $$$-$$$ $$$l$$$ $$$r$$$ ($$$1 \leq l \leq r \leq 10^9$$$).

Output: After each operation, print "YES" if there exists a pair of segments in the multiset that do not intersect, and "NO" otherwise.You can print the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.

Note: In the example, after the second, third, fourth, and fifth operations, there exists a pair of segments $$$(1, 2)$$$ and $$$(3, 4)$$$ that do not intersect.Then we remove exactly one segment $$$(3, 4)$$$, and by that time we had two segments. Therefore, the answer after this operation also exists.

Sample Inputs:
12
+ 1 2
+ 3 4
+ 2 3
+ 2 2
+ 3 4
- 3 4
- 3 4
- 1 2
+ 3 4
- 2 2
- 2 3
- 3 4

Sample Outputs:
NO
YES
YES
YES
YES
YES
NO
NO
YES
NO
NO
NO

Solution (Python):
import heapq as hp
from collections import defaultdict
import sys
input = sys.stdin.readline
a = defaultdict(int)
b = defaultdict(int)
c = []
d = []
ans = []
for _ in range(int(input())):
    (i, j, k) = input().split()
    j = -int(j)
    k = int(k)
    if i == '+':
        a[j] += 1
        b[k] += 1
        hp.heappush(c, j)
        hp.heappush(d, k)
    else:
        a[j] -= 1
        b[k] -= 1
    while c and a[c[0]] < 1:
        hp.heappop(c)
    while d and b[d[0]] < 1:
        hp.heappop(d)
    ans.append('YES' if c and d[0] < -c[0] else 'NO')
print('\n'.join(ans))

Solution (Python):
import heapq as hp
from collections import defaultdict
import sys
input = sys.stdin.readline
print = sys.stdout.write
a = defaultdict(int)
b = defaultdict(int)
c = []
d = []
ans = []
for _ in range(int(input())):
    (i, j, k) = input().split()
    j = -int(j)
    k = int(k)
    if i == '+':
        a[j] += 1
        b[k] += 1
        hp.heappush(c, j)
        hp.heappush(d, k)
    else:
        a[j] -= 1
        b[k] -= 1
    while c and a[c[0]] < 1:
        hp.heappop(c)
    while d and b[d[0]] < 1:
        hp.heappop(d)
    ans.append('YES' if c and d[0] < -c[0] else 'NO')
print('\n'.join(ans))

Solution (Python):
from collections import defaultdict
(minr, maxl) = (99999999999999999999, -1)
(cntl, cntr) = (defaultdict(int), defaultdict(int))
n = int(input())
ans = []
for _ in range(n):
    (a, b, c) = input().split()
    (b, c) = (int(b), int(c))
    if a == '+':
        maxl = max(maxl, b)
        minr = min(minr, c)
        cntl[b] += 1
        cntr[c] += 1
    else:
        if b == maxl and cntl[b] == 1:
            cntl.pop(b)
            if len(cntl) > 0:
                maxl = sorted(cntl.keys())[-1]
            else:
                maxl = -1
        elif b == maxl and cntl[b] > 1:
            cntl[b] -= 1
        elif cntl[b] == 1:
            cntl.pop(b)
        else:
            cntl[b] -= 1
        if c == minr and cntr[c] == 1:
            cntr.pop(c)
            if len(cntr) > 0:
                minr = sorted(cntr.keys())[0]
            else:
                minr = 99999999999999999999
        elif c == minr and cntr[c] > 1:
            cntr[c] -= 1
        elif cntr[c] == 1:
            cntr.pop(c)
        else:
            cntr[c] -= 1
    if maxl > minr:
        ans.append('YES')
    else:
        ans.append('NO')
print('\n'.join(ans))