|
| 1 | +const RangeModule = function() { |
| 2 | + this.range = [] |
| 3 | +} |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {number} left |
| 7 | + * @param {number} right |
| 8 | + * @return {void} |
| 9 | + */ |
| 10 | +RangeModule.prototype.addRange = function(left, right) { |
| 11 | + let index1 = this.range.length |
| 12 | + let low = 0 |
| 13 | + let high = this.range.length - 1 |
| 14 | + while (low <= high) { |
| 15 | + const mid = (low + high) >> 1 |
| 16 | + if (this.range[mid][1] >= left) { |
| 17 | + index1 = Math.min(index1, mid) |
| 18 | + high = mid - 1 |
| 19 | + } else { |
| 20 | + low = mid + 1 |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + let index2 = -1 |
| 25 | + low = 0 |
| 26 | + high = this.range.length - 1 |
| 27 | + while (low <= high) { |
| 28 | + const mid = (low + high) >> 1 |
| 29 | + if (this.range[mid][0] <= right) { |
| 30 | + index2 = Math.max(index2, mid) |
| 31 | + low = mid + 1 |
| 32 | + } else { |
| 33 | + high = mid - 1 |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (index1 === this.range.length) { |
| 38 | + this.range.push([left, right]) |
| 39 | + return |
| 40 | + } else if (index2 === -1) { |
| 41 | + this.range.unshift([left, right]) |
| 42 | + return |
| 43 | + } |
| 44 | + left = Math.min(left, this.range[index1][0]) |
| 45 | + right = Math.max(right, this.range[index2][1]) |
| 46 | + this.range.splice(index1, index2 - index1 + 1, [left, right]) |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {number} left |
| 51 | + * @param {number} right |
| 52 | + * @return {boolean} |
| 53 | + */ |
| 54 | +RangeModule.prototype.queryRange = function(left, right) { |
| 55 | + let index = -1 |
| 56 | + let low = 0 |
| 57 | + let high = this.range.length - 1 |
| 58 | + while (low <= high) { |
| 59 | + const mid = (low + high) >> 1 |
| 60 | + if (this.range[mid][0] <= left) { |
| 61 | + index = Math.max(index, mid) |
| 62 | + low = mid + 1 |
| 63 | + } else { |
| 64 | + high = mid - 1 |
| 65 | + } |
| 66 | + } |
| 67 | + if (index === -1 || this.range[index][1] < right) { |
| 68 | + return false |
| 69 | + } |
| 70 | + return true |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @param {number} left |
| 75 | + * @param {number} right |
| 76 | + * @return {void} |
| 77 | + */ |
| 78 | +RangeModule.prototype.removeRange = function(left, right) { |
| 79 | + let index1 = this.range.length |
| 80 | + let low = 0 |
| 81 | + let high = this.range.length - 1 |
| 82 | + while (low <= high) { |
| 83 | + const mid = (low + high) >> 1 |
| 84 | + if (this.range[mid][1] >= left) { |
| 85 | + index1 = Math.min(index1, mid) |
| 86 | + high = mid - 1 |
| 87 | + } else { |
| 88 | + low = mid + 1 |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + let index2 = -1 |
| 93 | + low = 0 |
| 94 | + high = this.range.length - 1 |
| 95 | + while (low <= high) { |
| 96 | + const mid = (low + high) >> 1 |
| 97 | + if (this.range[mid][0] <= right) { |
| 98 | + index2 = Math.max(index2, mid) |
| 99 | + low = mid + 1 |
| 100 | + } else { |
| 101 | + high = mid - 1 |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (index1 === this.range.length || index2 === -1) { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + const newRange = [] |
| 110 | + if (left > this.range[index1][0]) { |
| 111 | + newRange.push([this.range[index1][0], left]) |
| 112 | + } |
| 113 | + if (right < this.range[index2][1]) { |
| 114 | + newRange.push([right, this.range[index2][1]]) |
| 115 | + } |
| 116 | + this.range.splice(index1, index2 - index1 + 1, ...newRange) |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Your RangeModule object will be instantiated and called as such: |
| 121 | + * var obj = new RangeModule() |
| 122 | + * obj.addRange(left,right) |
| 123 | + * var param_2 = obj.queryRange(left,right) |
| 124 | + * obj.removeRange(left,right) |
| 125 | + */ |
0 commit comments