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

Interval Diversity

Back to problem

Editorial

Fix the left endpoint lll, and extend the right endpoint rrr from lll to NNN one by one.

Maintain the occurrence counts of values inside the current interval. When ArA_rAr​ is added, we can check whether this value was absent before. If so, the number of distinct integers xxx increases by 111.

The interval length is y=r−l+1y=r-l+1y=r−l+1, so we compute x2/yx^2/yx2/y for every interval and update the maximum.

There are O(N2)O(N^2)O(N2) intervals, and each update can be done in expected O(1)O(1)O(1) time. Therefore, the total time complexity is O(N2)O(N^2)O(N2.

)