#P3569. [POI 2014] KAR-Cards

[POI 2014] KAR-Cards

Description

There are nn cards arranged on a table in a fixed order.

Each card has two integers, one on each side: the obverse and the reverse. The ii-th card has xix_i on the obverse and yiy_i on the reverse. Initially, all cards lie with the obverse side up.

Byteasar, the Great Illusionist, wants the sequence of visible numbers on the cards to be non-decreasing. He may flip any subset of cards to show the other side.

However, an audience participant will repeatedly swap two cards on the table. After each such swap, Byteasar may again flip any cards, but he might still be unable to achieve a non-decreasing sequence. After each swap, determine whether it is possible to obtain a non-decreasing sequence of visible numbers by flipping cards as needed.

Input Format

The first line contains a single integer nn (2n200 0002 \le n \le 200\ 000), the number of cards.

The next nn lines describe the cards, in their current order on the table. The ii-th of these lines contains two integers xix_i and yiy_i (0xi,yi1070 \le x_i, y_i \le 10^7), separated by a single space, where xix_i is written on the obverse and yiy_i on the reverse of the ii-th card.

The initial sequence of cards may not allow forming a non-decreasing sequence.

Afterwards, there is a line with a single integer mm (1m1 000 0001 \le m \le 1\ 000\ 000), the number of card swaps. The next mm lines describe the swaps: the jj-th of these lines has two integers aja_j and bjb_j (1aj,bjn1 \le a_j, b_j \le n), indicating that the jj-th participant swaps the aja_j-th and bjb_j-th cards.

Output Format

Print mm lines. On the jj-th line, print TAK (Polish for yes) if, after the jj-th swap, Byteasar can obtain a non-decreasing sequence of visible numbers by flipping any subset of cards; otherwise, print NIE (Polish for no).

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

NIE
TAK

Hint

There are several cards, each with a number on both sides. You may flip any cards. Each operation swaps two cards’ positions. After each operation, determine whether, with the current card order, flipping some cards can form a non-decreasing sequence.

Translated by ChatGPT 5