-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path715. Range Module.cpp
370 lines (322 loc) · 9.11 KB
/
715. Range Module.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#if 0
// segment-tree, 54 / 54 Test case passed but TLE
/// Need to optimize as other segment tree solutions are acceptable
// likely like lazy updates.
struct stree{
int low,high;
stree *left,*right;
bool tracked;
stree(int rlow,int rhigh){
low = rlow;
high = rhigh;
left = right = nullptr;
tracked = false;
}
void add(int rlow,int rhigh) {
if(rlow> high || rhigh < low)
return;
if(rlow<=low && rhigh>=high){
tracked = true;
if(left){
left->add(rlow,rhigh);
right->add(rlow,rhigh);
}
return;
}
if(tracked)
return;
int mid = low + (high-low)/2;
if(!left) {
left = new stree(low,mid);
right = new stree(mid+1,high);
}
left->add(rlow,rhigh);
right->add(rlow,rhigh);
tracked = right->tracked && left->tracked;
return;
}
bool query(int rlow, int rhigh){
if(rlow> high || rhigh < low)
return false;
if((rlow<=low && rhigh>=high) || tracked || !left){
return tracked;
}
int mid = low + (high-low)/2;
if(mid >= rhigh)
return left->query(rlow,rhigh);
else if(mid<rlow)
return right->query(rlow,rhigh);
else
return left->query(rlow,rhigh) && right->query(rlow,rhigh);
return false;
}
void remove(int rlow,int rhigh){
if(rlow> high || rhigh < low)
return;
int mid = low + (high-low)/2;
if((rlow<=low && rhigh>=high)){
tracked = false;
if(left) {
left->remove(rlow,rhigh);
right->remove(rlow,rhigh);
}
return;
}
if(!tracked && !left)
return;
if(!left) {
left = new stree(low,mid);
right = new stree(mid+1,high);
left->tracked = tracked;
right->tracked = tracked;
}
if(mid >= rhigh)
left->remove(rlow,rhigh);
else if(mid<rlow)
right->remove(rlow,rhigh);
else {
left->remove(rlow,rhigh);
right->remove(rlow,rhigh);
}
tracked = false;
}
};
class RangeModule {
stree tree;
public:
RangeModule():tree(1,1e9) {
}
void addRange(int left, int right) {
tree.add(left,right-1);
}
bool queryRange(int left, int right) {
return tree.query(left,right-1);
}
void removeRange(int left, int right) {
tree.remove(left,right-1);
}
};
// https://leetcode.com/problems/range-module/discuss/108912/C%2B%2B-vector-O(n)-and-map-O(logn)-compare-two-solutions
// 446 ms
class RangeModule {
public:
void addRange(int left, int right) {
auto l = invals.upper_bound(left), r = invals.upper_bound(right);
if (l != invals.begin()) {
l--;
if (l->second < left) l++;
}
if (l != r) {
left = min(left, l->first);
right = max(right, (--r)->second);
invals.erase(l,++r);
}
invals[left] = right;
}
bool queryRange(int left, int right) {
auto it = invals.upper_bound(left);
if (it == invals.begin() || (--it)->second < right) return false;
return true;
}
void removeRange(int left, int right) {
auto l = invals.upper_bound(left), r = invals.upper_bound(right);
if (l != invals.begin()) {
l--;
if (l->second < left) l++;
}
if (l == r) return;
int l1 = min(left, l->first), r1 = max(right, (--r)->second);
invals.erase(l, ++r);
if (l1 < left) invals[l1] = left;
if (r1 > right) invals[right] = r1;
}
private:
map<int, int> invals;
};
// another one based on map
//https://leetcode.com/problems/range-module/discuss/475266/C%2B%2B-amortized-O(log(n)
// 385 ms
class RangeModule {
map<int, int, greater<int>> tree; // start -> end
public:
void addRange(int left, int right) {
for (auto it = tree.lower_bound(right); it != tree.end() && it->second >= left;) {
left = min(left, it->first);
right = max(right, it->second);
it++;
tree.erase(prev(it));
}
tree[left] = right;
}
bool queryRange(int left, int right) {
auto it = tree.lower_bound(left);
if (it == tree.end()) {
return false;
}
return right <= it->second;
}
void removeRange(int left, int right) {
for (auto it = tree.upper_bound(right); it != tree.end() && it->second > left;) {
auto nt = next(it);
if (it->second > right) {
tree[right] = it->second;
}
if (it->first < left) {
it->second = left;
} else {
tree.erase(it);
}
it = nt;
}
}
};
#endif
//https://leetcode.com/problems/range-module/discuss/388941/Trust-me-it's-great-Generalizable-Segment-Tree-Solution330msand-140MB
// Check later Segment tree : 533 ms
template <typename T>
class MemoryPool {
public:
std::array<T, 10000000> mem_pool;
int ptr = 0;
T *offer() {
return &mem_pool[ptr++];
}
};
struct LogicalAndMonoid {
typedef bool T;
T append(T a, T b) const { return a && b; }
T unit() const { return true ;}
};
struct MaskOperatorMonoid {
typedef bool T;
typedef int F;
F identity() const { return -1; }
T apply(F a, T b) const {
if (a == -1) return b;
return a;
}
F compose(F a, F b) const {
return a;
}
};
template <typename M, typename O>
class Node {
public:
typedef typename M::T T;
typedef typename O::F F;
static constexpr M mon {};
static constexpr O op {};
static Node *build(int, int, T = mon.unit(), F = op.identity(), Node<M, O>* = nullptr, Node<M, O>* = nullptr);
public:
int ll, rr;
T val;
F f;
Node* lch;
Node* rch;
void push() {
if (lch != nullptr || rch != nullptr)
return;
// hard coded predicate to terminate push
if (ll +1 == rr)
return;
grow();
lch->f = op.compose(f, lch->f);
rch->f = op.compose(f, rch->f);
f = op.identity();
lch->val = op.apply(lch->f, lch->val);
rch->val = op.apply(rch->f, rch->val);
}
void retract() {
if (lch != nullptr) lch->retract();
if (rch != nullptr) rch->retract();
lch = nullptr;
rch = nullptr;
}
void grow() {
if (lch == nullptr) lch = build(ll, (ll + rr) / 2);
if (rch == nullptr) rch = build((ll + rr) / 2, rr);
}
};
MemoryPool<Node<LogicalAndMonoid, MaskOperatorMonoid>> mem_pool;
template <typename M, typename O>
Node<M, O>* Node<M, O>::build(int ll, int rr, T init_t, F init_f, Node<M, O>* lch, Node<M, O>* rch) {
Node * ret = mem_pool.offer();
ret->ll = ll;
ret->rr = rr;
ret->val = init_t;
ret->f = init_f;
ret->lch = lch;
ret->rch = rch;
return ret;
}
template <typename M, typename O>
class LazyDynamicSegmentTree {
public:
using T = typename Node<M, O>::T;
using F = typename Node<M, O>::F;
static constexpr M mon = Node<M, O>::mon;
static constexpr O op = Node<M, O>::op;
Node<M, O> *root;
LazyDynamicSegmentTree() {
root = Node<M, O>::build(0, 1000000000, false, false);
}
void range_apply(int a, int b, bool v) {
range_apply(root, a, b, v);
}
void range_apply(Node<M, O> * node, int a, int b, bool v) {
if(a >= node->rr || b <= node->ll) {
return;
}
Node<M, O> * &lch = node->lch;
Node<M, O> * &rch = node->rch;
if(a <= node->ll && b >= node->rr) {
node->val = op.apply(v, node->val);
node->f = op.compose(v, node->f);
node->retract();
return;
}
node->push();
range_apply(lch, a, b, v);
range_apply(rch, a, b, v);
node->val = op.apply(node->f, mon.append(lch->val, rch->val));
}
bool range_concat(int a, int b) {
return range_concat(root, a, b);
}
bool range_concat(Node<M, O> * node, int a, int b) {
if(a >= node->rr || b <= node->ll) {
return mon.unit();
}
if(a <= node->ll && b >= node->rr) {
return node->val;
}
if(node->lch == nullptr) {
return node->val;
}
// alternatively, one could do this.
// node->push();
return op.apply(node->f, mon.append(range_concat(node->lch, a, b), range_concat(node->rch, a, b)));
}
};
class RangeModule {
public:
LazyDynamicSegmentTree<LogicalAndMonoid, MaskOperatorMonoid> h;
RangeModule() : h() {
}
void addRange(int left, int right) {
h.range_apply(left, right, true);
}
bool queryRange(int left, int right) {
return h.range_concat(left, right);
}
void removeRange(int left, int right) {
h.range_apply(left, right, false);
}
};
/**
* Your RangeModule object will be instantiated and called as such:
* RangeModule* obj = new RangeModule();
* obj->addRange(left,right);
* bool param_2 = obj->queryRange(left,right);
* obj->removeRange(left,right);
*/