DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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 account linking.

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

Awesome Intervals 1.5

Back to problem

Editorial

Reachability An interval [l,r][l,r][l,r] is awesome exactly when, for each i∈[l,r]i\in[l,r]i∈[l,r], every integer in [Ai,Bi)[A_i,B_i)[Ai​,Bi​) occurs among Bl,…,Bi−1B_l,\ldots,B_{i-1}Bl​,…,Bi−1​. The requirement is empty when Ai=BiA_i=B_iAi​=Bi​. For sufficiency, finish the elements from left to right. Each increment from vvv to v+1v+1v+1 uses a finished earlier element equal to vvv. For necessity, an increment at position iii from vvv to v+1v+1v+1 needs an earlier element equal to vvv. If that element ends above vvv, its own increment past vvv needs an even earlier element equal to vvv. Indices strictly decrease, so this chain must end at an element whose final value is vvv. A final value below vvv is impossible because values never decrease.

Allowed starting positions Let last⁡i(v)\operatorname{last}i(v)lasti(v) be the largest j<ij<ij<i with Bj=vB_j=vBj​=, or if absent. For , define

ti=min⁡Ai≤v<Bilast⁡i(v).t_i=\min{A_i\le v<B_i}\operatorname{last}_i(v).ti​=minAi​≤v<B

Set ti=it_i=iti​=i when Ai=BiA_i=B_iAi​=B. Then is awesome exactly when for all . Compress the distinct values of in sorted order. If the number of distinct values in is less than , set . Otherwise all required integers exist in the compressed domain, and a range-minimum segment tree of last occurrences computes . Initialize occurrences to , query before updating the occurrence of to , and never enumerate a potentially huge value interval.

Allowed ending positions For each lll, find the first i≥li\ge li≥l with ti<lt_i<lti​<l. Set fl=i, or if there is no such position. The awesome intervals starting at have precisely the ends . If , none exist. A segment tree over finds the first violating position by descending into nodes whose minimum is below , in per start.

Offline counting Let F(x,y)F(x,y)F(x,y) count awesome intervals with l≤xl\le xl≤x and r≤yr\le yr≤y; it is zero if either argument is negative. Each query equals

F(e1,e2)−F(s1−1,e2)−F(e1,s2−1)+F(s1−1,s2−1).F(e_1,e_2)-F(s_1-1,e_2)-F(e_1,s_2-1)+F(s_1-1,s_2-1).F(e1​,e2​

Put k=min⁡(x,y)k=\min(x,y)k=min(x,y). Only valid starts l≤kl\le kl≤k with fl≥lf_l\ge lfl contribute, each by . Let and be their count and sum of indices, obtained from prefix sums. Process all prefix queries in increasing . Insert valid starts with into two Fenwick trees indexed by , storing their count and sum of . Let the prefix results through be and . Then

F(x,y)=(y+1)C(k)−L(k)+S(k)−yD(k).F(x,y)=(y+1)C(k)-L(k)+S(k)-yD(k).F(x,y)=(y+1)C(k)−L(k)+

The correction is zero at fl=yf_l=yfl​=y; the reference uses strict inequality for insertion.

Subtasks and complexity For subtask 1, enumerate intervals and simulate increments while finishing elements left to right. Reject immediately if an increment count exceeds the number of earlier elements. This takes O(N5+Q)O(N^5+Q)O(N5+Q) time and O(N2)O(N^2)O(N2) space, including a two-dimensional prefix table for queries. For subtask 2, fix and extend . Maintain the earlier final values as disjoint consecutive integer runs in an ordered map. Check whether one run contains in , then insert and merge neighboring runs. Build a two-dimensional prefix table of valid intervals. Time is and space is . The full solution takes time and space. Answers are at most . Use signed 64-bit integers for counts, prefix sums, products, and inclusion-exclusion.

Solution written by GPT6

v
−1-1−1
Ai<BiA_i<B_iAi​<Bi​
i​
lasti​
(
v
)
.
i
​
[l,r][l,r][l,r]
ti≥lt_i\ge lti​≥l
i∈[l,r]i\in[l,r]i∈[l,r]
BBB
[Ai,Bi)[A_i,B_i)[Ai​,Bi​)
Bi−AiB_i-A_iBi​−Ai​
ti=−1t_i=-1ti​=−1
tit_iti​
−1-1−1
BiB_iBi​
iii
−1f_l=i-1
fl​=i−1
N−1N-1N−1
lll
l,…,fll,\ldots,f_ll,…,fl​
fl=l−1f_l=l-1fl​=l−1
ttt
lll
O(log⁡N)O(\log N)O(logN)
)
−
F(s1​−
1,e2​)−
F(e1​,s2​−
1)+
F(s1​−
1,s2​−
1).
​
≥
l
min⁡(fl,y)−l+1\min(f_l,y)-l+1min(fl​,y)−l+1
C(k)C(k)C(k)
L(k)L(k)L(k)
yyy
fl<yf_l<yfl​<y
lll
flf_lfl​
kkk
D(k)D(k)D(k)
S(k)S(k)S(k)
S(k)−
yD(k).
ll
l
rrr
[Ar,Br)[A_r,B_r)[Ar​,Br​)
O(log⁡N)O(\log N)O(logN)
BrB_rBr​
O(N2log⁡N+Q)O(N^2\log N+Q)O(N2logN+Q)
O(N2)O(N^2)O(N2)
O((N+Q)log⁡(N+Q))O((N+Q)\log(N+Q))O((N+Q)log(N+Q))
O(N+Q)O(N+Q)O(N+Q)
N(N+1)/2=20,000,100,000N(N+1)/2=20,000,100,000N(N+1)/2=20,000,100,000