-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path715. Range Module.cpp
More file actions
83 lines (67 loc) · 2.52 KB
/
715. Range Module.cpp
File metadata and controls
83 lines (67 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <algorithm>
#include <map>
using namespace std;
// uses a map(AVL/BST) to store the ranges with left range as the key
// add/remove needs to merge/delete all the overlapped ranges(number m).
// add/remove T: O(mlogn)
// Query T: O(logn)
class RangeModule {
public:
RangeModule():ranges_() {}
void addRange(int left, int right) {
map<int, int>::iterator first = ranges_.end();
map<int, int>::iterator last = ranges_.end();
// T: O(logn)
getOverlappedRanges(left, right, first, last);
// exists overlapped ranges
if (first != last) {
left = min(left, first->first);
right = max(right, (--last)->second);
// erases overlapped ranges. T: O(mlogn) because each deletion(/insertion/search) in AVL cost O(logn)
ranges_.erase(first, ++last);
}
ranges_[left] = right;
}
bool queryRange(int left, int right) {
map<int, int>::iterator first = ranges_.end();
map<int, int>::iterator last = ranges_.end();
// T: O(logn)
getOverlappedRanges(left, right, first, last);
if (first == last) return false;
// only true when all covered in the first range
return left >= first->first && right <= first->second;
}
void removeRange(int left, int right) {
map<int, int>::iterator first = ranges_.end();
map<int, int>::iterator last = ranges_.end();
// T: O(logn)
getOverlappedRanges(left, right, first, last);
if (first == last) return;
auto l_it = last;
int f = first->first;
int l = (--last)->second;
// T: O(mlogn)
ranges_.erase(first, l_it);
if (left > f) ranges_[f] = left;
if (right < l) ranges_[right] = l;
return;
}
private:
map<int, int> ranges_;
// [first, last) is half-open
void getOverlappedRanges(int left, int right, map<int, int>::iterator& first, map<int, int>::iterator& last) {
// upper_bound > right, lower_bound >= right
// T: O(logn)
last = ranges_.upper_bound(right);
first = ranges_.upper_bound(left);
if (first != ranges_.begin() && (--first)->second < left) ++first;
return;
}
};
/**
* Your RangeModule object will be instantiated and called as such:
* RangeModule* obj = new RangeModule();
* obj->addRange(left,right);
* bool param_2 = obj->queryRange(left,right);
* obj->removeRange(left,right);
*/