LeetCode 2276. Count Integers in Intervals

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
class CountIntervals {
map<int, int> m;
int cnt = 0;
public:
CountIntervals() {}

void add(int left, int right) {
for (auto it = m.lower_bound(left); it != m.end() && it->second <= right; m.erase(it++)) {
int l = it->second, r = it->first;
left = min(left, l), right = max(right, r);
cnt -= r - l + 1;
}
cnt += right - left + 1;
m[right] = left;
}

int count() {
return cnt;
}
};

/**
* Your CountIntervals object will be instantiated and called as such:
* CountIntervals* obj = new CountIntervals();
* obj->add(left,right);
* int param_2 = obj->count();
*/