In depth tutorial for Adding Arithmetic progression to range using Segment Tree wrt Problem Mister b and PR Shifts
Difference between en6 and en7, changed 6 character(s)
```↵
If your are familier with concept of Segment Trees with Lazy Update, Please go ahead and read the solution. ↵
If not, please do these two classic simple problems of Segment Trees with Lazy Update on Codchef. ↵
Then come back to this problem. This problem is a classic implementation of Segment Trees. ↵
Purpose of this tutorial is to help you find your solution and not just giving the solutions.↵
```↵
~~~~~↵
Your code here...↵
~~~~~↵



* [Flipcoin Codechef](https://www.codechef.com/problems/FLIPCOIN) <br/>↵
  You can read my commented code for this flipcoin (https://www.codechef.com/viewsolution/4341486)↵
* [MultQ3 Codechef](https://www.codechef.com/problems/MULTQ3)  <br/>↵
  You can read my commented code for this MultQ3 (https://www.codechef.com/viewsolution/4342905)↵
  ↵
  ![Alt text](https://user-images.githubusercontent.com/26462566/27768658-46f17eb6-5f36-11e7-8c24-d1ff7072dcd8.jpg) <br/>↵
 Now we will consider with above example. Hope this example is self explanatory.↵
 Now think how you can exploit this concept of adding a value in a range. Try yourself. Think in terms of segment tree.↵
 ↵
 <b>Now step two &mdash; Thinking how actually this problem is adding in a range problem. </b> ↵
 ↵
 ![Text](https://user-images.githubusercontent.com/26462566/27768833-3209fb0a-5f3a-11e7-8a84-e5a520a56c97.jpg) <br/>↵
 <br/>↵
 ↵
```↵
 Consider example &mdash;  ↵
        ↵
          P1 P2 P3 P4 P5 P6 P7 P8 P9 P10<br/>↵
  8  3  5  7  4  9  10 1  2  6   <br />↵

 Think all the case i.e. in what range we add to what values. ? Try yourself. <br/>   ↵
 Case 1. p[i] >= i <br/>↵
         Take p[4] = 7<br/>↵
         for shift id             Id0   Id1  Id2  Id3  Id4  Id5 Id6  Id7  Id8  Id9<br/>↵
 value which is to be <br/>↵
 added  -->               3     2    1    0    1    2   3    6    5    4<br/>↵
 <br/>↵
 (hope you understands this. !!!!)   bingo go ahead...<br/>↵
 deduction now <br/>↵
  <br/>↵
  Case 1.1   We have to add values (3, 2, 1, 0) for shift id range (0, 1, 2, 3)<br/>↵
  Case 1.2   We have to add values (1, 2, 3, 0) for shift id range (4, 5, 6)<br/>↵
  Case 1.3   We have to add values (6, 5, 4) for shift id range (7, 8, 9)<br/>↵
 Case 2. p[i] < i <br/>↵
         Try yourself. Code will help. Code is commented. Please see if you are struck. <br/>↵
```↵
  ↵

 ↵
 ↵
 ```↵
 So now we have to add a arithmetic progression in some range in a segment tree.↵
  ```↵
 Adding some value to Segment tree is direct problem. Now think how we can add AP to ST.↵
 Before going to this, please evaluate what must be final answer and time complexity.↵
 ↵
 ```↵
 Since leaves store the value which are added to a particular shift. ↵
        Like 1st leave store value to be added for 0 shift.↵
     2nd leave store value to be added for 1st shift.↵
 Hence we need to take the minimum of all leaves.↵
 Therefore query segment tree once in the end. ↵
 Updating Segment tree is O(Logn)↵
 Total time complexity O(nLogn).↵
 ```↵

 <b> Step 3 &mdash; Adding Arithmetic Progression to Segment Tree using Segment Tree with Lazy propogation. </b> 
 
  ↵
  ![text](https://user-images.githubusercontent.com/26462566/27769389-eb92e45a-5f45-11e7-99a9-97ca938bdaa5.jpg)↵

 ↵
  ```↵
  Now, consider a segment tree with 0-7 as nodes. i.e. it means we have shift id from 0 to 7. leaves represents ↵
  value to be added for shift id 0,1,2,3...,7.  ↵
  Now if we have updation query to add values (0,1,2,3) in range (2,5) of segment tree.↵
  Refer to above diagram.↵
  we are splittng range (2,5) in two parts 1. range(2,3) 2. range(4,5) ie there are 4 nodes and 2 nodes in both ↵
  left & right tree.↵
  So, value(0,3) also has to be splitted in same way.↵
  So Left Tree range(2,3)↵
               value(0,1)↵
     Right Tree range(4,5)↵
               value(2,3).↵
  ↵
  General formulae :-↵
  we have range(l,r) in which we have to update.↵
          value as (valueLeft, valueRight) values to be updated↵
  (a,b) current range in our segment tree↵
  ↵
  N = number of nodes in (l,r) = r - l +1↵
  this N also is number values in AP in (valueLeft, valueRight)↵
  valueLeft + (N-1)*Difference = valueRight↵
  Difference = (valueRight - valueLeft) / (N-1). //Corner case if N==1.↵
  so now we have to split (l,r) in two parts having ↵
  Leftnodes = (a+b)/2 - l + 1↵
  Rightnodes = (r- (a+b)/2).↵
      ↵
                                            (valueLeft,valueRight)↵
                              |↵
                           ----------------    |   ----------------------↵
(valueLeft, valueLeft+(Leftnodes-1)*Difference )          (valueLeft+(Leftnodes)*Difference, valueRight) ↵

  ↵
  ```↵

  My solution with comments :- [Accepted Submission](http://codeforces.com/contest/819/submission/28197900)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en10 English uditgupta 2017-07-02 15:21:46 2 Tiny change: 'POLOGIES\n```\nIf ' -> 'POLOGIES\n\n```\nIf '
en9 English uditgupta 2017-07-02 15:21:22 143
en8 English uditgupta 2017-07-02 14:59:17 50
en7 English uditgupta 2017-07-02 14:54:10 6
en6 English uditgupta 2017-07-02 14:50:35 15
en5 English uditgupta 2017-07-02 14:48:00 34
en4 English uditgupta 2017-07-02 14:44:44 10
en3 English uditgupta 2017-07-02 14:43:48 39 Tiny change: 'ns.\n```\n* [Flipc' -> 'ns.\n```\n~~~~~\nYour code here...\n~~~~~\n\n\n\n* [Flipc'
en2 English uditgupta 2017-07-02 14:42:11 0 (published)
en1 English uditgupta 2017-07-02 14:41:49 4797 Initial revision (saved to drafts)