javascript treemap 1650ms
\n///////////////////////////////////////////////////// Template /////////////////////////////////////////////////////////////////\nfunction Bisect() {\n    ret
```\n///////////////////////////////////////////////////// Template /////////////////////////////////////////////////////////////////\nfunction Bisect() {\n    return { insort_right, insort_left, bisect_left, bisect_right }\n    function insort_right(a, x, lo = 0, hi = null) {\n        lo = bisect_right(a, x, lo, hi);\n        a.splice(lo, 0, x);\n    }\n    function bisect_right(a, x, lo = 0, hi = null) { // > upper_bound\n        if (lo < 0) throw new Error(\'lo must be non-negative\');\n        if (hi == null) hi = a.length;\n        while (lo < hi) {\n            let mid = parseInt((lo + hi) / 2);\n            a[mid] > x ? hi = mid : lo = mid + 1;\n        }\n        return lo;\n    }\n    function insort_left(a, x, lo = 0, hi = null) {\n        lo = bisect_left(a, x, lo, hi);\n        a.splice(lo, 0, x);\n    }\n    function bisect_left(a, x, lo = 0, hi = null) { // >= lower_bound\n        if (lo < 0) throw new Error(\'lo must be non-negative\');\n        if (hi == null) hi = a.length;\n        while (lo < hi) {\n            let mid = parseInt((lo + hi) / 2);\n            a[mid] < x ? lo = mid + 1 : hi = mid;\n        }\n        return lo;\n    }\n}\n\n/*\nusage:\nlet ts = new TreeMap();\nlet ts = new TreeMap([[3, 1], [7, 1], [7, 2], [1, 1], [3, 2]]); // Map { 1 => 1, 3 => 2, 7 => 2 }  (console.log(ts.show()))\n*/\nfunction TreeMap(g) {\n    let ts = [], m = new Map(), bisect = new Bisect();\n    initialize();\n    return { put, ceilingKey, higherKey, lowerKey, floorKey, ceilingEntry, higherEntry, lowerEntry, floorEntry, remove, contains, size, clear, show };\n    function initialize() {\n        if (g) {\n            for (const [k, v] of g) {\n                if (!m.has(k)) bisect.insort_right(ts, k);\n                m.set(k, v);\n            }\n        }\n    }\n    function put(k, v) {\n        if (!m.has(k)) bisect.insort_right(ts, k); // ts has no duplicates/unique key\n        m.set(k, v); // update key with most recent value\n    }\n    function ceilingKey(e) { // >= lower_bound\n        let idx = bisect.bisect_right(ts, e);\n        let res = ts[idx - 1] == e ? e : ts[bisect.bisect_right(ts, e)];\n        return res == undefined ? null : res;\n    }\n    function higherKey(e) { // > upper_bound\n        let idx = bisect.bisect_right(ts, e);\n        let res = ts[idx] > e ? ts[idx] : ts[bisect.bisect_right(ts, e) + 1];\n        return res == undefined ? null : res;\n    }\n    function floorKey(e) { // <= \n        let idx = bisect.bisect_left(ts, e);\n        let res = ts[idx] == e ? e : ts[bisect.bisect_left(ts, e) - 1];\n        return res == undefined ? null : res;\n    }\n    function lowerKey(e) { // <\n        let idx = bisect.bisect_left(ts, e);\n        let res = ts[idx] < e ? ts[idx] : ts[bisect.bisect_left(ts, e) - 1];\n        return res == undefined ? null : res;\n    }\n    function data(k) {\n        return k == null ? null : { key: k, value: m.get(k) }\n    }\n    function ceilingEntry(k) {\n        return data(ceilingKey(k));\n    }\n    function higherEntry(k) {\n        return data(higherKey(k));\n    }\n    function floorEntry(k) {\n        return data(floorKey(k));\n    }\n    function lowerEntry(k) {\n        return data(lowerKey(k));\n    }\n    function remove(e) {\n        let idx = bisect.bisect_left(ts, e);\n        if (ts[idx] == e) ts.splice(idx, 1);\n        m.delete(e);\n    }\n    function contains(e) {\n        return m.has(e);\n    }\n    function size() {\n        return ts.length;\n    }\n    function clear() {\n        ts = [];\n        m.clear();\n    }\n    function show() {\n        let res = new Map();\n        for (const x of ts) res.set(x, m.get(x));\n        return res;\n    }\n}\n//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction CountIntervals() {\n    let tm = new TreeMap(), cnt = 0;\n    return { add, count }\n    function add(left, right) {\n        let lower = tm.floorEntry(left);\n        if (lower != null && lower.value >= left) {\n            let k = lower.key, v = lower.value;\n            cnt -= v - k + 1;\n            left = Math.min(left, k);\n            right = Math.max(right, v);\n            tm.remove(k);\n        }\n        while (1) {\n           let higher = tm.ceilingEntry(left);\n           if (higher == null || higher.key > right) break;\n           let k = higher.key, v = higher.value;\n           tm.remove(k);\n           cnt -= v - k + 1;\n           right = Math.max(right, v);\n        }\n        cnt += right - left + 1;\n        tm.put(left, right);\n    }\n    function count() {\n        return cnt;\n    }\n}\n```
['Tree', 'Ordered Set', 'JavaScript']