QOJ.ac

QOJ

时间限制: 4 s 内存限制: 512 MB 总分: 100

#5409. Perotation

统计

For a sequence $a_1, a_2, \cdots, a_n$ with distinct elements, we define a valid operation as follows:

  • Select an index $i \in [2, n-1]$ such that $a_{i-1} < a_i < a_{i+1}$.
  • Move $a_{i+1}$ to the position before $a_{i-1}$. That is, if the values at positions $i-1, i, i+1$ in the original sequence are $a_{i-1}, a_i, a_{i+1}$, then in the new sequence, the values at positions $i-1, i, i+1$ become $a_{i+1}, a_{i-1}, a_i$.

For example, if $a=[1, 2, 3, 4, 5]$ and we choose $i=3$, the sequence becomes $[1, 4, 2, 3, 5]$.

A sequence is defined as valid if and only if it can be obtained from a monotonically increasing sequence through a finite number of operations.

Given a permutation $a_1, a_2, \cdots, a_n$ of length $n$, there are $q$ modifications, where each modification consists of swapping the values of $a_x$ and $a_y$. After each modification, you need to find the smallest positive integer $k$ such that $a_k, a_{k+1}, \cdots, a_n$ is a valid sequence. It is easy to see that any sequence of length $1$ is valid, so the smallest positive integer $k$ always exists and is unique.

Input

The first line contains two integers $n$ and $q$.

The second line contains $n$ integers separated by spaces, representing $a_1, a_2, \cdots, a_n$.

The next $q$ lines each contain two integers $x$ and $y$, describing a modification.

Output

Output $q$ lines, each containing an integer representing the answer.

Examples

Input 1

7 5
6 7 2 1 5 3 4
3 1
3 1
7 5
1 6
6 5

Output 1

3
4
6
7
4

Note 1

After the third operation, the sequence becomes 6 7 2 1 4 3 5.

For a subsequence of length $1$, it is clearly valid, so $a_7$ is a valid sequence.

For a subsequence of length $2$ 3 5, since it is initially ordered, it is clearly valid. Thus $a_6\,a_7$ is a valid sequence.

For a subsequence of length $3$ 4 3 5, since it can only be transformed into 3 4 5 and 5 3 4 from the initial state, it is clearly invalid, so $a_5\,a_6\,a_7$ is an invalid sequence.

Similarly, we can always show that for $k=1, 2, 3, 4$, the sequence $a_k, a_{k+1}, \cdots, a_n$ is invalid.

Therefore, the answer is $6$.

Subtasks

  • Subtask 1 ($10\%$): $n \leq 10$
  • Subtask 2 ($20\%$): $n \leq 100$
  • Subtask 3 ($30\%$): $n \leq 5\,000$
  • Subtask 4 ($40\%$): No special constraints.

For all test data, $2 \leq n \leq 100\,000$ and $1 \leq q \leq 100\,000$.

For each query, $1 \leq x, y \leq n$ and $x \ne y$.

It is guaranteed that $a_i$ is a permutation of $[1, n]$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.