Let f(l,r) be the LIS length of the interval [l,r].
For a fixed right endpoint r, define
dlβ=f(l,r)βf(l,rβ1)(1β€l<r).
Appending one element changes the LIS length by either 0 or 1, so every dlβ is 0 or 1.
The interval LIS matrix satisfies the following unit-Monge inequality:
f(l,r)+f(l+1,rβ1)β€f(l,rβ1)+f(l+1,r).
One way to see this is to place every permutation element as a point (i,Piβ) 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
f(l,r)βf(l,rβ1)β€f(l+1,r)βf(l+1,rβ1).
Thus, for fixed r, the sequence d1β,d2β,β―,drβ1β is nondecreasing. Since every value is either 0 or 1, there is a boundary t such that dlβ=0 for l<t and dlβ=1 for tβ€l<r. The case t=r means that every dlβ is 0.
Assume that all answers ending at rβ1 are already known. A query for f(m,r) can be compared with the known value f(m,rβ1), so it tells whether dmβ is 0 or 1. Therefore, the boundary t can be found by binary search.
After finding t, compute
- f(l,r)=f(l,rβ1) for l<t,
- f(l,r)=f(l,rβ1)+1 for tβ€l<r,
- f(r,r)=1.
For one right endpoint r, at most βlog2βrβ queries are needed. For N=100, the total number of queries is
r=2β100ββlog2βrβ=1+4+12+32+80+192+252=573.
This is within the limit of 600 queries.
The running time is O(N2) because all answers must be produced, and the number of queries is O(NlogN).
Solution written by GPT5.6