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

Three Permutations and Queries

Back to problem

Editorial

For two values x,yx,yx,y, orient an edge x→yx \to yx→y if xxx appears before yyy in at least two of the three permutations. Exactly one direction is chosen for every pair, so this is a tournament. A good permutation exists exactly when there is a linear order consistent with every edge of this tournament, which is equivalent to the tournament being transitive.

For every value xxx, define

d[x]=#{y:y appears before x in at least two of the three permutations}.d[x]=\#\{y:\text{$y$ appears before $x$ in at least two of the three permutations}\}.d[x]=#{y:y appears before x in at least two of the three permutations}.

Thus, d[x]d[x]d[x] is the indegree of xxx in the tournament.

In a transitive tournament, the indegrees are exactly 0,1,⋯ ,N−10,1,\cdots,N-10,1,⋯,N−1. Conversely, if all d[x]d[x]d[x] are distinct, then because every indegree lies between 000 and N−1N-1, the set of indegrees is exactly . The vertex of indegree is before every other vertex. Removing it leaves a tournament whose indegrees are again , so induction shows that the tournament is transitive. Therefore, a good permutation exists if and only if all values are distinct.

It remains to compute the initial d[x]d[x]d[x] efficiently. Let pos⁡u[x]\operatorname{pos}_u[x]posu​[x] be the position of xxx in permutation uuu. For two permutations , define

Cuv[x]=#{y:pos⁡u[y]<pos⁡u[x], pos⁡v[y]<pos⁡v[x]},C_{uv}[x]=\#\{y:\operatorname{pos}_u[y]<\operatorname{pos}_u[x],\ \operatorname{pos}_v[y]<\operatorname{pos}_v[x]\},Cuv​[x]=#{y

and let Cpqr[x]C_{pqr}[x]Cpqr​[x] be the number of values that appear before xxx in all three permutations. If a fixed yyy appears before xxx in exactly of the three permutations, it contributes to and contributes once to only when . Hence

d[x]=Cpq[x]+Cpr[x]+Cqr[x]−2Cpqr[x].d[x]=C_{pq}[x]+C_{pr}[x]+C_{qr}[x]-2C_{pqr}[x].d[x]=Cpq​[x]+C

Each CuvC_{uv}Cuv​ can be computed in O(Nlog⁡N)O(N\log N)O(NlogN) by scanning one permutation and inserting positions in the other permutation into a Fenwick tree. Computing CpqrC_{pqr}C is a three-dimensional dominance counting problem on the three positions. Sort by the position in , then use CDQ divide and conquer with a Fenwick tree to obtain all values in .

Now consider a query. Suppose the two adjacent values in the selected permutation are x,yx,yx,y, with xxx before yyy before the swap. Swapping adjacent elements changes the relative order of only the pair (x,y)(x,y)(x,y) in that permutation. Therefore, only the tournament edge between xx and can possibly change.

If the other two permutations agree on the order of x,yx,yx,y, the majority result is already fixed and nothing changes. If the other two permutations disagree, then the edge is x→yx\to yx→y before the swap and becomes y→xy\to xy→x after the swap. Only in this case, update

d[x]+=1,d[y]−=1.d[x]\mathrel{+}=1,\qquad d[y]\mathrel{-}=1.d[x]+=1,d[y]−=1.

Maintain the frequency of each possible indegree 0,1,⋯ ,N−10,1,\cdots,N-10,1,⋯,N−1, together with the number of indegree values whose frequency is at least 222. This number is 000 exactly when all d[x]d[x]d[ are distinct. At most two indegrees change in one query, so this maintenance takes time. Also maintain the current position arrays of the three permutations, allowing the order comparison in the other two permutations to be done in .

The initialization takes O(Nlog⁡2N)O(N\log^2 N)O(Nlog2N) time, every query takes O(1)O(1)O(1) time, and the memory usage is O(N)O(N)O(N). Thus the total time complexity is .

Solution written by GPT5.6

N−
1
{0,1,⋯ ,N−1}\{0,1,\cdots,N-1\}{0,1,⋯,N−1}
000
0,1,⋯ ,N−20,1,\cdots,N-20,1,⋯,N−2
d[x]d[x]d[x]
u,vu,v
u,v
:
posu​[y]<
posu​[x], posv​[y]<
posv​[x]},
kk
k
(k2)\binom{k}{2}(2k​)
Cpq+Cpr+CqrC_{pq}+C_{pr}+C_{qr}Cpq​+Cpr​+Cqr​
CpqrC_{pqr}Cpqr​
k=3k=3k=3
pr​
[
x
]
+
Cqr​[x]−
2Cpqr​[x].
pqr​
ppp
O(Nlog⁡2N)O(N\log^2 N)O(Nlog2N)
x
yyy
x
]
O(1)O(1)O(1)
O(1)O(1)O(1)
O(Nlog⁡2N+M)O(N\log^2 N+M)
O(Nlog2N+M)