dirigent — COCI CONTEST_#3
Task type: batch | Time limit: 1 s | Memory limit: 512 MB | Difficulty: 13
Algorithms: data structures, simulation, modular arithmetic, combinatorics

# Task Dirigent

Winter school of informatics ends with a traditional dance. There are *n* students who participate. Each of them has a unique label between 1 and *n*.

First, conductor Krešo orders the students to form a circle such that each student holds hands with two other students.

Alenka is wondering if it is possible to break the circle by making exactly one pair of neighbouring students stop holding hands and that the newly formed sequence of students is sorted by their labels. For example, if their order is 3 4 1 2, than
the circle can be broken between students with labels 4 and 1, but if their order is 2 1 4 3, than there is no way to break the circle in such way.

During the night Krešo is going to give *q* instructions. In each of them, he is going to order two students to swap places. After each swap you need to help Alenka answer her question.

## Input

The first line contains two integers *n* and *q* (1 ≤ n, q ≤ 300 000), the number of students and the number of swaps.

The second line contains *n* integers a_i (1 ≤ a_i ≤ *n*), describing the initial placement of students in the circle.

In each of the next *q* lines there are two integers x_i, y_i (1 ≤ x_i, y_i ≤ *n*, x_i \neq y_i), that describe Krešo's *i*-th instruction in which students with labels *xi* and *yi* swap places.

## Output

In the *i*-th of the *q* lines output the answer to Alenka's question after *i* swaps have been carried out. If the answer is affirmative output DA, otherwise NE.

## Scoring

| Subtask | Points | Constraints |
|---------|--------|-------------|
| 1       | 15     | $n, q \leq 500$ |
| 2       | 20     | $n, q \leq 5000$ |
| 3       | 35     | No additional constraints. |

![](_page_0_Picture_16.jpeg)

![](_page_1_Picture_0.jpeg)

## Examples

| input | input | input |
|---|---|---|
| 5 2\n2 3 4 5 1\n1 3\n3 1 | 4 2\n2 3 1 4\n4 2\n3 4 | 6 5\n2 1 5 6 3 4\n3 1\n3 4\n3 2\n4 5\n5 4 |
| output\nNE\nDA | output\nNE\nDA | output\nNE\nNE\nDA\nNE\nDA |

## Clarification of the second example:

Students in the beginning, after the first and after the second swap.

![](_page_1_Figure_7.jpeg)

Starter code: