-
Notifications
You must be signed in to change notification settings - Fork 9
/
README
70 lines (49 loc) · 1.72 KB
/
README
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
Segment Tree with range operations
==================================
.. figure:: https://img.shields.io/badge/license-MIT-blue.svg
:alt: LicenseLink
This is a general implementation of a segment tree for Python 3.
- Semigroup range operations in O(logN) time
- Built-in support for ``max``, ``min``, ``sum`` operations
- Extensible to support more semigroup operations
- Limited support for multidimensional trees
- Python 2 is not currently supported
Installation
============
``pip3 install segment-tree``
Segment Tree (one-dimensional)
==============================
Basic usage
-----------
.. code:: python
from segment_tree import *
array = [3, 1, 5, 3, 13, 7, 2, 7, 2]
tree = SegmentTree(array)
t.query(1, 3, "sum") # 9
t.update(0, 10) # [10, 1, 5, 3, 13, 7, 2, 7, 2]
t.query(0, 8, "min") # 0
t.update(2, -1) # [10, 1, -1, 3, 13, 7, 2, 0, 2]
t.query(0, 2, "min") # -1
Updates on ranges
-----------------
.. code:: python
from segment_tree import *
array = [1, 2, 3, 4, 5]
t = SegmentTree(array)
t.update_range(0, 2, 6) # 6 6 6 4 5
t.update_range(1, 4, 2) # 6 2 2 2 2
t.query(0, 3, "min") # 2
Multidimensional Segment Tree (alpha version, might have bugs)
==============================================================
Basic usage
-----------
.. code:: python
from segment_tree import *
a = [[[1, 2], [1, 3]], [[-1, -2], [-1, -3]]]
tree = MultidimensionalSegmentTree(a)
tree.query([(0, 1), (0, 0), (0, 0)], max) # 1
tree.query([(1, 1), (0, 1), (0, 1)], sum) # -7
tree.query([(0, 1), (1, 1), (0, 1)], min) # -3
Tests
=====
Execute ``python3 setup.py test`` to run tests.