#P9757. Cyclic Sorted Circle
Cyclic Sorted Circle
Cyclic Sorted Circle
In the closing ceremony of an informatics winter camp, n students (numbered from \(1\) to \(n\)) stand in a circle holding hands with their two neighbors. Initially, the students form a circle. Alenka wonders if by removing exactly one pair of adjacent hand-holds (i.e. by making one cut in the circle) one can obtain a linear sequence of students whose numbers are in increasing order \(1,2,\dots,n\).
For example, if the circle order is 3 4 1 2
, then by cutting between the students numbered \(4\) and \(1\) the resulting linear sequence becomes \(1,2,3,4\); however, if the order is 2 1 4 3
, then no valid cut exists.
That same evening, Kreso issues \(q\) commands. In each command, he instructs two students to swap positions. After each swap, you are to determine whether it is possible to achieve a sorted linear sequence \(1,2,\dots,n\) by cutting the circle at exactly one adjacent pair.
Note: The circle is considered cyclic, that is, the last student is adjacent to the first.
Observation: A necessary and sufficient condition for the circle to be "cuttable" into the ordered sequence is that the current circular arrangement is a cyclic shift of \(1,2,\dots,n\). Equivalently, if you consider each adjacent pair \(a[i]\) and \(a[(i+1)\bmod n]\), it must hold that either \(a[(i+1)\bmod n]=a[i]+1\) or \(a[i]=n\) and \(a[(i+1)\bmod n]=1\).
inputFormat
The first line contains two integers \(n\) and \(q\) \((1 \le n, q \le 10^5)\), representing the number of students and the number of swap commands.
The second line contains \(n\) distinct integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le n)\) denoting the initial circular arrangement of student numbers in clockwise order.
Each of the next \(q\) lines contains two integers \(i\) and \(j\) \((1 \le i, j \le n)\) indicating that the students in positions \(i\) and \(j\) (1-indexed) will swap places.
outputFormat
For each swap command, output a line containing YES
if, after the swap, it is possible to perform exactly one cut (i.e. remove a single pair of adjacent hand-holds) such that the resulting linear sequence is in increasing order \(1,2,\dots,n\); otherwise, output NO
.
sample
4 4
3 4 1 2
1 3
1 2
2 4
1 4
NO
NO
NO
YES
</p>