Блог пользователя Sahu_1402

Автор Sahu_1402, история, 11 месяцев назад, По-английски

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

  • Проголосовать: нравится
  • +15
  • Проголосовать: не нравится

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

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

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

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 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +8 Проголосовать: не нравится

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