Sahu_1402's blog

By Sahu_1402, history, 11 months ago, In English

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

  • Vote: I like it
  • +15
  • Vote: I do not like it

»
11 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

might be overkill but an implicit treap can definitely work here.

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Could you please elaborate?

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it +5 Vote: I do not like it

      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.

»
11 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

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:

Info in each node

Now for each range query you need to print node.ans.

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it -8 Vote: I do not like it

    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.

»
11 months ago, hide # |
Rev. 2  
Vote: I like it +8 Vote: I do not like it

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:

void insert(int l, int r, long long value) {
   while(l <= n) {
      T[l].insert(r, value);
      l += (-l) & l;
   }
}

void update(int l, int r, long long add) {
   while(l <= n) {
      T[l].add_to_range(r, n, add);
      l += (-l) & l;
   }
}

long long get_max() {
   long long res = LLONG_MIN;
   for(int p = n; p > 0; p &= p - 1) {
      res = max(res, T[p].get_max());
   }
   return res;
}

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})$$$.