-
Notifications
You must be signed in to change notification settings - Fork 3
/
2276.cpp
63 lines (56 loc) · 1.82 KB
/
2276.cpp
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
class CountIntervals {
public:
// use map to store interval [start, end)
map<int, int> mp;
int length;
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) {
// update the total covered length
length += (right - left);
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);
// update the total covered length by deleting overlapped ones
// r++: [l, r] -> [l, r) to half-open interval
auto it = l;
r++;
while (it != r) {
length -= (it->second - it->first);
it++;
}
// update the total covered length by the new interval
length += (maxVal - minVal);
// remove overlap segment
mp.erase(l, r);
// return new interval
return {minVal, maxVal};
}
CountIntervals() {
length = 0;
}
void add(int left, int right) {
// use half-open interval to simply the implementation
right++;
auto x = find(left, right);
mp[x.first] = x.second;
}
int count() {
return length;
}
};
/**
* Your CountIntervals object will be instantiated and called as such:
* CountIntervals* obj = new CountIntervals();
* obj->add(left,right);
* int param_2 = obj->count();
*/