Let be the LIS length of the interval .
For a fixed right endpoint , define
Appending one element changes the LIS length by either or , so every is or .
The interval LIS matrix satisfies the following unit-Monge inequality:
One way to see this is to place every permutation element as a point and interpret an increasing subsequence as an up-right path. Two optimal paths can be uncrossed by exchanging their suffixes, which gives the inequality.
Rearranging it gives
Thus, for fixed , the sequence is nondecreasing. Since every value is either or , there is a boundary such that for and for . The case means that every is .
Assume that all answers ending at are already known. A query for can be compared with the known value , so it tells whether is or . Therefore, the boundary can be found by binary search.
After finding , compute
For one right endpoint , at most queries are needed. For , the total number of queries is
This is within the limit of queries.
The running time is because all answers must be produced, and the number of queries is .
Solution written by GPT5.6