I am facing a problem in lazy propagation of segment tree. I have an array A, of length N ,of smaller arrays (of max length 20). I also have an array of indices B, referring to the index i am currently pointing to in the array Ai.
There are 2 operations,: a) update the pointers of B for a given range to point to the next element. b) print the max of the values the pointers are currently pointing to in the given range.
for example:-
int[][] array=
{
{1,2,3,4},
{8,4,0,0},
{3,4,2,5}
};
int[] B={1,1,1};
On making a query for range 1,2 max is 8. This is because the pointers of array B are pointing to the first elements of the array.So we are working with 1,8.
On making a query of 2,3 max =8; this is because we are working with the values 8,3. In general,
int max(int[][] arr,int[] b,int l,int r){
int max=0;
for(int i=l;i<=r;i++){
max=Math.max(max,arr[i][b[i]]);//using java Math class here
}
return max;
}
void update(int[] b,int l,int r){
for(int i=l;i<=r;i++){
b[i]++;
}
}
These are the two methods in a very simple form. However ,due to large input constraints, i need a O(logn) query and update time.That is why i thought of using segment trees(currently it's complexity is O(n^2).However I cannot seem to figure out how to update the intermediate nodes in lazy propagation.
Any insight will be helpful. Also, if you could link any similar problem online, it would be really helpful as I could not(I do not know of any such as this is not from any website). Thank you for any help.
NOTE : If b[i]>a[i].length
then replace a[i][b[i]]
with 1.