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

스케이트 연습과 쿼리

Back to problem

Editorial

연습 구간이 [L,R][L,R][L,R]라고 하자. R+1R+1R+1번 지점에서는 속력이 000이어야 하므로, 오른쪽에서 왼쪽으로 최적 속력을 정하면 된다. sR+1=0s_{R+1}=0sR+1​=0으로 두면

si=min⁡(Vi,si+1+1)s_i=\min(V_i,s_{i+1}+1)si​=min(Vi​,si+

이 항상 최적이다. 속력은 왼쪽에서 오른쪽으로 증가할 때 제한이 없으므로, 각 위치에서는 오른쪽으로 무사히 내려갈 수 있는 최대 속력을 잡으면 된다.

이제 오른쪽 끝 RRR을 111부터 NNN까지 증가시키며 모든 질의를 오프라인으로 처리한다. 현재 오른쪽 끝이 RRR일 때, si=Vis_i=V_isi​인 가장 오른쪽 위치를 라고 하자. 보다 오른쪽의 모든 위치는 속도 제한에 걸리지 않고 도착점 때문에만 제한되므로, 에 대해 이다.

오른쪽 끝을 R−1R-1R−1에서 RRR로 늘릴 때, 이전 last를 기준으로 다음 변화가 생긴다.

  • L>lastL>\text{last}L>last인 시작점의 답은 R−L+1R-L+1R−L+1만큼 증가한다.
  • L≤lastL\le \text{last}L≤last인 시작점의 답은 R−만큼 증가한다.

또한 새로 속도 제한에 걸리는 위치는 정확히 i+Vi=R+1i+V_i=R+1i+Vi​=R+1을 만족하는 위치들이다. 따라서 각 값 i+Vii+V_ii+V에 대해 가장 큰 만 저장해 두면, 는 전체 과정에서 단조 증가한다.

각 시작점 LLL에 대해 last가 처음으로 LLL 이상이 되는 시각을 BLB_LBL​이라고 하자. last가 한 번 증가할 때 지나가는 LLL들에 대해 BLB_L을 채우면 전체 이다.

또 PR=∑t=1R(t−lastt−1)P_R=\sum_{t=1}^{R}(t-\text{last}_{t-1})PR​=∑t=1R​(t−를 전처리한다. 그러면 질의 의 답은 다음과 같다.

  • R≤BLR\le B_LR≤BL​이면 답은 1+2+⋯+(R−L+1)1+2+\cdots+(R-L+1)1+2+⋯+(R이다.

따라서 전체 시간 복잡도는 O(N+Q)O(N+Q)O(N+Q), 메모리 복잡도는 O(N+Q)O(N+Q)O(N+Q)이다.

1
​
+
1)
=
Vi​
last
last
i>lasti>\text{last}i>last
si=R+1−is_i=R+1-isi​=R+1−i
lastR-\text{last}
R−last
i​
iii
last
B
L​
O(N)O(N)O(N)
lastt−1​)
[L,R][L,R][L,R]
−
L+
1)
  • R>BLR>B_LR>BL​이면 답은 1+2+⋯+(BL−L+1)+PR−PBL1+2+\cdots+(B_L-L+1)+P_R-P_{B_L}1+2+⋯+(BL​−L+1)+PR​−PBL​​이다.