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

ABAB

Back to problem

Editorial

First, coordinate-compress the values. Only equality between values matters, so this does not change the answer.

For every value xxx, maintain:

  • last⁡[x]\operatorname{last}[x]last[x]: the last position where xxx occurs in the whole sequence;
  • recent⁡[x]\operatorname{recent}[x]recent[x]: the most recent occurrence of xxx before the current position.

Suppose position kkk is being processed and x=Akx=A_kx=Ak​. Let i=recent⁡[x]i=\operatorname{recent}[x]i=recent[x]. The answer is YES if there is a value such that

i<recent⁡[y]<k<last⁡[y].i<\operatorname{recent}[y]<k<\operatorname{last}[y].i<recent[y]<k<last[y].

Indeed, setting j=recent⁡[y]j=\operatorname{recent}[y]j=recent[y] and l=last⁡[y]l=\operatorname{last}[y]l=last[y] gives the required four positions. The strict inequality recent⁡[y]>recent⁡[x]\operatorname{recent}[y]>\operatorname{recent}[x] also guarantees .

Thus, among values that occur again after the current position, we need the largest recent occurrence position.

Whenever value xxx occurs at position ttt and last⁡[x]>t\operatorname{last}[x]>tlast[x]>t, append (t,x)(t,x)(t,x) to a stack. The appended positions are strictly increasing, so the valid pair with the largest position is always at the back of the stack.

Before processing position kkk, repeatedly remove the last pair (t,y)(t,y)(t,y) if either:

  • recent⁡[y]≠t\operatorname{recent}[y]\neq trecent[y]=t, meaning a newer occurrence of yyy has made the pair stale; or
  • last⁡[y]≤k\operatorname{last}[y]\leq klast[y]≤, meaning does not occur after position .

After these removals, the last pair stores the largest recent occurrence among all values that occur again later. Therefore, if xxx has appeared before and the last stored position is greater than recent⁡[x]\operatorname{recent}[x]recent[x], the answer is YES.

To prove completeness, suppose

i<j<k<l,Ai=Ak=x,Aj=Al=yi<j<k<l,\qquad A_i=A_k=x,\qquad A_j=A_l=yi<j<k<l,Ai​=

is a valid answer. Let k′k'k′ be the first occurrence of xxx after jjj, and let i′i'i′ be the previous occurrence of . Then

i′≤i<j<k′≤k<l.i'\leq i<j<k'\leq k<l.i′≤i<j<k′≤k<

When processing k′k'k′, the recent occurrence of yyy is at least jjj, and its last occurrence is at least lll. Hence a valid pair with position greater than i′i' exists in the stack, so the algorithm detects the pattern.

Each pair is pushed and popped at most once. The scan after coordinate compression takes O(N)O(N)O(N) time. The total time complexity is O(Nlog⁡N)O(N\log N)O(NlogN) and the memory complexity is O(N)O(N)O(N).

Solution written by GPT5.6

yyy
recent
[
y
]
>
recent[x]
x≠yx\neq yx=y
k
yyy
kkk
Ak​
=
x,Aj​=
Al​=
y
xx
x
l.
i
′