Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Build a Segment Tree Given a Circular Array?

Given a circular array A, how would you construct a segment tree off of A? Do you use two separate arrays for the segment tree or some other method?

If possible, it would also be very helpful if someone could explain how the range update and range queries would work for such a case.

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We only use segment trees for associative operations, like "sum" and "min" and "xor", where if I give you the result for [startmid) and [midend), you can directly compute the result for [startend).

So you don’t actually need to do anything special for circular arrays; you can build and maintain your segment tree exactly the same as if the array were not interpreted as circular. To compute the result for an interval of the form [greater_indexlesser_index) that wraps around the end of the array, just use the normal method to compute the results for [greater_indexarray_size) and [0, lesser_index), and combine them as normal.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading