P14463 [CodeChef] BREAK
提高+
题目描述

Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Chef has to cook many dishes today. In between cooking two of them, he decided to relax and play a card game with his friend.

The game is played with cards that have integer ranks written on them. There may be multiple cards with the same rank. At the beginning of the game, Chef gets $N$ cards with ranks $a_{1}, a_{2}, \ldots, a_{N}$ and his friend gets $N$ cards with ranks $b_{1}, b_{2}, \ldots, b_{N}$.

The game is played in turns. The players alternate turns and in each turn, one player is the attacker and the other is the defender. In the first turn, Chef is the attacker. The roles in each subsequent turn depend on the result of the previous turn.

In each turn, the attacker chooses one of his cards and puts it on the table. The defender must either give up this turn or beat this card by choosing one of his cards with a strictly higher rank and putting it on the table. Then, the attacker must give up the turn or choose another of his cards such that its rank is equal to the rank of any card on the table, then the defender must give up or beat this card, and so on until one player gives up the turn. If both players still have cards afterwards, the game proceeds to the next turn with the following rules:
If the attacker gave up, all cards on the table are discarded. In the next turn, the roles of the players are swapped: the attacker becomes the defender and vice versa.
If the defender gave up, he takes all cards on the table, which he can use in the subsequent turns just like the remaining cards which he did not put on the table. In the next turn, the roles are left unchanged.

The game ends when at least one player does not have any cards at the start of some turn. If the attacker gave up the last turn and both players do not have any cards, a draw is announced. Otherwise, the player that ran out of cards wins.

Chef and his friend want to end the game with a draw. Determine if they can do that.

Note that there are two subtasks and your goals in them are different: in the first subtask, Chef does not have much time, so the goal is to get a draw in one turn, while in the second subtask, it is sufficient to get a draw in any number of turns.

------ Input ------
The first line of the input contains two space-separated integers $T$ and $S$ denoting the number of test cases and the subtask. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$.
The third line contains $N$ space-separated integers $b_{1}, b_{2}, \ldots, b_{N}$.

------ Output ------
For each test case, print a single line containing the string "YES" if it is possible to reach the given goal or "NO" otherwise (without quotes).

------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$S \in \{1, 2\}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ a_{i}, b_{i} ≤ 10^{9}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{5}$

------ Subtasks ------
Subtask #1 (50 points):
$S=1$
the goal is to end the game with a draw in one turn

Subtask #2 (50 points):
$S=2$
the goal is to end the game with a draw in any number of turns

------ Example Input 1 ------

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

------ Example Output 1 ------

YES
NO
NO
NO

------ Example Input 2 ------

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

------ Example Output 2 ------

YES
YES
YES
NO

------ Explanation ------
The only difference between the two examples is that in the first one, $S=1$, while in the second one, $S=2$.

Example case 1: It is possible to reach a draw in one turn if Chef starts by choosing a card with rank $1$, his friend beats it with a card with rank $2$, then Chef chooses a card with rank $2$, his friend beats it with a card with rank $3$, and finally Chef chooses his last card with rank $1$ and his friend beats it with his last card with rank $3$. All cards are discarded afterwards.

Example case 2: At least two turns are necessary to reach a draw. In the first turn, Chef can choose a card with rank $1$, his friend can beat it with a card with rank $3$, then Chef can choose a card with rank $3$ and his friend can beat it with a card with rank $5$. Then, Chef gives up and all these cards are discarded. In the next turn, Chef's friend is the attacker, he chooses the last card with rank $2$ and Chef can beat it with his last card with rank $4$.

Example case 3: The game can end in a draw in five turns as follows:
Chef chooses a card with rank $5$ and his friend gives up and takes it.
Chef chooses a card with rank $4$ and his friend beats it with a card with rank $5$. Chef gives up and both cards are discarded.
Chef's friend chooses a card with rank $1$, Chef takes it.
Chef's friend chooses a card with rank $2$, Chef beats it with a card with rank $6$.
Chef chooses a card with rank $1$ and his friend beats it with a card with rank $3$.

Example case 4: It is impossible to reach a draw at all.

输入格式

见题面

输出格式

见题面

样例输入
4 1 3 2 1 1 3 2 3 3 4 1 3 3 5 2 3 5 4 6 1 3 2 2 1 1 1 1
样例输出
YES NO NO NO