forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
182 lines (139 loc) · 5.46 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// Source : https://leetcode.com/problems/my-calendar-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-27
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Dynamic Segment Tree
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class MyCalendarThree {
private:
struct Node{
int left, right;
int event = 0;
int leftNodeIndex = -1, rightNodeIndex = -1;
Node(int left, int right, int event){
this->left = left;
this->right = right;
this->event = event;
}
};
vector<Node> tree;
public:
MyCalendarThree() {
Node root(0, 1000000000, 0);
tree.push_back(root);
}
int book(int start, int end) {
add(0, start, end);
return tree[0].event;
}
private:
int query(int treeID, int l, int r){
if(l == tree[treeID].left && r == tree[treeID].right)
return tree[treeID].event;
if(tree[treeID].leftNodeIndex == -1)
return tree[treeID].event;
int leftNodeIndex = tree[treeID].leftNodeIndex;
int rightNodeIndex = tree[treeID].rightNodeIndex;
if(r <= tree[leftNodeIndex].right)
return query(leftNodeIndex, l, r);
else if(l >= tree[rightNodeIndex].left){
return query(rightNodeIndex, l, r);
}
int leftEvent = query(leftNodeIndex, l, tree[leftNodeIndex].right);
int rightEvent = query(rightNodeIndex, tree[rightNodeIndex].left, r);
return max(leftEvent, rightEvent);
}
void add(int treeID, int l, int r){
//assert(tree[treeID].left <= l && l < r && r <= tree[treeID].right);
if(l == tree[treeID].left && r == tree[treeID].right){
int leftNodeIndex, rightNodeIndex;
if(tree[treeID].leftNodeIndex != -1){
//assert(tree[treeID].rightNodeIndex != -1);
leftNodeIndex = tree[treeID].leftNodeIndex;
add(leftNodeIndex, tree[leftNodeIndex].left, tree[leftNodeIndex].right);
rightNodeIndex = tree[treeID].rightNodeIndex;
add(rightNodeIndex, tree[rightNodeIndex].left, tree[rightNodeIndex].right);
}
tree[treeID].event += 1;
return;
}
if(tree[treeID].leftNodeIndex == -1){
//assert(tree[treeID].rightNodeIndex == -1);
int leftNodeIndex, rightNodeIndex;
if(l == tree[treeID].left){
Node leftNode(l, r, tree[treeID].event);
leftNodeIndex = tree.size();
tree.push_back(leftNode);
tree[treeID].leftNodeIndex = leftNodeIndex;
//assert(r < tree[treeID].right);
Node rightNode(r, tree[treeID].right, tree[treeID].event);
rightNodeIndex = tree.size();
tree.push_back(rightNode);
tree[treeID].rightNodeIndex = rightNodeIndex;
add(leftNodeIndex, l, r);
}
else if(r == tree[treeID].right){
//assert(tree[treeID].left < l);
Node leftNode(tree[treeID].left, l, tree[treeID].event);
leftNodeIndex = tree.size();
tree.push_back(leftNode);
tree[treeID].leftNodeIndex = leftNodeIndex;
Node rightNode(l, r, tree[treeID].event);
rightNodeIndex = tree.size();
tree.push_back(rightNode);
tree[treeID].rightNodeIndex = rightNodeIndex;
add(rightNodeIndex, l, r);
}
else{
//assert(tree[treeID].left < l);
Node leftNode(tree[treeID].left, l, tree[treeID].event);
leftNodeIndex = tree.size();
tree.push_back(leftNode);
tree[treeID].leftNodeIndex = leftNodeIndex;
//assert(l < tree[treeID].right);
Node rightNode(l, tree[treeID].right, tree[treeID].event);
rightNodeIndex = tree.size();
tree.push_back(rightNode);
tree[treeID].rightNodeIndex = rightNodeIndex;
add(rightNodeIndex, l, r);
}
int leftEvent = tree[leftNodeIndex].event;
int rightEvent = tree[rightNodeIndex].event;
tree[treeID].event = max(leftEvent, rightEvent);
return;
}
int leftNodeIndex = tree[treeID].leftNodeIndex;
int rightNodeIndex = tree[treeID].rightNodeIndex;
//assert(leftNodeIndex + 1 == rightNodeIndex);
//assert(tree[leftNodeIndex].right == tree[rightNodeIndex].left);
if(r <= tree[leftNodeIndex].right){
add(leftNodeIndex, l, r);
}
else if(l >= tree[rightNodeIndex].left){
add(rightNodeIndex, l, r);
}
else{
add(leftNodeIndex, l, tree[leftNodeIndex].right);
add(rightNodeIndex, tree[rightNodeIndex].left, r);
}
int leftEvent = tree[leftNodeIndex].event;
int rightEvent = tree[rightNodeIndex].event;
tree[treeID].event = max(leftEvent, rightEvent);
return;
}
};
int main() {
MyCalendarThree calendar;
cout << calendar.book(10, 20) << endl; // 1
cout << calendar.book(50, 60) << endl; // 1
cout << calendar.book(10, 40) << endl; // 2
cout << calendar.book(5, 15) << endl; // 3
cout << calendar.book(5, 10) << endl; // 3
cout << calendar.book(25, 55) << endl; // 3
cout << endl;
return 0;
}