aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/dsa/SplayTree_Range.hpp
blob: def7ef7e0b3c90a27ff20f37a00165654a4f4d8e (plain) (blame)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "SplayTree_Range.h"


#include <cstdlib>

#include <utility>

#include "../math/utility.h"

namespace meow{
  ///////////////////////////// **# Node #** /////////////////////////
  //////////////////// **# Node -- Constructure #** //////////////////
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::Node::Node(Key const& __key,
                                          Value const& __value):
  _key(__key), _keyOffset(0), _value(__value), _valueOffset(0), _range(__value){
    _same     = false;
    _size     = 1;
    _parent   = NULL;
    _child[0] = NULL;
    _child[1] = NULL;
  }
  ///////////////// **# Node -- Offset / Override #** ////////////////
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::Node::keyOffset(Key const& __delta){
    _key       = _key       + __delta;
    _keyOffset = _keyOffset + __delta;
  }
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::Node::valueUpdate(Value const& __delta,
                                                 bool __over){
    if(__over){
      _value       = __delta * _size;
      _valueOffset = __delta;
      _range       = __delta * _size;
      _same = true;
    }else{
      _value       = _value       + __delta * _size;
      _valueOffset = _valueOffset + __delta;
      _range       = _range       + __delta * _size;
    }
  }
  //////////////////////// **# Node -- sync #** //////////////////////
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::Node::syncDown() const{
    for(size_t i = 0; i < 2; i++){
      if(_child[i] == NULL) continue;
      _child[i]->keyOffset  (_keyOffset);
      _child[i]->valueUpdate(_valueOffset, _same);
    }
    ((Node*)this)->_keyOffset   = Key(0);
    ((Node*)this)->_valueOffset = Value(0);
    ((Node*)this)->_same        = false;
  }
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::Node::syncUp() const{
    ((Node*)this)->_size = 1;
    Value* v[3] = {&(((Node*)this)->_value), NULL, NULL};
    size_t vct = 1;
    for(size_t i = 0; i < 2; i++){
      if(_child[i] == NULL) continue;
      ((Node*)this)->_size += _child[i]->_size;
      v[vct++] = &(_child[i]->_range);
    }
    if     (vct == 1) ((Node*)this)->_range = (*v[0]);
    else if(vct == 2) ((Node*)this)->_range = (*v[0]) | (*v[1]);
    else              ((Node*)this)->_range = (*v[0]) | (*v[1]) | (*v[2]);
  }
  ////////////////////////// **# SplayTree_Range #** ///////////////////////
  ///////////////////// **# connection, splay #** ////////////////////
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::connect(Node const* __parent,
                                       size_t __left_right,
                                       Node const* __child) const{
    Node* parent = (Node*)__parent;
    Node* child  = (Node*)__child;
    if(parent != NULL) parent->_child[__left_right] = child;
    if(child  != NULL) child ->_parent              = parent;
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Node const*
  SplayTree_Range<Key, Value>::splay(Node const* __node) const{
    if(__node != NULL && __node->_parent != NULL){
      for(const Node *g_grand, *grand, *parent, *child = __node; ; ){
        g_grand = (grand = parent = child->_parent)->_parent;
        size_t pc = (parent->_child[0] == child ? 0 : 1);
        connect(parent,  pc, child->_child[!pc]);
        connect(child , !pc, parent);
        if(g_grand != NULL){
          g_grand = (grand = g_grand)->_parent;
          size_t gp = (grand->_child[0] == parent ? 0 : 1);
          Node const* who = (pc == gp ? parent : child);
          connect(grand,  gp, who->_child[!gp]);
          connect(who  , !gp, grand);
          grand->syncUp();
        }
        parent->syncUp();
        child ->syncUp();
        if(g_grand == NULL){
          connect(NULL, 0, child);
          break;
        }
        connect(g_grand, (g_grand->_child[0] == grand ?  0 : 1), child);
      }
    }
    return (((SplayTree_Range*)this)->_root = (Node*)__node);
  }
  ///////////////////////// **# clear, dup #** ///////////////////////
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::clear(Node* __node){
    if(__node == NULL) return ;
    clear(__node->_child[0]);
    clear(__node->_child[1]);
    delete __node;
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Node*
  SplayTree_Range<Key, Value>::dup(Node* __node){
    if(__node == NULL) return NULL;
    __node->syncDown();
    Node* node = new Node(__node->_key, __node->_value);
    connect(node, 0, dup(__node->_child[0]));
    connect(node, 1, dup(__node->_child[1]));
    node->syncUp();
    return node;
  }
  /////////////////////////// **# find #** ///////////////////////////
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Node const*
  SplayTree_Range<Key, Value>::findKey(Node const* __node,
                                       Key const& __key) const{
    Node const* ret = __node;
    while(__node != NULL){
      __node->syncDown();
      ret = __node;
      if(!(__key < __node->_key)){
        if(!(__node->_key< __key)) break;
        __node = __node->_child[1];
      }else{
        __node = __node->_child[0];
      }
    }
    return ret;
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Node const*
  SplayTree_Range<Key, Value>::findMinMax(Node const* __node,
                                          bool __minimum) const{
    Node const* ret = __node;
    for(int i = __minimum ? 0 : 1; __node != NULL; __node = __node->_child[i]){
      __node->syncDown();
      ret = __node;
    }
    return ret;
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Node const*
  SplayTree_Range<Key, Value>::findOrder(Node const* __node,
                                         size_t __order) const{
    Node const* ret = __node;
    while(__node != NULL){
      __node->syncDown();
      ret = __node;
      size_t ord = 1 + (__node->_child[0]==NULL ? 0:__node->_child[0]->_size);
      if     (ord == __order) return ret;
      else if(ord <  __order){ __node = __node->_child[1]; __order -= ord; }
      else                   { __node = __node->_child[0];                 }
    }
    return ret;
  }
  /////////////////////// **# split, merge #** ///////////////////////
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::split(Node* __root, Node** __left,
                                     Node** __right){
    if(__root == NULL){ *__left  = NULL; *__right = NULL; return ; }
    __root->syncDown();
    *__left  = __root;
    *__right = __root->_child[1];
    if(*__right != NULL){
      (*__left )->_child[1] = NULL;
      (*__right)->_parent   = NULL;
      (*__left )->syncUp();
    }
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Node*
  SplayTree_Range<Key, Value>::merge(Node* __left, Node* __right){
    if(__left  == NULL) return __right;
    if(__right == NULL) return __left ;
    __left->syncDown();
    connect(__left, 1, __right);
    __left->syncUp();
    return __left;
  }
  ///////////////////////// **# Element ##* //////////////////////////
  template<class Key, class Value>
  inline void SplayTree_Range<Key, Value>::Element::reset(Node* __node){
    _node = __node;
    delete _entry;
    if(__node == NULL) _entry = NULL;
    else               _entry = new Entry(__node->_key, __node->_value);
  }
  //
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::Element::Element():
  _entry(NULL), _node(NULL){
  }
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::Element::Element(Node* __node):
  _entry(NULL), _node(NULL){
    reset(__node);
  }
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::Element::Element(Element const& __element2):
  _entry(NULL), _node(NULL){
    reset(__element2._node);
  }
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::Element::~Element(){
    delete _entry;
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element&
  SplayTree_Range<Key, Value>::Element::operator=(Element const& __e2){
    reset(__e2._node);
    return *this;
  }
  //////////////////// **# Element operations #** ////////////////////
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element::Entry*
  SplayTree_Range<Key, Value>::Element::operator->(){ return  _entry; }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element::Entry&
  SplayTree_Range<Key, Value>::Element::operator*(){ return *_entry; }
  //
  template<class Key, class Value>
  inline bool
  SplayTree_Range<Key, Value>::Element::operator==(Element const& __e2) const{
    return (_node == __e2._node);
  }
  template<class Key, class Value>
  inline bool
  SplayTree_Range<Key, Value>::Element::operator!=(Element const& __e2) const{
    return (_node != __e2._node);
  }
  /////// **# Splay tree constructure/destructure/copy oper #** //////
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::SplayTree_Range(): _root(NULL){
  }
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::SplayTree_Range(SplayTree_Range const& __tree2):
  _root(NULL){
    _root = dup((Node*)(__tree2._root));
  }
  template<class Key, class Value>
  inline
  SplayTree_Range<Key, Value>::~SplayTree_Range(){
    clear(_root);
  }
  template<class Key, class Value>
  inline SplayTree_Range<Key, Value>&
  SplayTree_Range<Key, Value>::operator=(SplayTree_Range const& __tree2){
    clear(_root);
    _root = dup((Node*)(__tree2._root));
    return *this;
  }
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::moveTo(SplayTree_Range* __tree2){
    __tree2->clear();
    __tree2->_root = _root;
    _root = NULL;
  }
  //////////////////////// **# Bounding #** //////////////////////////
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::lowerBound(Key const& __key) const{
    splay(findKey(_root, __key));
    if(_root == NULL || !(_root->_key < __key)) return Element(_root);
    if(_root->_child[1] == NULL) return Element(NULL);
    splay(findMinMax(_root->_child[1], true));
    return Element(_root);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::upperBound(Key const& __key) const{
    splay(findKey(_root, __key));
    if(_root == NULL || __key < _root->_key) return Element(_root);
    if(_root->_child[1] == NULL) return Element(NULL);
    splay(findMinMax(_root->_child[1], true));
    return Element(_root);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::rLowerBound(Key const& __key) const{
    splay(findKey(_root, __key));
    if(_root == NULL || !(__key < _root->_key)) return Element(_root);
    if(_root->_child[0] == NULL) return Element(NULL);
    splay(findMinMax(_root->_child[0], false));
    return Element(_root);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::rUpperBound(Key const& __key) const{
    splay(findKey(_root, __key));
    if(_root == NULL || _root->_key < __key) return Element(_root);
    if(_root->_child[0] == NULL) return Element(NULL);
    splay(findMinMax(_root->_child[0], false));
    return Element(_root);
  }
  ////////////// **# find / order / first / last / end #** ////////////
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::find(Key const& __key) const{
    splay(findKey(_root, __key));
    if(_root != NULL && !(__key < _root->_key) && !(_root->_key < __key)){
      return Element(_root);
    }
    return Element(NULL);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::order(size_t __order) const{
    if(_root == NULL || __order >= _root->_size) return Element(NULL);
    splay(findOrder(_root, __order + 1));
    return Element(_root);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::first() const{
    splay(findMinMax(_root, true));
    return Element(_root);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::last() const{
    splay(findMinMax(_root, false));
    return Element(_root);
  }
  template<class Key, class Value>
  inline typename SplayTree_Range<Key, Value>::Element
  SplayTree_Range<Key, Value>::end() const{ return Element(NULL); }
  //////////////////// **# query, range query #** ////////////////////
  template<class Key, class Value>
  inline Value
  SplayTree_Range<Key, Value>::query() const{
    if(_root == NULL) return Value(0);
    return _root->_range;
  }
  template<class Key, class Value>
  inline Value
  SplayTree_Range<Key, Value>::query(Key const& __first,
                                     Key const& __last) const{
    SplayTree_Range* self = (SplayTree_Range*)this;
    Node* tmp;
    rUpperBound(__first);
    self->split(self->_root, &tmp, &(self->_root));
    upperBound(__last);
    Value ret(0);
    if(_root != NULL && _root->_child[0] != NULL){
      ret = _root->_child[0]->_range;
    }
    self->_root = self->merge(tmp, self->_root);
    return ret;
  }
  //////////////////// **# size, empty, clear #** ////////////////////
  template<class Key, class Value>
  inline size_t SplayTree_Range<Key, Value>::size() const{
    return (_root == NULL ? 0 : _root->_size);
  }
  template<class Key, class Value>
  inline bool SplayTree_Range<Key, Value>::empty() const{
    return (size() == 0);
  }
  //
  template<class Key, class Value>
  inline void SplayTree_Range<Key, Value>::clear(){
    clear(_root);
    _root = NULL;
  }
  ////////////// **# insert, erase, keyOffset, oper[] #** ////////////
  template<class Key, class Value>
  inline bool SplayTree_Range<Key, Value>::insert(Key const& __key,
                                                  Value const& __value){
    if(_root == NULL){
      _root = new Node(__key, __value);
    }else{
      Node* parent = (Node*)findKey(_root, __key);
      if(!(parent->_key < __key) && !(__key < parent->_key)){
        splay(parent);
        return false;
      }
      Node* new_node = new Node(__key, __value);
      connect(parent, (parent->_key < __key ? 1 : 0), new_node);
      parent->syncUp();
      splay(new_node);
    }
    return true;
  }
  template<class Key, class Value>
  inline bool SplayTree_Range<Key, Value>::erase(Key const& __key){
    if(_root == NULL) return false;
    Node* body = (Node*)findKey(_root, __key);
    if(body->_key < __key || __key < body->_key){
      splay(body);
      return false;
    }
    Node* ghost;
    if(body->_child[1] == NULL){
      ghost = body->_child[0];
      if(ghost != NULL) ghost->syncDown();
    }else{
      ghost = (Node*)findMinMax(body->_child[1], true);
      connect(ghost, 0, body->_child[0]);
      if(ghost != body->_child[1]){
        connect(ghost->_parent, 0, ghost->_child[1]);
        connect(ghost, 1, body->_child[1]);
        for(Node* a = ghost->_parent; a != ghost; a = a->_parent)
          a->syncUp();
      }
      ghost->syncUp();
    }
    Node* parent = body->_parent;
    connect(parent, parent != NULL && parent->_child[0] == body ? 0 : 1,
            ghost);
    delete body;
    splay(ghost != NULL ? ghost : parent);
    return true;
  }
  template<class Key, class Value>
  inline void SplayTree_Range<Key, Value>::keyOffset(Key const& __delta){
    if(_root != NULL){
      _root->keyOffset(__delta);
    }
  }
  template<class Key, class Value>
  inline void SplayTree_Range<Key, Value>::valueOffset(Value const& __delta){
    if(_root != NULL){
      _root->valueUpdate(__delta, false);
    }
  }
  template<class Key, class Value>
  inline void SplayTree_Range<Key, Value>::valueOverride(Value const& __value){
    if(_root != NULL){
      _root->valueUpdate(__value, true);
    }
  }
  template<class Key, class Value>
  inline Value&
  SplayTree_Range<Key, Value>::operator[](Key const& __key){
    if(find(__key) == end()) insert(__key, Value());
    return _root->_value;
  }
  /////////////////////// **# split, merge #** ///////////////////////
  template<class Key, class Value>
  inline void
  SplayTree_Range<Key, Value>::splitOut(Key const& __upper_bound, SplayTree_Range* __right){
    __right->clear();
    if(rLowerBound(__upper_bound) != end()){
      split(_root, &_root, &(__right->_root));
    }else{
      __right->_root = _root;
      _root = NULL;
    }
  }
  template<class Key, class Value>
  inline bool
  SplayTree_Range<Key, Value>::mergeAfter(SplayTree_Range* __tree2){
    if(_root == NULL || __tree2->_root == NULL ||
       last()->first < __tree2->first()->first){
      _root = merge(_root, __tree2->_root);
      __tree2->_root = NULL;
      return true;
    }
    return false;
  }
  template<class Key, class Value>
  inline bool
  SplayTree_Range<Key, Value>::merge(SplayTree_Range* __tree2){
    if(_root == NULL || __tree2->_root == NULL ||
       last()->first < __tree2->first()->first){
      _root = merge(_root, __tree2->_root);
    }else if(__tree2->last()->first < first()->first){
      _root = merge(__tree2->_root, _root);
    }else{
      return false;
    }
    __tree2->_root = NULL;
    return true;
  }
}