from itertools import repeat as _repeat

_INF = 1 << 63
_MAX_TOP = 15  # MAX_LEVEL = 16 (levels 0..15), p = 1/4, (1/p)^16 = 2^32


class _Node:
    __slots__ = ("k", "v", "n0", "n1", "n2", "tower")

    def __init__(self, k, v, n0):
        self.k = k
        self.v = v
        self.n0 = n0


_NIL = _Node(_INF, 0, None)


class SkipList:
    __slots__ = ("_header", "_top", "_len", "_rng", "_update")

    def __init__(self) -> None:
        header = _Node(0, 0, _NIL)
        header.n1 = _NIL
        header.n2 = _NIL
        header.tower = [_NIL] * (_MAX_TOP - 2)
        self._header = header
        self._top = -1
        self._len = 0
        self._rng = 0x853C49E6748FEA9B
        self._update: list = [None] * (_MAX_TOP - 2)

    def __len__(self) -> int:
        return self._len

    def height(self) -> int:
        return self._top + 1

    def level_count(self, level: int) -> int:
        if level < 0 or level > self._top:
            return 0
        nil = _NIL
        count = 0
        if level == 0:
            cur = self._header.n0
            while cur is not nil:
                count += 1
                cur = cur.n0
        elif level == 1:
            cur = self._header.n1
            while cur is not nil:
                count += 1
                cur = cur.n1
        elif level == 2:
            cur = self._header.n2
            while cur is not nil:
                count += 1
                cur = cur.n2
        else:
            idx = level - 3
            cur = self._header.tower[idx]
            while cur is not nil:
                count += 1
                cur = cur.tower[idx]
        return count

    def get(self, key: int) -> int | None:
        cur = self._header
        lvl = self._top
        if lvl >= 3:
            idx = lvl - 3
            tower = cur.tower
            while idx >= 0:
                nxt = tower[idx]
                while nxt.k < key:
                    cur = nxt
                    tower = nxt.tower
                    nxt = tower[idx]
                idx -= 1
            nxt = cur.n2
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n2
            nxt = cur.n1
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n1
            nxt = cur.n0
            while nxt.k < key:
                nxt = nxt.n0
            if nxt.k == key:
                return nxt.v
            return None
        if lvl == 2:
            nxt = cur.n2
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n2
            nxt = cur.n1
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n1
            nxt = cur.n0
            while nxt.k < key:
                nxt = nxt.n0
            if nxt.k == key:
                return nxt.v
            return None
        if lvl == 1:
            nxt = cur.n1
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n1
            nxt = cur.n0
            while nxt.k < key:
                nxt = nxt.n0
            if nxt.k == key:
                return nxt.v
            return None
        if lvl == 0:
            nxt = cur.n0
            while nxt.k < key:
                nxt = nxt.n0
            if nxt.k == key:
                return nxt.v
        return None

    def insert(self, key: int, value: int) -> bool:
        s = (self._rng * 0xD1342543DE82EF95 + 1) & 0xFFFFFFFFFFFFFFFF
        cur = self._header
        lvl = self._top

        # Height 1 (75% of new nodes): no update array, no tower, 1 pointer link
        if s >= 0x4000000000000000:
            if lvl >= 3:
                idx = lvl - 3
                tower = cur.tower
                while idx >= 0:
                    nxt = tower[idx]
                    while nxt.k < key:
                        cur = nxt
                        tower = nxt.tower
                        nxt = tower[idx]
                    idx -= 1
                nxt = cur.n2
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n2
                nxt = cur.n1
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n1
                nxt = cur.n0
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n0
                if nxt.k == key:
                    nxt.v = value
                    return False
                self._rng = s
                cur.n0 = _Node(key, value, nxt)
                self._len += 1
                return True
            if lvl == 2:
                nxt = cur.n2
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n2
            if lvl >= 1:
                nxt = cur.n1
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n1
            nxt = cur.n0
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n0
            if nxt.k == key:
                nxt.v = value
                return False
            self._rng = s
            cur.n0 = _Node(key, value, nxt)
            self._len += 1
            if lvl < 0:
                self._top = 0
            return True

        # Height 2 (18.75% of new nodes): no update array, no tower
        if s >= 0x1000000000000000:
            if lvl >= 3:
                idx = lvl - 3
                tower = cur.tower
                while idx >= 0:
                    nxt = tower[idx]
                    while nxt.k < key:
                        cur = nxt
                        tower = nxt.tower
                        nxt = tower[idx]
                    idx -= 1
                nxt = cur.n2
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n2
                nxt1 = cur.n1
                while nxt1.k < key:
                    cur = nxt1
                    nxt1 = nxt1.n1
                u1 = cur
                nxt0 = cur.n0
                while nxt0.k < key:
                    cur = nxt0
                    nxt0 = nxt0.n0
                if nxt0.k == key:
                    nxt0.v = value
                    return False
                self._rng = s
                node = _Node(key, value, nxt0)
                node.n1 = nxt1
                cur.n0 = node
                u1.n1 = node
                self._len += 1
                return True
            if lvl == 2:
                nxt = cur.n2
                while nxt.k < key:
                    cur = nxt
                    nxt = nxt.n2
            nxt1 = cur.n1
            while nxt1.k < key:
                cur = nxt1
                nxt1 = nxt1.n1
            u1 = cur
            nxt0 = cur.n0
            while nxt0.k < key:
                cur = nxt0
                nxt0 = nxt0.n0
            if nxt0.k == key:
                nxt0.v = value
                return False
            self._rng = s
            node = _Node(key, value, nxt0)
            node.n1 = nxt1
            cur.n0 = node
            u1.n1 = node
            self._len += 1
            if lvl < 1:
                self._top = 1
            return True

        # Height 3 (4.69% of new nodes): no update array, no tower
        if s >= 0x0400000000000000:
            if lvl >= 3:
                idx = lvl - 3
                tower = cur.tower
                while idx >= 0:
                    nxt = tower[idx]
                    while nxt.k < key:
                        cur = nxt
                        tower = nxt.tower
                        nxt = tower[idx]
                    idx -= 1
            nxt2 = cur.n2
            while nxt2.k < key:
                cur = nxt2
                nxt2 = nxt2.n2
            u2 = cur
            nxt1 = cur.n1
            while nxt1.k < key:
                cur = nxt1
                nxt1 = nxt1.n1
            u1 = cur
            nxt0 = cur.n0
            while nxt0.k < key:
                cur = nxt0
                nxt0 = nxt0.n0
            if nxt0.k == key:
                nxt0.v = value
                return False
            self._rng = s
            node = _Node(key, value, nxt0)
            node.n1 = nxt1
            node.n2 = nxt2
            cur.n0 = node
            u1.n1 = node
            u2.n2 = node
            self._len += 1
            if lvl < 2:
                self._top = 2
            return True

        # Height 4..16 (1.56% of new nodes)
        top = (64 - s.bit_length()) >> 1
        if top > 15:
            top = 15
        top_idx = top - 3
        update = self._update
        if lvl >= 3:
            idx = lvl - 3
            tower = cur.tower
            while idx > top_idx:
                nxt = tower[idx]
                while nxt.k < key:
                    cur = nxt
                    tower = nxt.tower
                    nxt = tower[idx]
                idx -= 1
            while idx >= 0:
                nxt = tower[idx]
                while nxt.k < key:
                    cur = nxt
                    tower = nxt.tower
                    nxt = tower[idx]
                update[idx] = tower
                idx -= 1
        nxt2 = cur.n2
        while nxt2.k < key:
            cur = nxt2
            nxt2 = nxt2.n2
        u2 = cur
        nxt1 = cur.n1
        while nxt1.k < key:
            cur = nxt1
            nxt1 = nxt1.n1
        u1 = cur
        nxt0 = cur.n0
        while nxt0.k < key:
            cur = nxt0
            nxt0 = nxt0.n0
        if nxt0.k == key:
            nxt0.v = value
            if lvl >= 3:
                end = lvl - 3
                if end > top_idx:
                    end = top_idx
                while end >= 0:
                    update[end] = None
                    end -= 1
            return False
        self._rng = s
        if top > lvl:
            hdr_tower = self._header.tower
            idx = 0 if lvl < 3 else lvl - 2
            while idx <= top_idx:
                update[idx] = hdr_tower
                idx += 1
            self._top = top
        node = _Node(key, value, nxt0)
        node.n1 = nxt1
        node.n2 = nxt2
        cur.n0 = node
        u1.n1 = node
        u2.n2 = node
        if top_idx == 0:
            ut = update[0]
            node.tower = [ut[0]]
            ut[0] = node
            update[0] = None
        else:
            node_tower = [None] * (top_idx + 1)
            idx = 0
            while idx <= top_idx:
                ut = update[idx]
                node_tower[idx] = ut[idx]
                ut[idx] = node
                update[idx] = None
                idx += 1
            node.tower = node_tower
        self._len += 1
        return True

    def remove(self, key: int) -> bool:
        header = self._header
        first = header.n0
        if first.k == key:
            header.n0 = first.n0
            self._len -= 1
            if header.n1 is not first:
                if header.n0 is _NIL:
                    self._top = -1
                return True
            header.n1 = first.n1
            if header.n2 is not first:
                if self._top == 1 and header.n1 is _NIL:
                    self._top = -1 if header.n0 is _NIL else 0
                return True
            header.n2 = first.n2
            hdr_tower = header.tower
            if hdr_tower[0] is first:
                ft = first.tower
                i = len(ft) - 1
                while i >= 0:
                    hdr_tower[i] = ft[i]
                    i -= 1
            lvl = self._top
            while lvl >= 3 and hdr_tower[lvl - 3] is _NIL:
                lvl -= 1
            if lvl == 2 and header.n2 is _NIL:
                lvl = 1
            if lvl == 1 and header.n1 is _NIL:
                lvl = 0
            if lvl == 0 and header.n0 is _NIL:
                lvl = -1
            self._top = lvl
            return True

        lvl = self._top
        if lvl < 0:
            return False

        cur = header
        if lvl >= 3:
            idx = lvl - 3
            tower = cur.tower
            while idx >= 0:
                nxt = tower[idx]
                while nxt.k < key:
                    cur = nxt
                    tower = nxt.tower
                    nxt = tower[idx]
                if nxt.k == key:
                    target = nxt
                    tt = target.tower
                    tower[idx] = tt[idx]
                    idx -= 1
                    while idx >= 0:
                        nxt = tower[idx]
                        while nxt is not target:
                            cur = nxt
                            tower = nxt.tower
                            nxt = tower[idx]
                        tower[idx] = tt[idx]
                        idx -= 1
                    nxt = cur.n2
                    while nxt is not target:
                        cur = nxt
                        nxt = nxt.n2
                    cur.n2 = target.n2
                    nxt = cur.n1
                    while nxt is not target:
                        cur = nxt
                        nxt = nxt.n1
                    cur.n1 = target.n1
                    nxt = cur.n0
                    while nxt is not target:
                        cur = nxt
                        nxt = nxt.n0
                    cur.n0 = target.n0
                    self._len -= 1
                    hdr_tower = header.tower
                    while lvl >= 3 and hdr_tower[lvl - 3] is _NIL:
                        lvl -= 1
                    if lvl == 2 and header.n2 is _NIL:
                        lvl = 1
                    if lvl == 1 and header.n1 is _NIL:
                        lvl = 0
                    if lvl == 0 and header.n0 is _NIL:
                        lvl = -1
                    self._top = lvl
                    return True
                idx -= 1

        if lvl >= 2:
            nxt = cur.n2
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n2
            if nxt.k == key:
                target = nxt
                cur.n2 = target.n2
                nxt = cur.n1
                while nxt is not target:
                    cur = nxt
                    nxt = nxt.n1
                cur.n1 = target.n1
                nxt = cur.n0
                while nxt is not target:
                    cur = nxt
                    nxt = nxt.n0
                cur.n0 = target.n0
                self._len -= 1
                if lvl == 2 and header.n2 is _NIL:
                    lvl = 1
                    if header.n1 is _NIL:
                        lvl = 0
                        if header.n0 is _NIL:
                            lvl = -1
                    self._top = lvl
                return True

        if lvl >= 1:
            nxt = cur.n1
            while nxt.k < key:
                cur = nxt
                nxt = nxt.n1
            if nxt.k == key:
                target = nxt
                cur.n1 = target.n1
                nxt = cur.n0
                while nxt is not target:
                    cur = nxt
                    nxt = nxt.n0
                cur.n0 = target.n0
                self._len -= 1
                if lvl == 1 and header.n1 is _NIL:
                    lvl = 0
                    if header.n0 is _NIL:
                        lvl = -1
                    self._top = lvl
                return True

        nxt = cur.n0
        while nxt.k < key:
            cur = nxt
            nxt = nxt.n0
        if nxt.k == key:
            cur.n0 = nxt.n0
            self._len -= 1
            if lvl == 0 and header.n0 is _NIL:
                self._top = -1
            return True
        return False

    def range(self, lo: int, hi: int, limit: int) -> tuple[list[int], list[int]]:
        if limit <= 0 or lo > hi:
            return [], []
        lvl = self._top
        if lvl < 0:
            return [], []
        cur = self._header
        if lvl >= 3:
            idx = lvl - 3
            tower = cur.tower
            while idx >= 0:
                nxt = tower[idx]
                while nxt.k < lo:
                    cur = nxt
                    tower = nxt.tower
                    nxt = tower[idx]
                idx -= 1
            nxt = cur.n2
            while nxt.k < lo:
                cur = nxt
                nxt = nxt.n2
            nxt = cur.n1
            while nxt.k < lo:
                cur = nxt
                nxt = nxt.n1
            cur = cur.n0
            while cur.k < lo:
                cur = cur.n0
        else:
            if lvl == 2:
                nxt = cur.n2
                while nxt.k < lo:
                    cur = nxt
                    nxt = nxt.n2
            if lvl >= 1:
                nxt = cur.n1
                while nxt.k < lo:
                    cur = nxt
                    nxt = nxt.n1
            cur = cur.n0
            while cur.k < lo:
                cur = cur.n0

        if cur.k > hi:
            return [], []

        ks: list[int] = []
        vs: list[int] = []
        k_append = ks.append
        v_append = vs.append
        for _ in _repeat(None, limit):
            ck = cur.k
            if ck > hi:
                break
            k_append(ck)
            v_append(cur.v)
            cur = cur.n0
        return ks, vs
