|
| 1 | +import sys |
| 2 | +import math |
| 3 | +from collections import Counter |
| 4 | +from bisect import bisect_left, bisect_right |
| 5 | +from typing import Generic, Iterable, Iterator, List, TypeVar, Union |
| 6 | + |
| 7 | +T = TypeVar('T') |
| 8 | + |
| 9 | +class SortedSet(Generic[T]): |
| 10 | + BUCKET_RATIO = 50 |
| 11 | + REBUILD_RATIO = 170 |
| 12 | + |
| 13 | + def _build(self, a=None) -> None: |
| 14 | + "Evenly divide `a` into buckets." |
| 15 | + if a is None: a = list(self) |
| 16 | + size = self.size = len(a) |
| 17 | + bucket_size = int(math.ceil(math.sqrt(size / self.BUCKET_RATIO))) |
| 18 | + self.a = [a[size * i // bucket_size : size * (i + 1) // bucket_size] for i in range(bucket_size)] |
| 19 | + |
| 20 | + def __init__(self, a: Iterable[T] = []) -> None: |
| 21 | + "Make a new SortedSet from iterable. / O(N) if sorted and unique / O(N log N)" |
| 22 | + a = list(a) |
| 23 | + if not all(a[i] < a[i + 1] for i in range(len(a) - 1)): |
| 24 | + a = sorted(set(a)) |
| 25 | + self._build(a) |
| 26 | + |
| 27 | + def __iter__(self) -> Iterator[T]: |
| 28 | + for i in self.a: |
| 29 | + for j in i: yield j |
| 30 | + |
| 31 | + def __reversed__(self) -> Iterator[T]: |
| 32 | + for i in reversed(self.a): |
| 33 | + for j in reversed(i): yield j |
| 34 | + |
| 35 | + def __len__(self) -> int: |
| 36 | + return self.size |
| 37 | + |
| 38 | + def __repr__(self) -> str: |
| 39 | + return "SortedSet" + str(self.a) |
| 40 | + |
| 41 | + def __str__(self) -> str: |
| 42 | + s = str(list(self)) |
| 43 | + return "{" + s[1 : len(s) - 1] + "}" |
| 44 | + |
| 45 | + def _find_bucket(self, x: T) -> List[T]: |
| 46 | + "Find the bucket which should contain x. self must not be empty." |
| 47 | + for a in self.a: |
| 48 | + if x <= a[-1]: return a |
| 49 | + return a |
| 50 | + |
| 51 | + def __contains__(self, x: T) -> bool: |
| 52 | + if self.size == 0: return False |
| 53 | + a = self._find_bucket(x) |
| 54 | + i = bisect_left(a, x) |
| 55 | + return i != len(a) and a[i] == x |
| 56 | + |
| 57 | + def add(self, x: T) -> bool: |
| 58 | + "Add an element and return True if added. / O(√N)" |
| 59 | + if self.size == 0: |
| 60 | + self.a = [[x]] |
| 61 | + self.size = 1 |
| 62 | + return True |
| 63 | + a = self._find_bucket(x) |
| 64 | + i = bisect_left(a, x) |
| 65 | + if i != len(a) and a[i] == x: return False |
| 66 | + a.insert(i, x) |
| 67 | + self.size += 1 |
| 68 | + if len(a) > len(self.a) * self.REBUILD_RATIO: |
| 69 | + self._build() |
| 70 | + return True |
| 71 | + |
| 72 | + def discard(self, x: T) -> bool: |
| 73 | + "Remove an element and return True if removed. / O(√N)" |
| 74 | + if self.size == 0: return False |
| 75 | + a = self._find_bucket(x) |
| 76 | + i = bisect_left(a, x) |
| 77 | + if i == len(a) or a[i] != x: return False |
| 78 | + a.pop(i) |
| 79 | + self.size -= 1 |
| 80 | + if len(a) == 0: self._build() |
| 81 | + return True |
| 82 | + |
| 83 | + def lt(self, x: T) -> Union[T, None]: |
| 84 | + "Find the largest element < x, or None if it doesn't exist." |
| 85 | + for a in reversed(self.a): |
| 86 | + if a[0] < x: |
| 87 | + return a[bisect_left(a, x) - 1] |
| 88 | + |
| 89 | + def le(self, x: T) -> Union[T, None]: |
| 90 | + "Find the largest element <= x, or None if it doesn't exist." |
| 91 | + for a in reversed(self.a): |
| 92 | + if a[0] <= x: |
| 93 | + return a[bisect_right(a, x) - 1] |
| 94 | + |
| 95 | + def gt(self, x: T) -> Union[T, None]: |
| 96 | + "Find the smallest element > x, or None if it doesn't exist." |
| 97 | + for a in self.a: |
| 98 | + if a[-1] > x: |
| 99 | + return a[bisect_right(a, x)] |
| 100 | + |
| 101 | + def ge(self, x: T) -> Union[T, None]: |
| 102 | + "Find the smallest element >= x, or None if it doesn't exist." |
| 103 | + for a in self.a: |
| 104 | + if a[-1] >= x: |
| 105 | + return a[bisect_left(a, x)] |
| 106 | + |
| 107 | + def __getitem__(self, x: int) -> T: |
| 108 | + "Return the x-th element, or IndexError if it doesn't exist." |
| 109 | + if x < 0: x += self.size |
| 110 | + if x < 0: raise IndexError |
| 111 | + for a in self.a: |
| 112 | + if x < len(a): return a[x] |
| 113 | + x -= len(a) |
| 114 | + raise IndexError |
| 115 | + |
| 116 | + def index(self, x: T) -> int: |
| 117 | + "Count the number of elements < x." |
| 118 | + ans = 0 |
| 119 | + for a in self.a: |
| 120 | + if a[-1] >= x: |
| 121 | + return ans + bisect_left(a, x) |
| 122 | + ans += len(a) |
| 123 | + return ans |
| 124 | + |
| 125 | + def index_right(self, x: T) -> int: |
| 126 | + "Count the number of elements <= x." |
| 127 | + ans = 0 |
| 128 | + for a in self.a: |
| 129 | + if a[-1] > x: |
| 130 | + return ans + bisect_right(a, x) |
| 131 | + ans += len(a) |
| 132 | + return ans |
| 133 | + |
| 134 | +# region fastio |
| 135 | +input = lambda: sys.stdin.readline().rstrip() |
| 136 | +sint = lambda: int(input()) |
| 137 | +mint = lambda: map(int, input().split()) |
| 138 | +ints = lambda: list(map(int, input().split())) |
| 139 | +# endregion fastio |
| 140 | + |
| 141 | +# MOD = 998_244_353 |
| 142 | +# MOD = 10 ** 9 + 7 |
| 143 | +# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1)) |
| 144 | +# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)) |
| 145 | + |
| 146 | + |
| 147 | +def solve() -> None: |
| 148 | + ss = SortedSet() |
| 149 | + q, k = mint() |
| 150 | + right = Counter() |
| 151 | + left = Counter() |
| 152 | + for _ in range(q): |
| 153 | + t, x = mint() |
| 154 | + if t == 1: |
| 155 | + l = ss.lt(x) |
| 156 | + r = ss.gt(x) |
| 157 | + if x in ss: |
| 158 | + ss.discard(x) |
| 159 | + if l is not None: |
| 160 | + right[l] = 0 if r is None or r > l + k else right[r] + 1 |
| 161 | + if r is not None: |
| 162 | + left[r] = 0 if l is None or l < r - k else left[l] + 1 |
| 163 | + left[x] = right[x] = 0 |
| 164 | + else: |
| 165 | + ss.add(x) |
| 166 | + if l is not None: |
| 167 | + right[l] = 0 if x > l + k else 1 if r is None or r > x + k else right[r] + 2 |
| 168 | + if r is not None: |
| 169 | + left[r] = 0 if x < r - k else 1 if l is None or l < x - k else left[l] + 1 |
| 170 | + left[x] = 0 if l is None or l < x - k else left[l] + 1 |
| 171 | + right[x] = 0 if r is None or r > x + k else right[r] + 1 |
| 172 | + else: |
| 173 | + print(left[x] + right[x] + 1) |
| 174 | + |
| 175 | + |
| 176 | +solve() |
0 commit comments