Given an array of n integers and another array of length m of type [li,ri].
You are also given q queries and in each query you will get an index and a value and you will update arr[index] = value, then you need to print the maximum(arr[li] + arr[li+1] + arr[li+2] + .... + arr[ri]) over all 1<=i<=m.
Constraints : 1<=n,m,q<=1e5; -1e9<=arr[i]<=1e9
Eg. — arr = [1,3,-1,2]
brr = [[0,2],[3,3]]
index = 1 , value = -1
arr = [1,-1,-1,2]
sum([0,2]) = -1
sum([3,3]) = 2
max(-1,2) = 2
ans = 2








might be overkill but an implicit treap can definitely work here.
Could you please elaborate?
You can maintain the maximum subarray sum on every node of an implicit treap.
Then you can easily query for the maximum subarray sum of any interval while being able to update the value of any node.
Both queries and updates are $$$O(\log N)$$$.
Although this might be possible with a segment tree as well? I'm not sure.
This is solvable online using a segment tree.
Problem: SPOJ GSS3
In each node you keep the max prefix sum, max suffix sum, total sum and the answer. The merge is as following:
Now for each range query you need to print
node.ans.Thanks for your reply but I think you misread my question. In the question you mention we need to print the maximum sum of the subarray which is between [x,y] but the question which I stated we need to print maximum(sum(a[li] + a[li +1] + a[li + 2] + ... + a[ri])) over all 1<=i<=m.
i also misread it you should restate the statement
My bad, I just restated the problem, is it understandable now?
I guess that you can compute the original sum for each interval $$$[l_{i}, r_{i}]$$$ and after that, the problem reduces to adding some $$$x$$$ to all the intervals with $$$l \leq i \leq r$$$ and maintaining the maximum of those values.
You can do this in $$$O(\log^{2}{n})$$$ by using a Fenwick Tree in which each position $$$i$$$ stores the pairs $$$(r, value)$$$ of the intervals with $$$l \in [i - LSO(i) + 1]$$$ (Fenwick Tree nature) in a treap.
The operations reduce to:
Since once you iterate through the Fenwick Tree you have already guaranteed that $$$l \leq i$$$, you will only need to focus on updating the intervals with $$$i \leq r$$$, which can be done in $$$O(\log{n})$$$ in a treap with lazy propagation.
Since you will iterate over at most $$$O(\log{n})$$$ treaps, the queries can be solved in $$$O(\log^{2}{n})$$$.
EDIT: This idea doesn't work, but you can use SQRT decomposition to keep the intervals with $$$l$$$ inside the corresponding bucket and apply the same idea of lazy propagation for the values with $$$i \leq r$$$. This increases the query complexity to $$$O(\sqrt{n}\log{n})$$$.