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

Tangled Wires

Back to problem

Editorial

View every wire iii as the interval between its two current endpoints.

Reachable arrangements

We use the following lemma.

Initially, wire iii connects [ai,bi][a_i,b_i][ai​,bi​]. Let CCC be a sequence of length 2N2N2N in which every wire number occurs exactly twice. The sequence CCC is reachable if and only if both occurrences of every iii lie inside [ai,bi][a_i,b_i][ai​,bi​].

During one operation, each new endpoint lies between the two previous endpoints of the same wire. Therefore every reachable arrangement satisfies the condition.

The converse is the standard uncrossing lemma. Overlay the current matching and the target matching, pairing edges with the same label. Their union decomposes into even alternating cycles. If the two matchings differ, one alternating cycle contains two crossing current edges whose crossing can be resolved in the required direction while every target edge remains inside the new interval of the same label. This operation strictly decreases the sum of current interval lengths. Repeating this argument and applying induction on that sum reaches the target matching.

The problem is therefore equivalent to the following scheduling problem.

  • Wire iii gives two identical jobs with label iii.
  • Each job must be scheduled at a position from aia_iai​ through .
bib_i
bi​
  • Exactly one job is scheduled at every position.
  • The resulting label sequence must be lexicographically minimum.
  • Which label can be chosen now?

    Construct the answer from left to right, and suppose the current position is ppp. Let ri∈{0,1,2}r_i\in\{0,1,2\}ri​∈{0,1,2} be the number of unscheduled jobs of wire iii.

    For every d≥pd\geq pd≥p, define the slack

    S(d)=d−p+1−∑bi≤dri.S(d)=d-p+1-\sum_{b_i\leq d}r_i.S(d)=d−p+1−bi​≤d∑​ri​.

    Every remaining job whose deadline is at most ddd must fit into positions p,p+1,⋯ ,dp,p+1,\cdots,dp,p+1,⋯,d, so every feasible state satisfies S(d)≥0S(d)\geq0S(d)≥0.

    Suppose that one job of wire iii is scheduled at position ppp, after which the current position becomes p+1p+1p+1. The slack changes as follows.

    • If d<bid<b_id<bi​, then S(d)S(d)S(d) decreases by 111.
    • If d≥bid\geq b_id≥bi​, then S(d)S(d)S(d) does not change.

    Let zzz be the smallest index at least ppp with S(z)=0S(z)=0S(z)=0. Since the number of remaining jobs equals the number of remaining positions, S(2N)=0S(2N)=0S(2N)=0, so zzz always exists.

    Wire iii can be scheduled at the current position while preserving feasibility if and only if both conditions below hold.

    • ai≤pa_i\leq pai​≤p and ri>0r_i>0ri​>0.
    • bi≤zb_i\leq zbi​≤z.

    Necessity of the second condition follows immediately from the slack update. Sufficiency follows from Hall's condition for matching unit jobs to interval positions. After deleting the current position, the only Hall constraints that can become tighter are intervals beginning at the current position, and those constraints are exactly the slack inequalities above.

    Thus, at every position, choose the smallest wire number satisfying these conditions. This gives the lexicographically smallest feasible sequence.

    Data structures

    Use two segment trees.

    The first tree stores all values S(d)S(d)S(d). It supports range addition and finding the first zero at or after ppp. After choosing wire iii, add −1-1−1 to [p,bi−1][p,b_i-1][p,bi​−1].

    The second tree is indexed by deadlines bib_ibi​. If ai≤pa_i\leq pai​≤p and ri>0r_i>0ri​>0, store value iii at index bib_ibi​; otherwise store infinity. The minimum on [p,z][p,z][p,z] is the wire chosen now. After using a wire twice, replace its value by infinity.

    A constant number of segment-tree operations is performed for each position. The time complexity is O(Nlog⁡N)O(N\log N)O(NlogN) and the space complexity is O(N)O(N)O(N).

    Solution written by GPT5.6