Golang Treemap solution
Intuition\nMerge ranges on Add operation to avoid counting same range elements multiple times\n\n# Approach\nUse a treemap data structure to store the ranges in
# Intuition\nMerge ranges on `Add` operation to avoid counting same range elements multiple times\n\n# Approach\nUse a treemap data structure to store the ranges in increasing order\nOn `Add` operation, find the previous range and next range\n\nMerge the ranges if there is an overlap and update the total count\n\n# Complexity\n- Time complexity:\nO(nlogn) as finding floor and ceiling along with put operation takes log(n) time. And there are n operations\n\n- Space complexity:\nO(n) to store the ranges\n\n# Code\n```\ntype CountIntervals struct {\n    ranges *treemap.Map\n    count int\n}\n\nfunc comparator(a, b interface{}) int {\n    aInt := a.(int)\n    bInt := b.(int)\n    if aInt > bInt { return 1 }\n    if aInt < bInt { return -1 }\n    return 0\n}\n\nfunc Constructor() CountIntervals {\n    return CountIntervals{ranges: treemap.NewWith(comparator)}\n}\n\n\nfunc (this *CountIntervals) Add(left int, right int)  {\n    prevStart, prevEnd := this.ranges.Floor(left)\n    nextStart, nextEnd := this.ranges.Ceiling(left)\n    if prevStart == nil && nextStart == nil {\n        this.ranges.Put(left, right)\n        this.count = right - left + 1\n        return\n    }\n    start, end := left, right\n    if prevStart != nil {\n        // overlap and merge\n        if prevEnd.(int) >= left {\n            // Remove the previous range as merged one will be added\n            this.ranges.Remove(prevStart)\n            start = prevStart.(int)\n            end = max(right, prevEnd.(int))\n            // Remove the previous range count from total\n            this.count -= (prevEnd.(int) - prevStart.(int)) + 1\n        }\n    }\n    // Re-fetch next to avoid scenario where next is same as prev\n    nextStart, nextEnd = this.ranges.Ceiling(left)\n    // Keep finding next range until there is an overlap\n    for nextStart != nil && end >= nextStart.(int) {\n        // Remove the next range as merged one will be added\n        this.ranges.Remove(nextStart)\n        end = max(end, nextEnd.(int))\n        // Remove the next range count from total\n        this.count -= (nextEnd.(int) - nextStart.(int)) + 1\n        nextStart, nextEnd = this.ranges.Ceiling(left)\n    }\n    \n    // Add the count of merged range\n    this.count += end - start + 1\n    // Add the merged range to list of ranges\n    this.ranges.Put(start, end)\n}\n\n\nfunc (this *CountIntervals) Count() int {\n    return this.count\n}\n\nfunc max(a, b int) int {\n    if a > b {\n        return a\n    }\n    return b\n}\n\n/**\n * Your CountIntervals object will be instantiated and called as such:\n * obj := Constructor();\n * obj.Add(left,right);\n * param_2 := obj.Count();\n */\n```
['Ordered Map', 'Go']