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

Modular Range Counting

Back to problem

Editorial

Let B=450B=450B=450. For every x≤Bx\leq Bx≤B and every remainder, store the sorted indices of elements having that remainder. Each element enters BBB lists, so preprocessing time and memory are O(NB)O(NB)O(NB).

For x≤Bx\leq Bx≤B, answer a query with two binary searches in the corresponding list. For x>Bx>Bx>B, since Ai≤BA_i\leq BAi​≤B, at most one value can have remainder ; use the list for that exact value.

Each query takes O(log⁡N)O(\log N)O(logN) time.

Solution written by GPT5

yyy