Segment Tree¶
A segment tree is a data structure that supports two operations: processing a range query and updating an array value.
Segment trees can support sum queries, minimum and maximum queries and many other queries so that both operations work in $\Omicron(lgn)$ time.
DataStructure | Operations |
---|---|
Binary indexed tree | sum queries |
Segment tree | sum, minimum, maximum and many others |
Structure¶
A segment tree is a binary tree such that the nodes on the bottom level of the tree corresponds to the array elements, and the other nodes contain information needed for processing range queries.
Time complexity¶
Operation | Time complexity |
---|---|
build() | $\Omicron(n)$ |
query(l, r) | $\Omicron(\log(n))$ |
update(pos, x) | $\Omicron(\log(n))$ |
Implementation¶
Following implementation support single array element update
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 |
|