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-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

Coordinate Compression and Queries

Back to problem

Editorial

Maintain the array AAA explicitly and process the queries in order.

For a type 1 query, copy Al,…,ArA_l,\ldots,A_rAl​,…,Ar​, sort the copy, and remove duplicates. The lower_bound index of each original value in this sorted list is its compressed coordinate. Do not change elements outside the selected range.

For a type 2 query, scan the range to obtain its minimum mmm and maximum MMM. Instead of using floating-point arithmetic, compare ∣2x−(m+M)∣|2x-(m+M)|∣2x−(m+M)∣ for every candidate value xxx. Choose the smallest value among those minimizing this expression. Store array values and intermediate calculations in .

For a type 3 query, add ddd directly to every element in the selected range.

Correctness: The position in the sorted distinct-value list equals the number of distinct smaller values, so type 1 is correct. Type 2 considers every value occurring in the range, compares precisely twice its distance to the required midpoint, and applies the tie rule. Type 3 changes exactly the required elements. Therefore all queries are processed correctly.

Each type 1 query takes O(Nlog⁡N)O(N\log N)O(NlogN) time, while each other query takes O(N)O(N)O(N) time. There are at most 100100100 type 1 queries, giving total time O(NQ+100Nlog⁡N)O(NQ+100N\log N)O and auxiliary space .

long long
(
N
Q
+
100NlogN)
O(N)O(N)O(N)
Solution written by GPT6