-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path715.cpp
More file actions
52 lines (46 loc) · 1.56 KB
/
715.cpp
File metadata and controls
52 lines (46 loc) · 1.56 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
class RangeModule {
private:
// mp store <begin, end> pair
map<int, int> mp;
pair<int, int> find(int left, int right) {
// find segment.first > left and segment.first > right
auto l = mp.upper_bound(left);
auto r = mp.upper_bound(right);
// move back first segment and check if its end < left
// if yes, move forward first segment
if (l != mp.begin() && (--l)->second < left) ++l;
// if now l == r, it does not overlap with others
if (l == r) return {left, right};
// get the min val
int minVal = min(l->first, left);
// get the max val by moving back the second segment
int maxVal = max(right, (--r)->second);
// remove overlap segment
mp.erase(l, ++r);
// return new interval
return {minVal, maxVal};
}
public:
RangeModule() {
}
void addRange(int left, int right) {
auto x = find(left, right);
mp[x.first] = x.second;
}
bool queryRange(int left, int right) {
auto it = mp.upper_bound(left);
return (it != mp.begin()) && ((--it)->second >= right);
}
void removeRange(int left, int right) {
auto x = find(left, right);
if (left > x.first) mp[x.first] = left;
if (x.second > right) mp[right] = x.second;
}
};
/**
* 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);
*/