PSEGTREE - Make Versions in Segment Tree
You have an array of N integers, named Version-0 array.
You need to do Q queries. There are 2 type of queries.
- idx pos v: Take Version-idx array and copy it into another array. Name the new array Version-K array where K = (number of queries of 1st type before this query + 1). Then add v the element at index pos in Version-K array.
- idx l r: In Version-idx array, sum the elements from index l to r. Print the sum of the range
First line there will be an integer N (1 <= N <= 100000), the length of the array. The following line wil contain N integers, the elements of Version-0 array. Each element is non-negative and at most 100.
The next line will contain an integer Q (1 <= Q <= 100000), the number of queries. Next Q lines will contain the queries. All queries in form
a b c d
If a = 1, then you have first kind of query and idx = b, pos = c, v = d.
If a = 2, then you have second kind of query and idx = b, l = c, r = d.
For all queries, it is guaranteed that Version-idx array exists. And
1 <= pos <= N
1 <= l <= r <= N
1 <= v <= 100
If you incounter an query of second type, you need to print the required sum in a seperate line. These should be printed in the order they appears in the input.
1 2 3 4 5 6 7 8 9 10
2 0 1 6
1 0 10 30
1 1 2 10
1 2 3 10
2 3 2 3 Output: 21
Thanks @Arefin.Implemented basic persistent seg-tree successfully!
Problem statement didn't mention maximum number of queries. max value of Q.
The query is to add the new value, not just replace it with the new value.
@forhad__sidhu apparently, updating by v means adding v to the element.
i think ,,,,,out[ut of query(2,3,2,3) is wrong ,isn't it is 20 ??Last edit: 2017-09-23 04:56:12