I have faced some problems of this kind. Suppose we are given an array A of n elements where n is around 1 million. We are given queries of form 'p q'. In each query, we have to perform some operation on the elements A[p] to A[q].
XOR or something. For ex- taking XOR of every number in A( from index p to q )with a given number m. For ex- A[p]^m,A[p+1]^m..A[q]^mIf we are given thousands of queries of type 'p q' then, we do repetitive calculations for overlapping ranges (for ex.-p1<p2<q1<q2). If have a space/time tradeoff, I would prefer to do my work faster even if it takes more space. So, I am concentrating on time not much on space.
My question is - Is there any data structure which can process these type of queries(having some range which repeats in further queries) efficiently. What I can think of is something like- doing some pre-processing and saving things in a data structure. And then updating and processing as we receive further queries.
Can someone suggest some data structure which is helpful in processing the queries which are concerned with some ranges (specially when the ranges are further repeated extensively)?
The first thing I thought of was a Segment Tree with lazy propagation but it only really applies if the query you described is really an update to the table and doesn't return the result. So, there would be an update[p q] operation and a query[p q]. The first just updates using an operation like you described and the second returns the values from the range.
Also see this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With