DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

LIS of Every Interval

Back to problem

Editorial

Let f(l,r)f(l,r)f(l,r) be the LIS length of the interval [l,r][l,r][l,r].

For a fixed right endpoint rrr, define

dl=f(l,r)−f(l,r−1)(1≤l<r).d_l=f(l,r)-f(l,r-1) \qquad (1 \leq l < r).dl​=f(l,r)−f(l,r−1)(1≤l<r).

Appending one element changes the LIS length by either 000 or 111, so every dld_ldl​ is 000 or 111.

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).f(l,r)+f(l+1,r-1) \leq f(l,r-1)+f(l+1,r).f(l,r)+f(l+1,r−1)≤

One way to see this is to place every permutation element as a point (i,Pi)(i,P_i)(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).f(l,r)-f(l,r-1) \leq f(l+1,r)-f(l+1,r-1).f(l,r)−f(l,r−1)≤f(l

Thus, for fixed rrr, the sequence d1,d2,⋯ ,dr−1d_1,d_2,\cdots,d_{r-1}d1​,d2​,⋯,d 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 r−1r-1r−1 are already known. A query for f(m,r)f(m,r)f(m,r) can be compared with the known value f(m,r−1)f(m,r-1)f(m,r−1), so it tells whether is or . Therefore, the boundary can be found by binary search.

After finding ttt, compute

  • f(l,r)=f(l,r−1)f(l,r)=f(l,r-1)f(l,r)=f(l,r−1) for l<tl<tl<t,
  • for ,

For one right endpoint rrr, at most ⌈log⁡2r⌉\lceil\log_2 r\rceil⌈log2​r⌉ queries are needed. For N=100N=100N=100, the total number of queries is

∑r=2100⌈log⁡2r⌉=1+4+12+32+80+192+252=573.\sum_{r=2}^{100}\lceil\log_2 r\rceil =1+4+12+32+80+192+252 =573.r=2∑100​⌈log2​r⌉

This is within the limit of 600600600 queries.

The running time is O(N2)O(N^2)O(N2) because all answers must be produced, and the number of queries is O(Nlog⁡N)O(N\log N)O(NlogN).

Solution written by GPT5.6

f
(
l
,
r
−
1)+
f(l+
1,r).
+
1,r)−
f(l+
1,r−
1).
r−1​
000
111
ttt
dl=0d_l=0dl​=0
l<tl<tl<t
dl=1d_l=1dl​=1
t≤l<rt \leq l<rt≤l<r
t=rt=rt=r
dld_ldl​
000
dmd_m
dm​
000
111
ttt
f(l,r)=f(l,r−1)+1f(l,r)=f(l,r-1)+1
f(l,r)=f(l,r−1)+1
t≤l<rt \leq l<rt≤l<r
  • f(r,r)=1f(r,r)=1f(r,r)=1.
  • =
    1+
    4+
    12+
    32+
    80+
    192+
    252=
    573.