import java.util.Arrays;

public final class BTree {
    // Minimum degree t = 16 (fixed by specification).
    private static final int MIN_KEYS = 15; // t - 1
    private static final int MAX_KEYS = 31; // 2t - 1

    // Leaf pool layout:
    // Each leaf occupies 64 longs (512 bytes = 4 x 128-byte M3 cache lines).
    // With a 16-byte array header, offset 62 longs (496 bytes) places node 0 at byte 512,
    // so every leaf is 512-byte aligned (128-byte aligned and never crosses a 16 KB page).
    //   leafKV[base + 0]        : key count (or free-list next id when freed)
    //   leafKV[base + 1 .. 31]  : sorted keys; unused tail [count+1 .. 31] = Long.MAX_VALUE
    //   leafKV[base + 33 .. 63] : corresponding values (at keyIndex + 32)
    private static final int LEAF_SHIFT = 6;
    private static final int LEAF_OFFSET = 62;
    private static final int MAX_LEAF_CAP = (Integer.MAX_VALUE - LEAF_OFFSET) >>> LEAF_SHIFT;

    // Internal node pool layout (separate from leaves so internal routing stays in L2 cache):
    //   intKeys[base + 0]       : key count (or free-list next id when freed)
    //   intKeys[base + 1 .. 31] : sorted keys; unused tail [count+1 .. 31] = Long.MAX_VALUE
    //   intVals[base + 1 .. 31] : corresponding values
    //   intChildren[cbase .. cbase + 31] : child node ids (32 ints = 128 bytes = 1 cache line)
    // Offsets align intKeys/intVals to 256 bytes and intChildren to 128 bytes on G1 humongous regions.
    // Note: cbase = (id << 5) + 28 = base - 2, so child index `idx` for `pos = base + 1 + idx`
    // is simply `pos - 3`.
    private static final int INT_SHIFT = 5;
    private static final int INT_KEY_OFFSET = 30;
    private static final int INT_CHILD_OFFSET = 28;
    private static final int MAX_INT_CAP = (Integer.MAX_VALUE - INT_KEY_OFFSET) >>> INT_SHIFT;

    private long[] leafKV;
    private int leafCap;
    private int leafAllocated;
    private int leafFreeHead = -1;

    private long[] intKeys;
    private long[] intVals;
    private int[] intChildren;
    private int intCap;
    private int intAllocated;
    private int intFreeHead = -1;

    private int root = -1;
    private int height;
    private int size;

    // Reusable traversal stack (max possible B-tree height with t=16 and int size is 9).
    private final int[] pathNode = new int[16];
    private final int[] pathIdx = new int[16];

    public BTree() {
    }

    public int size() {
        return this.size;
    }

    public int height() {
        return this.height;
    }

    // ------------------------------------------------------------------ pool management

    private int allocLeaf() {
        int id = this.leafFreeHead;
        if (id >= 0) {
            this.leafFreeHead = (int) this.leafKV[(id << LEAF_SHIFT) + LEAF_OFFSET];
            return id;
        }
        int next = this.leafAllocated;
        if (next == this.leafCap) {
            growLeaf();
        }
        this.leafAllocated = next + 1;
        return next;
    }

    private void growLeaf() {
        int oldCap = this.leafCap;
        int newCap = (oldCap == 0) ? 16 : (oldCap > (MAX_LEAF_CAP >>> 1) ? MAX_LEAF_CAP : (oldCap << 1));
        int newLen = (newCap << LEAF_SHIFT) + LEAF_OFFSET;
        this.leafKV = (oldCap == 0) ? new long[newLen] : Arrays.copyOf(this.leafKV, newLen);
        this.leafCap = newCap;
    }

    private void freeLeaf(int id) {
        this.leafKV[(id << LEAF_SHIFT) + LEAF_OFFSET] = this.leafFreeHead;
        this.leafFreeHead = id;
    }

    private int allocInt() {
        int id = this.intFreeHead;
        if (id >= 0) {
            this.intFreeHead = (int) this.intKeys[(id << INT_SHIFT) + INT_KEY_OFFSET];
            return id;
        }
        int next = this.intAllocated;
        if (next == this.intCap) {
            growInt();
        }
        this.intAllocated = next + 1;
        return next;
    }

    private void growInt() {
        int oldCap = this.intCap;
        int newCap = (oldCap == 0) ? 4 : (oldCap > (MAX_INT_CAP >>> 1) ? MAX_INT_CAP : (oldCap << 1));
        int newKeyLen = (newCap << INT_SHIFT) + INT_KEY_OFFSET;
        int newChildLen = (newCap << INT_SHIFT) + INT_CHILD_OFFSET;
        if (oldCap == 0) {
            this.intKeys = new long[newKeyLen];
            this.intVals = new long[newKeyLen];
            this.intChildren = new int[newChildLen];
        } else {
            this.intKeys = Arrays.copyOf(this.intKeys, newKeyLen);
            this.intVals = Arrays.copyOf(this.intVals, newKeyLen);
            this.intChildren = Arrays.copyOf(this.intChildren, newChildLen);
        }
        this.intCap = newCap;
    }

    private void freeInt(int id) {
        this.intKeys[(id << INT_SHIFT) + INT_KEY_OFFSET] = this.intFreeHead;
        this.intFreeHead = id;
    }

    // ------------------------------------------------------------------ get

    public long get(long key, long missing) {
        int h = this.height;
        if (h == 0) {
            return missing;
        }
        int node = this.root;
        if (h > 1) {
            final long[] ik = this.intKeys;
            final int[] ic = this.intChildren;
            do {
                int base = (node << INT_SHIFT) + INT_KEY_OFFSET;
                int end = base + (int) ik[base]; // cache line 0
                int lo = base + 1;
                if (ik[lo + 15] < key) lo += 16; // cache line 1 (parallel with line 0)
                if (ik[lo + 7] < key) lo += 8;
                if (ik[lo + 3] < key) lo += 4;
                if (ik[lo + 1] < key) lo += 2;
                if (ik[lo] < key) lo += 1;
                if (lo <= end && ik[lo] == key) {
                    return this.intVals[lo];
                }
                node = ic[lo - 3];
                h--;
            } while (h > 1);
        }
        final long[] lk = this.leafKV;
        int base = (node << LEAF_SHIFT) + LEAF_OFFSET;
        int end = base + (int) lk[base];
        int lo = base + 1;
        if (lk[lo + 15] < key) lo += 16;
        if (lk[lo + 7] < key) lo += 8;
        if (lk[lo + 3] < key) lo += 4;
        if (lk[lo + 1] < key) lo += 2;
        if (lk[lo] < key) lo += 1;
        if (lo <= end && lk[lo] == key) {
            return lk[lo + 32];
        }
        return missing;
    }

    // ------------------------------------------------------------------ insert

    public boolean insert(long key, long value) {
        int h = this.height;
        if (h == 0) {
            insertEmpty(key, value);
            return true;
        }
        int node = this.root;
        int depth = 0;
        if (h > 1) {
            final long[] ik = this.intKeys;
            final int[] ic = this.intChildren;
            final int[] pNode = this.pathNode;
            final int[] pIdx = this.pathIdx;
            do {
                int base = (node << INT_SHIFT) + INT_KEY_OFFSET;
                int end = base + (int) ik[base];
                int lo = base + 1;
                if (ik[lo + 15] < key) lo += 16;
                if (ik[lo + 7] < key) lo += 8;
                if (ik[lo + 3] < key) lo += 4;
                if (ik[lo + 1] < key) lo += 2;
                if (ik[lo] < key) lo += 1;
                if (lo <= end && ik[lo] == key) {
                    this.intVals[lo] = value;
                    return false;
                }
                pNode[depth] = node;
                pIdx[depth] = lo - base - 1;
                depth++;
                node = ic[lo - 3];
                h--;
            } while (h > 1);
        }
        final long[] lk = this.leafKV;
        int base = (node << LEAF_SHIFT) + LEAF_OFFSET;
        int count = (int) lk[base];
        int end = base + count;
        int lo = base + 1;
        if (lk[lo + 15] < key) lo += 16;
        if (lk[lo + 7] < key) lo += 8;
        if (lk[lo + 3] < key) lo += 4;
        if (lk[lo + 1] < key) lo += 2;
        if (lk[lo] < key) lo += 1;
        if (lo <= end && lk[lo] == key) {
            lk[lo + 32] = value;
            return false;
        }
        this.size++;
        if (count < MAX_KEYS) {
            for (int j = end; j >= lo; j--) {
                lk[j + 1] = lk[j];
                lk[j + 33] = lk[j + 32];
            }
            lk[lo] = key;
            lk[lo + 32] = value;
            lk[base] = count + 1;
            return true;
        }
        splitLeafAndPropagate(node, lo - base - 1, key, value, depth);
        return true;
    }

    private void insertEmpty(long key, long value) {
        int r = allocLeaf();
        final long[] lk = this.leafKV;
        int base = (r << LEAF_SHIFT) + LEAF_OFFSET;
        lk[base] = 1;
        lk[base + 1] = key;
        lk[base + 33] = value;
        for (int i = base + 2; i <= base + MAX_KEYS; i++) {
            lk[i] = Long.MAX_VALUE;
        }
        this.root = r;
        this.height = 1;
        this.size = 1;
    }

    private void splitLeafAndPropagate(int leftLeaf, int insertIdx, long key, long value, int depth) {
        int rightLeaf = allocLeaf();
        final long[] lk = this.leafKV;
        int lBase = (leftLeaf << LEAF_SHIFT) + LEAF_OFFSET;
        int rBase = (rightLeaf << LEAF_SHIFT) + LEAF_OFFSET;

        long medKey;
        long medVal;
        if (insertIdx < 16) {
            medKey = lk[lBase + 16];
            medVal = lk[lBase + 48];
            System.arraycopy(lk, lBase + 17, lk, rBase + 1, 15);
            System.arraycopy(lk, lBase + 49, lk, rBase + 33, 15);
            int pos = lBase + 1 + insertIdx;
            for (int j = lBase + 15; j >= pos; j--) {
                lk[j + 1] = lk[j];
                lk[j + 33] = lk[j + 32];
            }
            lk[pos] = key;
            lk[pos + 32] = value;
        } else if (insertIdx == 16) {
            medKey = key;
            medVal = value;
            System.arraycopy(lk, lBase + 17, lk, rBase + 1, 15);
            System.arraycopy(lk, lBase + 49, lk, rBase + 33, 15);
        } else {
            medKey = lk[lBase + 17];
            medVal = lk[lBase + 49];
            int rIdx = insertIdx - 17;
            if (rIdx > 0) {
                System.arraycopy(lk, lBase + 18, lk, rBase + 1, rIdx);
                System.arraycopy(lk, lBase + 50, lk, rBase + 33, rIdx);
            }
            int rPos = rBase + 1 + rIdx;
            lk[rPos] = key;
            lk[rPos + 32] = value;
            int tail = 14 - rIdx;
            if (tail > 0) {
                System.arraycopy(lk, lBase + 1 + insertIdx, lk, rPos + 1, tail);
                System.arraycopy(lk, lBase + 33 + insertIdx, lk, rPos + 33, tail);
            }
        }
        lk[lBase] = 16;
        for (int i = lBase + 17; i <= lBase + MAX_KEYS; i++) {
            lk[i] = Long.MAX_VALUE;
        }
        lk[rBase] = 15;
        for (int i = rBase + 16; i <= rBase + MAX_KEYS; i++) {
            lk[i] = Long.MAX_VALUE;
        }

        int rightChild = rightLeaf;
        final int[] pNodeArr = this.pathNode;
        final int[] pIdxArr = this.pathIdx;
        while (depth > 0) {
            depth--;
            int parent = pNodeArr[depth];
            int pIdx = pIdxArr[depth];
            long[] ik = this.intKeys;
            long[] iv = this.intVals;
            int[] ic = this.intChildren;
            int pBase = (parent << INT_SHIFT) + INT_KEY_OFFSET;
            int pCBase = pBase - 2;
            int pCount = (int) ik[pBase];
            if (pCount < MAX_KEYS) {
                int pos = pBase + 1 + pIdx;
                for (int j = pBase + pCount; j >= pos; j--) {
                    ik[j + 1] = ik[j];
                    iv[j + 1] = iv[j];
                }
                ik[pos] = medKey;
                iv[pos] = medVal;
                int cPos = pCBase + pIdx + 1;
                for (int j = pCBase + pCount; j >= cPos; j--) {
                    ic[j + 1] = ic[j];
                }
                ic[cPos] = rightChild;
                ik[pBase] = pCount + 1;
                return;
            }

            // Parent is full: split parent into parent (16 keys, 17 children) + newRight (15 keys, 16 children).
            int newRight = allocInt();
            ik = this.intKeys;
            iv = this.intVals;
            ic = this.intChildren;
            int nrBase = (newRight << INT_SHIFT) + INT_KEY_OFFSET;
            int nrCBase = nrBase - 2;

            long nextMedKey;
            long nextMedVal;
            if (pIdx < 16) {
                nextMedKey = ik[pBase + 16];
                nextMedVal = iv[pBase + 16];
                System.arraycopy(ik, pBase + 17, ik, nrBase + 1, 15);
                System.arraycopy(iv, pBase + 17, iv, nrBase + 1, 15);
                int pos = pBase + 1 + pIdx;
                for (int j = pBase + 15; j >= pos; j--) {
                    ik[j + 1] = ik[j];
                    iv[j + 1] = iv[j];
                }
                ik[pos] = medKey;
                iv[pos] = medVal;

                System.arraycopy(ic, pCBase + 16, ic, nrCBase, 16);
                int cPos = pCBase + pIdx + 1;
                for (int j = pCBase + 15; j >= cPos; j--) {
                    ic[j + 1] = ic[j];
                }
                ic[cPos] = rightChild;
            } else if (pIdx == 16) {
                nextMedKey = medKey;
                nextMedVal = medVal;
                System.arraycopy(ik, pBase + 17, ik, nrBase + 1, 15);
                System.arraycopy(iv, pBase + 17, iv, nrBase + 1, 15);
                ic[nrCBase] = rightChild;
                System.arraycopy(ic, pCBase + 17, ic, nrCBase + 1, 15);
            } else {
                nextMedKey = ik[pBase + 17];
                nextMedVal = iv[pBase + 17];
                int rIdx = pIdx - 17;
                if (rIdx > 0) {
                    System.arraycopy(ik, pBase + 18, ik, nrBase + 1, rIdx);
                    System.arraycopy(iv, pBase + 18, iv, nrBase + 1, rIdx);
                }
                int nrPos = nrBase + 1 + rIdx;
                ik[nrPos] = medKey;
                iv[nrPos] = medVal;
                int tail = 14 - rIdx;
                if (tail > 0) {
                    System.arraycopy(ik, pBase + 1 + pIdx, ik, nrPos + 1, tail);
                    System.arraycopy(iv, pBase + 1 + pIdx, iv, nrPos + 1, tail);
                }

                System.arraycopy(ic, pCBase + 17, ic, nrCBase, rIdx + 1);
                ic[nrCBase + rIdx + 1] = rightChild;
                if (tail > 0) {
                    System.arraycopy(ic, pCBase + pIdx + 1, ic, nrCBase + rIdx + 2, tail);
                }
            }

            ik[pBase] = 16;
            for (int i = pBase + 17; i <= pBase + MAX_KEYS; i++) {
                ik[i] = Long.MAX_VALUE;
            }
            ik[nrBase] = 15;
            for (int i = nrBase + 16; i <= nrBase + MAX_KEYS; i++) {
                ik[i] = Long.MAX_VALUE;
            }

            medKey = nextMedKey;
            medVal = nextMedVal;
            rightChild = newRight;
        }

        // Root split: create a new internal root.
        int newRoot = allocInt();
        final long[] ik = this.intKeys;
        final long[] iv = this.intVals;
        final int[] ic = this.intChildren;
        int rootBase = (newRoot << INT_SHIFT) + INT_KEY_OFFSET;
        ik[rootBase] = 1;
        ik[rootBase + 1] = medKey;
        iv[rootBase + 1] = medVal;
        for (int i = rootBase + 2; i <= rootBase + MAX_KEYS; i++) {
            ik[i] = Long.MAX_VALUE;
        }
        ic[rootBase - 2] = this.root;
        ic[rootBase - 1] = rightChild;
        this.root = newRoot;
        this.height++;
    }

    // ------------------------------------------------------------------ remove

    public boolean remove(long key) {
        int h = this.height;
        if (h == 0) {
            return false;
        }
        int node = this.root;
        int depth = 0;
        if (h > 1) {
            final long[] ik = this.intKeys;
            final int[] ic = this.intChildren;
            final int[] pNode = this.pathNode;
            final int[] pIdx = this.pathIdx;
            do {
                int base = (node << INT_SHIFT) + INT_KEY_OFFSET;
                int end = base + (int) ik[base];
                int lo = base + 1;
                if (ik[lo + 15] < key) lo += 16;
                if (ik[lo + 7] < key) lo += 8;
                if (ik[lo + 3] < key) lo += 4;
                if (ik[lo + 1] < key) lo += 2;
                if (ik[lo] < key) lo += 1;
                if (lo <= end && ik[lo] == key) {
                    removeFromInternal(node, base, lo, depth, h);
                    return true;
                }
                pNode[depth] = node;
                pIdx[depth] = lo - base - 1;
                depth++;
                node = ic[lo - 3];
                h--;
            } while (h > 1);
        }
        final long[] lk = this.leafKV;
        int base = (node << LEAF_SHIFT) + LEAF_OFFSET;
        int count = (int) lk[base];
        int end = base + count;
        int lo = base + 1;
        if (lk[lo + 15] < key) lo += 16;
        if (lk[lo + 7] < key) lo += 8;
        if (lk[lo + 3] < key) lo += 4;
        if (lk[lo + 1] < key) lo += 2;
        if (lk[lo] < key) lo += 1;
        if (lo > end || lk[lo] != key) {
            return false;
        }
        this.size--;
        for (int j = lo; j < end; j++) {
            lk[j] = lk[j + 1];
            lk[j + 32] = lk[j + 33];
        }
        lk[end] = Long.MAX_VALUE;
        int newCount = count - 1;
        lk[base] = newCount;
        if (newCount >= MIN_KEYS) {
            return true;
        }
        if (depth == 0) {
            if (newCount == 0) {
                freeLeaf(node);
                this.root = -1;
                this.height = 0;
            }
            return true;
        }
        rebalanceAfterLeafDelete(node, depth);
        return true;
    }

    private void removeFromInternal(int targetNode, int targetBase, int targetLo, int depth, int h) {
        this.size--;
        final long[] ik = this.intKeys;
        final int[] ic = this.intChildren;
        final int[] pNode = this.pathNode;
        final int[] pIdx = this.pathIdx;

        // Descend to the in-order predecessor (rightmost entry of left child).
        pNode[depth] = targetNode;
        pIdx[depth] = targetLo - targetBase - 1;
        depth++;
        int cur = ic[targetLo - 3];
        h--;
        while (h > 1) {
            int cBase = (cur << INT_SHIFT) + INT_KEY_OFFSET;
            int cCount = (int) ik[cBase];
            pNode[depth] = cur;
            pIdx[depth] = cCount;
            depth++;
            cur = ic[cBase - 2 + cCount];
            h--;
        }

        final long[] lk = this.leafKV;
        int lBase = (cur << LEAF_SHIFT) + LEAF_OFFSET;
        int lCount = (int) lk[lBase];
        int lastPos = lBase + lCount;
        ik[targetLo] = lk[lastPos];
        this.intVals[targetLo] = lk[lastPos + 32];
        lk[lastPos] = Long.MAX_VALUE;
        int newCount = lCount - 1;
        lk[lBase] = newCount;
        if (newCount < MIN_KEYS) {
            rebalanceAfterLeafDelete(cur, depth);
        }
    }

    private void rebalanceAfterLeafDelete(int leaf, int depth) {
        depth--;
        int parent = this.pathNode[depth];
        int pIdx = this.pathIdx[depth];
        final long[] ik = this.intKeys;
        final long[] iv = this.intVals;
        final int[] ic = this.intChildren;
        final long[] lk = this.leafKV;

        int pBase = (parent << INT_SHIFT) + INT_KEY_OFFSET;
        int pCBase = pBase - 2;
        int pCount = (int) ik[pBase];

        // Try borrowing (redistributing) from left sibling.
        if (pIdx > 0) {
            int leftSib = ic[pCBase + pIdx - 1];
            int lsBase = (leftSib << LEAF_SHIFT) + LEAF_OFFSET;
            int lsCount = (int) lk[lsBase];
            if (lsCount > MIN_KEYS) {
                int move = (lsCount - (MIN_KEYS - 1)) >>> 1;
                int lBase = (leaf << LEAF_SHIFT) + LEAF_OFFSET;
                System.arraycopy(lk, lBase + 1, lk, lBase + 1 + move, MIN_KEYS - 1);
                System.arraycopy(lk, lBase + 33, lk, lBase + 33 + move, MIN_KEYS - 1);
                int sepPos = pBase + pIdx;
                lk[lBase + move] = ik[sepPos];
                lk[lBase + 32 + move] = iv[sepPos];
                int newSepPos = lsBase + 1 + lsCount - move;
                if (move > 1) {
                    System.arraycopy(lk, newSepPos + 1, lk, lBase + 1, move - 1);
                    System.arraycopy(lk, newSepPos + 33, lk, lBase + 33, move - 1);
                }
                ik[sepPos] = lk[newSepPos];
                iv[sepPos] = lk[newSepPos + 32];
                for (int i = newSepPos; i <= lsBase + lsCount; i++) {
                    lk[i] = Long.MAX_VALUE;
                }
                lk[lsBase] = lsCount - move;
                lk[lBase] = (MIN_KEYS - 1) + move;
                return;
            }
        }

        // Try borrowing (redistributing) from right sibling.
        if (pIdx < pCount) {
            int rightSib = ic[pCBase + pIdx + 1];
            int rsBase = (rightSib << LEAF_SHIFT) + LEAF_OFFSET;
            int rsCount = (int) lk[rsBase];
            if (rsCount > MIN_KEYS) {
                int move = (rsCount - (MIN_KEYS - 1)) >>> 1;
                int lBase = (leaf << LEAF_SHIFT) + LEAF_OFFSET;
                int sepPos = pBase + 1 + pIdx;
                lk[lBase + MIN_KEYS] = ik[sepPos];
                lk[lBase + 32 + MIN_KEYS] = iv[sepPos];
                if (move > 1) {
                    System.arraycopy(lk, rsBase + 1, lk, lBase + MIN_KEYS + 1, move - 1);
                    System.arraycopy(lk, rsBase + 33, lk, lBase + 33 + MIN_KEYS, move - 1);
                }
                ik[sepPos] = lk[rsBase + move];
                iv[sepPos] = lk[rsBase + 32 + move];
                int rem = rsCount - move;
                System.arraycopy(lk, rsBase + 1 + move, lk, rsBase + 1, rem);
                System.arraycopy(lk, rsBase + 33 + move, lk, rsBase + 33, rem);
                for (int i = rsBase + 1 + rem; i <= rsBase + rsCount; i++) {
                    lk[i] = Long.MAX_VALUE;
                }
                lk[lBase] = (MIN_KEYS - 1) + move;
                lk[rsBase] = rem;
                return;
            }
        }

        // Merge with left sibling (if available) or right sibling.
        int leftId;
        int rightId;
        int sepIdx;
        if (pIdx > 0) {
            leftId = ic[pCBase + pIdx - 1];
            rightId = leaf;
            sepIdx = pIdx - 1;
        } else {
            leftId = leaf;
            rightId = ic[pCBase + 1];
            sepIdx = 0;
        }
        int lBase = (leftId << LEAF_SHIFT) + LEAF_OFFSET;
        int rBase = (rightId << LEAF_SHIFT) + LEAF_OFFSET;
        int lCount = (int) lk[lBase];
        int rCount = (int) lk[rBase];
        int sepPos = pBase + 1 + sepIdx;
        lk[lBase + 1 + lCount] = ik[sepPos];
        lk[lBase + 33 + lCount] = iv[sepPos];
        System.arraycopy(lk, rBase + 1, lk, lBase + 2 + lCount, rCount);
        System.arraycopy(lk, rBase + 33, lk, lBase + 34 + lCount, rCount);
        lk[lBase] = lCount + 1 + rCount; // 30
        freeLeaf(rightId);

        // Remove separator and right child from parent.
        int pEnd = pBase + pCount;
        for (int j = sepPos; j < pEnd; j++) {
            ik[j] = ik[j + 1];
            iv[j] = iv[j + 1];
        }
        ik[pEnd] = Long.MAX_VALUE;
        int cEnd = pCBase + pCount;
        for (int j = pCBase + sepIdx + 1; j < cEnd; j++) {
            ic[j] = ic[j + 1];
        }
        int newPCount = pCount - 1;
        ik[pBase] = newPCount;

        if (newPCount >= MIN_KEYS) {
            return;
        }
        if (depth == 0) {
            if (newPCount == 0) {
                this.root = leftId;
                this.height--;
                freeInt(parent);
            }
            return;
        }
        rebalanceInternal(parent, depth);
    }

    private void rebalanceInternal(int node, int depth) {
        final long[] ik = this.intKeys;
        final long[] iv = this.intVals;
        final int[] ic = this.intChildren;
        final int[] pNodeArr = this.pathNode;
        final int[] pIdxArr = this.pathIdx;

        while (true) {
            depth--;
            int parent = pNodeArr[depth];
            int pIdx = pIdxArr[depth];
            int pBase = (parent << INT_SHIFT) + INT_KEY_OFFSET;
            int pCBase = pBase - 2;
            int pCount = (int) ik[pBase];

            // Try borrowing from left sibling.
            if (pIdx > 0) {
                int leftSib = ic[pCBase + pIdx - 1];
                int lsBase = (leftSib << INT_SHIFT) + INT_KEY_OFFSET;
                int lsCount = (int) ik[lsBase];
                if (lsCount > MIN_KEYS) {
                    int move = (lsCount - (MIN_KEYS - 1)) >>> 1;
                    int nBase = (node << INT_SHIFT) + INT_KEY_OFFSET;
                    int nCBase = nBase - 2;
                    int lsCBase = lsBase - 2;

                    System.arraycopy(ik, nBase + 1, ik, nBase + 1 + move, MIN_KEYS - 1);
                    System.arraycopy(iv, nBase + 1, iv, nBase + 1 + move, MIN_KEYS - 1);
                    System.arraycopy(ic, nCBase, ic, nCBase + move, MIN_KEYS);

                    int sepPos = pBase + pIdx;
                    ik[nBase + move] = ik[sepPos];
                    iv[nBase + move] = iv[sepPos];
                    int newSepPos = lsBase + 1 + lsCount - move;
                    if (move > 1) {
                        System.arraycopy(ik, newSepPos + 1, ik, nBase + 1, move - 1);
                        System.arraycopy(iv, newSepPos + 1, iv, nBase + 1, move - 1);
                    }
                    System.arraycopy(ic, lsCBase + lsCount - move + 1, ic, nCBase, move);

                    ik[sepPos] = ik[newSepPos];
                    iv[sepPos] = iv[newSepPos];
                    for (int i = newSepPos; i <= lsBase + lsCount; i++) {
                        ik[i] = Long.MAX_VALUE;
                    }
                    ik[lsBase] = lsCount - move;
                    ik[nBase] = (MIN_KEYS - 1) + move;
                    return;
                }
            }

            // Try borrowing from right sibling.
            if (pIdx < pCount) {
                int rightSib = ic[pCBase + pIdx + 1];
                int rsBase = (rightSib << INT_SHIFT) + INT_KEY_OFFSET;
                int rsCount = (int) ik[rsBase];
                if (rsCount > MIN_KEYS) {
                    int move = (rsCount - (MIN_KEYS - 1)) >>> 1;
                    int nBase = (node << INT_SHIFT) + INT_KEY_OFFSET;
                    int nCBase = nBase - 2;
                    int rsCBase = rsBase - 2;

                    int sepPos = pBase + 1 + pIdx;
                    ik[nBase + MIN_KEYS] = ik[sepPos];
                    iv[nBase + MIN_KEYS] = iv[sepPos];
                    if (move > 1) {
                        System.arraycopy(ik, rsBase + 1, ik, nBase + MIN_KEYS + 1, move - 1);
                        System.arraycopy(iv, rsBase + 1, iv, nBase + MIN_KEYS + 1, move - 1);
                    }
                    System.arraycopy(ic, rsCBase, ic, nCBase + MIN_KEYS, move);

                    ik[sepPos] = ik[rsBase + move];
                    iv[sepPos] = iv[rsBase + move];
                    int rem = rsCount - move;
                    System.arraycopy(ik, rsBase + 1 + move, ik, rsBase + 1, rem);
                    System.arraycopy(iv, rsBase + 1 + move, iv, rsBase + 1, rem);
                    System.arraycopy(ic, rsCBase + move, ic, rsCBase, rem + 1);
                    for (int i = rsBase + 1 + rem; i <= rsBase + rsCount; i++) {
                        ik[i] = Long.MAX_VALUE;
                    }
                    ik[nBase] = (MIN_KEYS - 1) + move;
                    ik[rsBase] = rem;
                    return;
                }
            }

            // Merge internal node with left sibling (if available) or right sibling.
            int leftId;
            int rightId;
            int sepIdx;
            if (pIdx > 0) {
                leftId = ic[pCBase + pIdx - 1];
                rightId = node;
                sepIdx = pIdx - 1;
            } else {
                leftId = node;
                rightId = ic[pCBase + 1];
                sepIdx = 0;
            }
            int lBase = (leftId << INT_SHIFT) + INT_KEY_OFFSET;
            int rBase = (rightId << INT_SHIFT) + INT_KEY_OFFSET;
            int lCount = (int) ik[lBase];
            int rCount = (int) ik[rBase];
            int sepPos = pBase + 1 + sepIdx;

            ik[lBase + 1 + lCount] = ik[sepPos];
            iv[lBase + 1 + lCount] = iv[sepPos];
            System.arraycopy(ik, rBase + 1, ik, lBase + 2 + lCount, rCount);
            System.arraycopy(iv, rBase + 1, iv, lBase + 2 + lCount, rCount);
            System.arraycopy(ic, rBase - 2, ic, lBase - 2 + lCount + 1, rCount + 1);
            ik[lBase] = lCount + 1 + rCount; // 30
            freeInt(rightId);

            // Remove separator and right child from parent.
            int pEnd = pBase + pCount;
            for (int j = sepPos; j < pEnd; j++) {
                ik[j] = ik[j + 1];
                iv[j] = iv[j + 1];
            }
            ik[pEnd] = Long.MAX_VALUE;
            int cEnd = pCBase + pCount;
            for (int j = pCBase + sepIdx + 1; j < cEnd; j++) {
                ic[j] = ic[j + 1];
            }
            int newPCount = pCount - 1;
            ik[pBase] = newPCount;

            if (newPCount >= MIN_KEYS) {
                return;
            }
            if (depth == 0) {
                if (newPCount == 0) {
                    this.root = leftId;
                    this.height--;
                    freeInt(parent);
                }
                return;
            }
            node = parent;
        }
    }

    // ------------------------------------------------------------------ range

    public int range(long lo, long hi, long[] outKeys, long[] outVals, int limit) {
        if (limit <= 0 || lo > hi || this.height == 0) {
            return 0;
        }
        int h = this.height;
        int node = this.root;
        final long[] lk = this.leafKV;

        if (h == 1) {
            int lBase = (node << LEAF_SHIFT) + LEAF_OFFSET;
            int lEnd = lBase + (int) lk[lBase];
            int pos = lBase + 1;
            if (lk[pos + 15] < lo) pos += 16;
            if (lk[pos + 7] < lo) pos += 8;
            if (lk[pos + 3] < lo) pos += 4;
            if (lk[pos + 1] < lo) pos += 2;
            if (lk[pos] < lo) pos += 1;
            if (pos > lEnd) {
                return 0;
            }
            int rem = lEnd - pos + 1;
            if (lk[lEnd] <= hi && rem <= limit) {
                System.arraycopy(lk, pos, outKeys, 0, rem);
                System.arraycopy(lk, pos + 32, outVals, 0, rem);
                return rem;
            }
            int written = 0;
            for (int p = pos; p <= lEnd && written < limit; p++) {
                long k = lk[p];
                if (k > hi) {
                    return written;
                }
                outKeys[written] = k;
                outVals[written] = lk[p + 32];
                written++;
            }
            return written;
        }

        final long[] ik = this.intKeys;
        final long[] iv = this.intVals;
        final int[] ic = this.intChildren;
        final int[] sNode = this.pathNode;
        final int[] sIdx = this.pathIdx;
        int sp = 0;

        // Seek down to the starting position for `lo`.
        boolean exactInternal = false;
        while (h > 1) {
            int base = (node << INT_SHIFT) + INT_KEY_OFFSET;
            int end = base + (int) ik[base];
            int pos = base + 1;
            if (ik[pos + 15] < lo) pos += 16;
            if (ik[pos + 7] < lo) pos += 8;
            if (ik[pos + 3] < lo) pos += 4;
            if (ik[pos + 1] < lo) pos += 2;
            if (ik[pos] < lo) pos += 1;
            sNode[sp] = node;
            sIdx[sp] = pos - base - 1;
            sp++;
            if (pos <= end && ik[pos] == lo) {
                exactInternal = true;
                break;
            }
            node = ic[pos - 3];
            h--;
        }

        int written = 0;
        if (!exactInternal) {
            int lBase = (node << LEAF_SHIFT) + LEAF_OFFSET;
            int lEnd = lBase + (int) lk[lBase];
            int pos = lBase + 1;
            if (lk[pos + 15] < lo) pos += 16;
            if (lk[pos + 7] < lo) pos += 8;
            if (lk[pos + 3] < lo) pos += 4;
            if (lk[pos + 1] < lo) pos += 2;
            if (lk[pos] < lo) pos += 1;
            if (pos <= lEnd) {
                int rem = lEnd - pos + 1;
                if (lk[lEnd] <= hi && rem <= limit) {
                    System.arraycopy(lk, pos, outKeys, 0, rem);
                    System.arraycopy(lk, pos + 32, outVals, 0, rem);
                    written = rem;
                    if (written == limit) {
                        return written;
                    }
                } else {
                    for (int p = pos; p <= lEnd && written < limit; p++) {
                        long k = lk[p];
                        if (k > hi) {
                            return written;
                        }
                        outKeys[written] = k;
                        outVals[written] = lk[p + 32];
                        written++;
                    }
                    return written;
                }
            }
        }

        final int leafParentSp = this.height - 1;
        while (sp > 0) {
            if (sp == leafParentSp) {
                // Fast inner loop over a level-2 internal node and its leaf children.
                int pNode = sNode[sp - 1];
                int pIdx = sIdx[sp - 1];
                int pBase = (pNode << INT_SHIFT) + INT_KEY_OFFSET;
                int pCBase = pBase - 2;
                int pCount = (int) ik[pBase];
                while (pIdx < pCount) {
                    int keyPos = pBase + 1 + pIdx;
                    long k = ik[keyPos];
                    if (k > hi) {
                        return written;
                    }
                    outKeys[written] = k;
                    outVals[written] = iv[keyPos];
                    written++;
                    if (written == limit) {
                        return written;
                    }
                    pIdx++;
                    int leaf = ic[pCBase + pIdx];
                    int lBase = (leaf << LEAF_SHIFT) + LEAF_OFFSET;
                    int lCount = (int) lk[lBase];
                    int lEnd = lBase + lCount;
                    if (lk[lEnd] <= hi && lCount <= limit - written) {
                        System.arraycopy(lk, lBase + 1, outKeys, written, lCount);
                        System.arraycopy(lk, lBase + 33, outVals, written, lCount);
                        written += lCount;
                        if (written == limit) {
                            return written;
                        }
                    } else {
                        for (int p = lBase + 1; p <= lEnd && written < limit; p++) {
                            long lkVal = lk[p];
                            if (lkVal > hi) {
                                return written;
                            }
                            outKeys[written] = lkVal;
                            outVals[written] = lk[p + 32];
                            written++;
                        }
                        return written;
                    }
                }
                sp--;
            } else {
                // Internal node at level >= 3.
                int pNode = sNode[sp - 1];
                int pIdx = sIdx[sp - 1];
                int pBase = (pNode << INT_SHIFT) + INT_KEY_OFFSET;
                int pCount = (int) ik[pBase];
                if (pIdx >= pCount) {
                    sp--;
                    continue;
                }
                int keyPos = pBase + 1 + pIdx;
                long k = ik[keyPos];
                if (k > hi) {
                    return written;
                }
                outKeys[written] = k;
                outVals[written] = iv[keyPos];
                written++;
                if (written == limit) {
                    return written;
                }
                sIdx[sp - 1] = pIdx + 1;

                // Descend leftmost from right child down to a leaf.
                int cur = ic[pBase - 2 + pIdx + 1];
                while (sp < leafParentSp) {
                    sNode[sp] = cur;
                    sIdx[sp] = 0;
                    sp++;
                    cur = ic[(cur << INT_SHIFT) + INT_CHILD_OFFSET];
                }

                int lBase = (cur << LEAF_SHIFT) + LEAF_OFFSET;
                int lCount = (int) lk[lBase];
                int lEnd = lBase + lCount;
                if (lk[lEnd] <= hi && lCount <= limit - written) {
                    System.arraycopy(lk, lBase + 1, outKeys, written, lCount);
                    System.arraycopy(lk, lBase + 33, outVals, written, lCount);
                    written += lCount;
                    if (written == limit) {
                        return written;
                    }
                } else {
                    for (int p = lBase + 1; p <= lEnd && written < limit; p++) {
                        long lkVal = lk[p];
                        if (lkVal > hi) {
                            return written;
                        }
                        outKeys[written] = lkVal;
                        outVals[written] = lk[p + 32];
                        written++;
                    }
                    return written;
                }
            }
        }
        return written;
    }
}
