segment-tree | Rust implementation of two segment trees | Dataset library
kandi X-RAY | segment-tree Summary
kandi X-RAY | segment-tree Summary
A Rust implementation of two segment trees and a fenwick tree.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of segment-tree
segment-tree Key Features
segment-tree Examples and Code Snippets
Community Discussions
Trending Discussions on segment-tree
QUESTION
I was going through an article in geeksforgeeks on how to implement dictionary using balanced BST and found this line:
If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree.
I didn't understand how this would be O(M*logn), given balanced BSTs always maintain a max height of O(logn), shouldn't this be (logn)?
...ANSWER
Answered 2022-Jan-31 at 07:14This quote is taken from a context of searching words in a dictionary:
Trie is an efficient data structure for searching words in dictionaries, search complexity with Trie is linear in terms of word (or key) length to be searched. If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree. Using trie, we can search the key in O(M) time. So it is much faster than BST.
In a BST these words would be stored such that one node gets one word.
Although in a balanced BST of 𝑁 nodes you need to visit O(log𝑁) nodes to find the targeted value in the worst case, it will also take time to compare a node's string with the target string. As the maximum length of a word is given as 𝑀, the worst case time complexity for each individual string comparison is O(𝑀). As such a comparison happens at each visited node, this gives a total complexity of O(𝑀log𝑁).
QUESTION
given N elements of an array compute the sum (min*max) across all the subarrays of the array.
e.g. N = 5
Array: 5 7 2 3 9
output: 346 (5*5 + 7*7 + 2*2 + 3*3 + 9*9 + 5*7 + 2*7 + 2*3 + 3*9 + 2*7+2*7 + 2*9 + 2*7 + 2*9 + 2*9)
i cannot think of anything better than O(n^2). The editorial solution uses segment trees which i couldnt understand.
...ANSWER
Answered 2020-May-18 at 01:29Hint regarding the editorial (the details of which I am uncertain about): if we can solve the problem for all the intervals that include both A[i]
and A[i+1]
, where i
divides A
in half, in O(n)
time, then we can solve the whole problem in O(n log n)
time using divide and conquer, solving left and right separately, and adding to that the intervals that overlap both left and right.
QUESTION
I'm referring to this post : https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
Pasting the code here for reference. My questions are below the code.
...ANSWER
Answered 2020-Apr-10 at 01:20The variable p
doesn't mean parent, it means child. In for
loop, i
is child node, and we update value of i
's parent.
tree[p+n] = value;
: update the value of leave node (node without children). Then we update value of node's parent from the leave node. tree[i>>1] = tree[i] + tree[i^1];
, tree[i>>1]
is tree[i]
' parent.
For example: the array size is 16 (the tree size is 32) and I want to update arr[8]. So I call updateTreeNode(8, value)
. First three[8+16]
is updated, which corresponds to arr[8]
. Then p
is set to 24. In for loop, we update p's parent (tree[12]), then set p to p/2 (p=12), until p doesn't have parent.
If l
and r
is even, we add the parent's value in next recurrence. That's the function of segment tree, to avoid querying each element in the interval.
For example: the array size is 16 and I want to query [8,10). In segment tree, the interval is [24,26). We don't need to add value of tree[24]
and tree[25]
, we add value of tree[12]
!
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install segment-tree
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page