-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangeModule.cpp
More file actions
62 lines (54 loc) · 1.58 KB
/
rangeModule.cpp
File metadata and controls
62 lines (54 loc) · 1.58 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
// Source: https://leetcode.com/problems/range-module/
// Author: Miao Zhang
// Date: 2021-03-03
class RangeModule {
public:
RangeModule() {
}
void addRange(int left, int right) {
it l, r;
getOverlapRanges(left, right, l, r);
if (l != r) {
auto last = r;
--last;
left = min(left, l->first);
right = max(right, last->second);
ranges_.erase(l, r);
}
ranges_[left] = right;
}
bool queryRange(int left, int right) {
it l, r;
getOverlapRanges(left, right, l, r);
if (l == r) return false;
return l->first <= left && l->second >= right;
}
void removeRange(int left, int right) {
it l, r;
getOverlapRanges(left, right, l, r);
if (l == r) return;
auto last = r; --last;
int start = min(left, l->first);
int end = max(right, last->second);
ranges_.erase(l, r);
if (start < left) ranges_[start] = left;
if (end > right) ranges_[right] = end;
}
private:
typedef map<int, int>::iterator it;
map<int, int> ranges_;
void getOverlapRanges(int left, int right, it& l, it& r) {
l = ranges_.upper_bound(left);
r = ranges_.upper_bound(right);
if (l != ranges_.begin()) {
if ((--l)->second < left) l++;
}
}
};
/**
* 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);
*/