# Dynamic array updated by adding arithmetic progressions

I have an array $$a_1, a_2, \dots, a_n$$. After $$O(n)$$ preprocessing, I want to be able to compute each of the following in amortized $$O(\log n)$$:

1. Given $$l,r,b,d$$, for each $$i \in \{l,\ldots,r\}$$, increase $$a_i$$ by $$b + (i-l)d$$.
2. Given $$l,r$$, compute $$\sum_{i=l}^r a_i$$.
• Where are all these contest-style questions coming from? – Yuval Filmus Apr 22 at 17:50
• What is the best solution you've found so far, and what running time does it have for each of those two operations? – D.W. Apr 22 at 18:06
• It seems to me that I almost solved the problem. I came up with storing a progression step and the first member of the progression at each vertex of the tree of segments. And when the request arrives, it’s easy to calculate the required amount. And when a request for a sub-segment is received regarding the first request, then I will push it to the descendants, counting q. But I'm not sure that I can build such a tree of segments in O (n). – Maxim Apr 22 at 22:15

Hint: find a data structure that supports these three basic operations:

1. Given $$l,r,b$$, for each $$i \in \{l,\dots,r\}$$, increase $$a_i$$ by $$b$$.
2. Given $$l,r,d$$, for each $$i \in \{l,\dots,r\}$$, increase $$a_i$$ by $$id$$.
3. Given $$l,r$$, compute $$\sum_{i=l}^r a_i$$.

You can then use those basic operations to implement your operation 1.

Can you think of any way to support basic operations 1+3? Can you think of any way to support basic operations 2+3? If you can answer both of those questions with a "yes", you will probably then be able to solve your problem.

• I think, for operations 1+3 I can use default segment tree. For operations 2+3 i think for operations 2 + 3, I think I need to use a modified tree of segments: at the top of each subtree, I need to store the value by which I need to increase this sub-segment. – Maxim Apr 22 at 18:58
• Can you tell me how solving this subtask will help me solve the main problem? – Maxim Apr 22 at 19:00
• @Maxim, it's your task, so I suggest spending some more time thinking about it. I notice that you haven't responded to the other requests (about the source of this problem, or what attempts you've made), which makes it look like you're hoping someone will solve it for you. – D.W. Apr 22 at 21:47