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

Homework Ladder

Back to problem

Editorial

The order of queries and the order in which rungs are crossed are different. Query order determines which rungs exist for each question, while height order determines how those rungs are crossed. Read the entire input first and handle these two orders separately.

Let DDD be the number of type 222 queries, and number them 0,1,…,D−10,1,\ldots,D-10,1,…,D−1 in their input order. If kkk type 222 queries have appeared before a rung is added, that rung exists only for questions with numbers at least kkk. Store each rung as its left line iii, height hhh, and threshold kkk. If D=0D=0D=0, there are no answers to output, so processing of this test case can end.

Think of each question as a ball placed on its starting vertical line. For each line ccc, maintain a set ScS_cSc​ of the question numbers currently on that line. Initially, put the number of each question 2 x2\ x2 x into SxS_xS.

Sort all rungs by increasing height hhh and process them from top to bottom. For a rung (i,h,k)(i,h,k)(i,h,k), exchange only the elements numbered at least kkk between SiS_iSi​ and . Questions numbered below must remain unchanged because this rung had not yet been added when those questions were asked. Rungs at the same height share no vertical line, so they can be processed in any order.

To perform these exchanges efficiently, represent each set by a dynamic segment tree over the coordinate interval [0,D−1][0,D-1][0,D−1]. Create leaves containing question numbers and their ancestors as needed. Every line's tree uses the same subdivision of this interval, so entire subtrees representing the same interval can be exchanged.

To exchange the interval [k,D−1][k,D-1][k,D−1] between two trees, suppose the current pair of nodes represents [l,r][l,r][l,r]:

  • If r<kr<kr<k, nothing in this interval should move, so return.
  • If k≤lk\leq lk≤l, exchange the two node references and return.
  • Otherwise, l<k≤rl<k\leq rl<k≤r. Split both intervals at the same midpoint and recursively exchange the relevant pairs of children.

If both trees are empty, return immediately. If only part of the interval must be exchanged and one node is missing, create an empty node on that side to hold its two children. Exchanging node references does not copy a subtree.

At each depth, at most one interval contains the threshold kkk in its interior. Every other interval is either left unchanged or exchanged as one whole subtree. Thus, processing one rung takes O(log⁡(D+1))O(\log(D+1))O(log(D+1)) time and creates at most that many new nodes.

After processing all rungs, traverse the tree for each line ccc. For every question number ttt found at a leaf of that tree, set ans[t]=c\mathrm{ans}[t]=cans[t]=c. Output these answers in question number order.

Let RRR be the number of rungs, so D+R=QD+R=QD+R=Q. Building the initial trees takes O(Dlog⁡(D+1))O(D\log(D+1))O(Dlog(D+1)) time, sorting the rungs takes , and processing all exchanges takes . Including initialization of the line roots and the final tree traversal, the total time complexity is . Counting all nodes created during initialization and exchanges gives the same bound, , for space.

Proof

Fix a question number ttt and a rung. If kkk questions have appeared before that rung is added, questions with t<kt<kt<k occur before its insertion, and questions with t≥kt\geq kt≥k occur afterward. Hence, this rung exists for question exactly when .

Solution written by GPT6

x
​
Si+1S_{i+1}Si+1​
kkk
O(Rlog⁡(R+1))O(R\log(R+1))O(Rlog(R+1))
O(Rlog⁡(D+1))O(R\log(D+1))O(Rlog(D+1))
O(N+Qlog⁡(Q+1))O(N+Q\log(Q+1))O(N+Qlog(Q+1))
O(N+Qlog⁡(Q+1))O(N+Q\log(Q+1))O(N+Qlog(Q+1))
tt
t
t≥kt\geq kt≥k

While processing rungs in height order, maintain the following invariant: each question number belongs to the set of the line reached in that question's ladder after descending through all heights processed so far. Initially, every question is on its starting line, so the invariant holds.

Consider a rung (i,h,k)(i,h,k)(i,h,k). If t<kt<kt<k, the rung does not exist for question ttt, and its position must stay unchanged. If t≥kt\geq kt≥k, it must cross to the other side exactly when it is currently on line iii or i+1i+1i+1; otherwise, it stays where it is. Exchanging the elements numbered at least kkk between these two sets performs precisely these moves. The segment tree recursion exchanges exactly the leaves in that interval, so it implements the same operation. The invariant is therefore preserved.

Rungs at the same height use disjoint pairs of vertical lines, so their processing order does not affect the result. After all rungs have been processed, each question has passed every rung present in its ladder, from top to bottom. By the invariant, the line whose set contains that question is its actual destination. Consequently, every output answer is correct.