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

Ladders and Queries

Back to problem

Editorial

Write sxs_xsx​ for a rung between bars xxx and x+1x+1x+1. First place the following rungs in increasing height order:

s1,s2,⋯ ,sN−1,sN−1,⋯ ,s2,s1.s_1,s_2,\cdots,s_{N-1},s_{N-1},\cdots,s_2,s_1.s1​,s2​,⋯,sN−1​,s

The second half reverses the first, so every starting bar initially leads to itself. This uses 2N−22N-22N−2 type-111 operations.

For a target (a,b)(a,b)(a,b) with a=ba=ba=b, simply print 2 a2\ a2 a. Otherwise, if a>1a>1a>1, erase at height in the first half. If , erase at height in the second half. Print , then restore the erased rungs.

Erasing sa−1s_{a-1}sa−1​ in the first half moves the path starting from aaa onto the former path from bar 111. Erasing sb−1s_{b-1}s in the second half leads that path to bar . Hence, the query returns . Restoring the rungs returns to the identity ladder for the next target.

The initial setup uses 2N−22N-22N−2 type-111 operations, and each target uses at most four more. The total is at most 6N−26N-26N−2, within the limit. The output length and running time are O(N)O(N)O(N).

Proof

A rung sxs_xsx​ swaps the two paths passing through bars xxx and x+1x+1x+1 at that height. The initial rung sequence consists of a sequence followed by its inverse, so its overall mapping is the identity.

Removing sa−1s_{a-1} from the first half swaps the paths from bars and in that identity mapping. Removing from the second half swaps the destination bars and . With both removals, the path from starting bar first follows the former path from bar and then reaches destination bar . If or , the corresponding removal is unnecessary.

In subtask 1, output only the queries. In subtask 2, toggle the single rung between the two bars before and after each needed query. In subtask 3, temporarily erase a rung only in the second half. The full solution uses both halves.

Solution written by GPT5

N
−
1
​
,
⋯
,
s2​
,
s1​
.
sa−1s_{a-1}
sa−1​
a−1a-1a−1
b>1b>1b>1
sb−1s_{b-1}sb−1​
2N−b2N-b2N−b
2 a2\ a2 a
b−1
​
bbb
bbb
sa−1​
111
aaa
sb−1s_{b-1}sb−1​
111
bbb
aaa
111
bbb
a=1a=1a=1
b=1b=1b=1

After each query, all removed rungs are restored. Thus, every query begins with the identity ladder and independently reaches its requested destination.