-
Notifications
You must be signed in to change notification settings - Fork 0
/
715.hpp
65 lines (54 loc) · 1.46 KB
/
715.hpp
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
#ifndef LEETCODE_715_HPP
#define LEETCODE_715_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
#include <map>
using namespace std;
class RangeModule {
public:
void addRange(int left, int right) {
auto l = intervals.upper_bound(left), r = intervals.upper_bound(right);
if (l != intervals.begin()) {
l--;
if (l->second < left)
l++;
}
if (l != r) {
left = min(left, l->first);
right = max(right, (--r)->second);
intervals.erase(l, ++r);
}
intervals[left] = right;
}
bool queryRange(int left, int right) {
auto ite = intervals.upper_bound(left);
return (ite != intervals.begin() && (--ite)->second >= right);
}
void removeRange(int left, int right) {
auto l = intervals.upper_bound(left), r = intervals.upper_bound(right);
if (l != intervals.begin()) {
l--;
if (l->second < left)
l++;
}
if (l == r)
return;
int l1 = min(left, l->first), r1 = max(right, (--r)->second);
intervals.erase(l, ++r);
if (l1 < left)
intervals[l1] = left;
if (r1 > right)
intervals[right] = r1;
}
private:
map<int, int> intervals;
};
#endif //LEETCODE_715_HPP