So, basically the problem is that there is a series of hill points. we can only move from one hill point to the next greater hill point.
Please see the below image to get an idea.
On above image black points are hill tops, each hill top has some spices with some taste value. we can move from one hill top to another tasting spices in them.
Please See below image to know valid movements. green lines gives valid movements
.
So, Now we will be given a query with 2 integers b,c and for each query we need to find if moving from b hill point to c is possible. if it is then we have to output the total tastiness of journey.
Suppose a query comes in which b = 1 , c = 10.
So we need to find if moving from hill 1 to hill 10 possible and if it is we have to output the tastiness of journey.
The problem is simple if there are very low queries, as for each query we can just iterate in loop and find if reaching from 1 to 10 is possible or not and if it is then we will sum the taste.
for this particular problem our path will be 1-->2-->6-->7-->9-->10.
But if a query comes such that b = 1 , c = 11.
so we cant go from 1 to 11, hence no path is possible and answer is -1.
But when there are very large queries we can iterate in loop for every query. I mean we have to pre-process the data, so that in each query we just calculate the result in constant time.
What particularly i want to know ?
How can i know if reaching from b to c is possible or not in constant time.
If path is possible than how to calculate the sum of path in constant time.