-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCalendar_III.cpp
More file actions
74 lines (69 loc) · 1.64 KB
/
Copy pathMyCalendar_III.cpp
File metadata and controls
74 lines (69 loc) · 1.64 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
63
64
65
66
67
68
69
70
71
72
73
74
class MyCalendarThree {
public:
map<int,int> check;
MyCalendarThree() {
check.clear();
}
int book(int start, int end) {
check[start]++;
check[end]--;
int active = 0, res = 0;
for(auto& [key,values]: check){
active += values;
if(active>res)
res = active;
}
return res;
}
};
/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree* obj = new MyCalendarThree();
* int param_1 = obj->book(start,end);
*/
struct Tree{
int start,end,booked,res;
Tree* left;
Tree* right;
Tree(int s, int e){
start = s;
end = e;
booked = res = 0;
left = NULL;
right = NULL;
}
void add(int s, int e, int val){
if(e<start || s>end)
return;
if(s<=start && e>=end){
booked += val;
res += val;
}
else{
int mid = start + (end-start)/2;
if(!left)
left = new Tree(start,mid);
if(!right)
right = new Tree(mid+1,end);
left->add(s,e,val);
right->add(s,e,val);
res = max(left->res,right->res) + booked;
}
}
};
class MyCalendarThree {
public:
Tree* root;
MyCalendarThree() {
root = new Tree(0,1000000000);
}
int book(int start, int end) {
root->add(start,end-1,1);
return root->res;
}
};
/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree* obj = new MyCalendarThree();
* int param_1 = obj->book(start,end);
*/