I read about a very simple problem, and tried to complex it further and further and included other concepts along with that. But I got stuck at one point and cannot resolve it further now.
Problem 1: You are given an array, Now there are Q queries that You have to find out next greater element of any element in this array.
Ex: Array: 1,2,4,3,5 then next greater element of 2 is 4, next greater element of 4 is 5 but 5 does not have any next greater element.
Solution: You can use Stack simply and can be solved in O(max(n,Q)). This can be find out here.
Problem 2: Now you have to find out next ith greater element, BUT when I am saying next ith greater element, I mean, Make an strictly increasing array greedily, starting from given element and tell me ith element in that. (If you see it closely it's related to problem 1).
Ex: Next 2nd greater element of 2 in 1,2,4,3,5 is 5 and not 3. Because increasing array would be 2,4,5 starting from 2 and made greedily.
Solution: This can be solved using DP, as I will make an array of n x log(n) and i,j element of table will store next 2^j th greater element index of ith element. This table can be filled easily column wise like this:
dp[i][j] = dp[dp[i][j-1]][j-1];
where 0th column was filled using stack as we solved it in problem 1. Hence query Complexity would be O(nlog(i)) and space complexity would be O(nlog(n)).
Problem 3: Now let's make problem 1 dynamic. Now let's say we can update a range of array by increasing or decreasing it by some number. And query next greater element of ith element.
Ex: Array: 1,2,4,3,5. Here next greater element of 4 is 5. But let's say I updated range[4,5] by 3 then new Array would be, 1,2,4,6,8. Now next greater element of 4 is 6.
Solution: I could not solve this problem in reasonable complexity, But point that I observed here is, If you are updating a range then only those elements would be effected(whose next greater would change) which lie in range but their next greater lie outside range and which are on left side of range but their next greater element lie inside or on right side of range.
So, I tweaked it little bit, and said that you have to see next greater element in small next range, if a greater element exist then OK otherwise say it -1. For Ex: if small range is 2 then next greater element for 6 in 4,3,2,6,5,7 is 7 but for 4 is none or say -1. Because 6 lie at a distance of 3.
Now this problem can be solved by involving Segment Tree into picture. As whenever there is range[lower,upper] update then we just have to update solution for left part(lower-smallRange,lower+smallRange) and for right part(upper-smallRange,upper+smallRange) in which solution for (lower-smallRange,lower) and (upper-smallRange,upper) would update only(It can be seen from above logic). And while applying stack method as used in problem 1, You have to get current value at index after multiple range update using Segment Tree in O(log(n)). So This can be solved in O(q * smallRange * log(n)) where q is number of queries.
Problem 4(Main Problem): Now let's combine Problem 2 and Problem 3, As there would be two types of queries now, In first query You have to find ith greater element of given element. and In Second query you can update range by some number. I could not solve it even after including smallRange concept that I used in Problem 3.
Ex: Array: 1,2,4,3,5. Here 2nd next greater element of 2 is 5. But let's say I updated range[4,5] by 3 then new Array would be, 1,2,4,6,8. Now 2nd next greater element of 2 is 6.
Solution: I was thinking to mix both solutions that I used in problem 2 and 3 but main problem is, in dp table elements other than smallRange(as mentioned in problem 3) would get affected because of 2^j concept used in table. And It will affect complexity badly. In other way, I could not take help from smallRange here which I used in problem 3. I also tried to think to use lazy propagation inside dp table but that did not help either.
I know this is long problem, but I tried to explain it stepwise. Hope Someone will read it and help me here.