After each swap in a permutation A, decide whether the permutation graph (edges between crossing chords) is scorpion-like.
Hard8GraphSortingImplementationMathNo attempts yetTime limit1sMemory limit256 MBThink about the graphs used in computer science, and in particular about undirected graphs, whose edges carry no direction. Some undirected graphs have a special property.
| Connected graph | Forest | Tree | Scorpion graph |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
This problem is about scorpion graphs. A graph is scorpion-like when it satisfies the following.

The picture above is an example of a scorpion graph.
Computer scientists found this property and gave it a name because the cost of testing it is unusual. Given an N×N adjacency matrix, deciding a nontrivial graph property (connected graph, forest, tree, and so on) normally takes O(N2) time on top of reading the input. Scorpion-ness is different. No matter how many edges the graph has, an algorithm decides it in O(N) time on top of reading the input.
Jaehyun admired that property, so he hands you another unusual graph, the permutation graph, and asks whether it is scorpion-like. The permutation graph of a permutation A1,A2,…,AN of length N is defined as follows.

The picture above is the permutation graph of A=[2,5,4,1,3].
Jaehyun wanted a harder problem, so he added Q operations that swap two elements of the permutation. Decide, after each swap, whether the permutation graph of the current A is a scorpion graph. A swap is not temporary, and it stays in effect for the operations that follow.
The first line contains the length of the permutation, N (4≤N≤100000).
The second line contains N distinct positive integers A1,A2,…,AN (1≤Ai≤N) that form the permutation A.
The third line contains the number of swap operations, Q (1≤Q≤100000).
Each of the next Q lines describes one swap operation. A line holds two positive integers x and y (1≤x,y≤N, x=y) separated by a space. Swap the x-th element Ax and the y-th element Ay of the permutation A first, then decide whether the permutation graph of the resulting A is a scorpion graph and print the answer.
For each swap operation, print YES if the permutation graph of A after the swap is a scorpion graph, and NO otherwise, one answer per line.