Monster Fighting

면접 대비

시간 제한2초메모리 제한256 MB

요약
각각 타입과 전투력을 가진 아군 몬스터 N마리와 적 몬스터 N마리가 주어질 때, 전투력이 상대 이상이거나 같은 타입이면서 절반 이상이면 이기는 조건으로 완전 매칭이 존재하는지 판정한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 투 포인터
정답자
아직 제출이 없습니다

문제

Busy Beaver is playing his favorite adventure game, where he battles wild monsters with monsters of his own!

In this game, there are two types of monsters: light and dark. Each monster has exactly one type, and some positive integer power level. A monster with power pp can defeat another monster qq if either p≥qp \geq q, or if they have the same type and 2p≥q2p \geq q.

Busy Beaver has just encountered NN monsters, and he also has NN monsters in his party. To defend against the encounter, he needs to pair his monsters with the opposing monsters such that in each pair, Busy Beaver's monster can defeat the opposing monster according to the rules above. Given the types and power levels of each of the monsters, please report whether or not such a pairing exists.

입력

Each test contains multiple test cases. The first line of input contains a single integer TT (1≤T≤105)(1 \leq T \leq 10^5), the number of test cases. The description of each test case follows.

The first line of each test case contains a single positive integer NN (1≤N≤2⋅1051 \leq N \leq 2 \cdot 10^5).

Then, NN lines of input follow, describing Busy Beaver's monsters. The iith line contains two positive integers p_i,s_ip\_i, s\_i (1≤p_i≤109,s_i∈0,11 \leq p\_i \leq 10^9, s\_i \in \\{0, 1\\}) --- the power level p_ip\_i and type s_is\_i of the iith monster. s_i=0s\_i = 0 denotes a light-type monster, and s_i=1s\_i = 1 denotes a dark-type monster.

After that, there are NN more lines of input, describing the encountered monsters. The iith line contains two positive integers q_i,t_iq\_i, t\_i (1≤q_i≤109,t_i∈0,11 \leq q\_i \leq 10^9, t\_i \in \\{0, 1\\}) --- the power level q_iq\_i and type t_it\_i of the iith monster. t_i=0t\_i = 0 denotes a light-type monster, and t_i=1t\_i = 1 denotes a dark-type monster.

It is guaranteed that the sum of NN across all test cases is no more than 2⋅1052 \cdot 10^5.

출력

For each test case, output "YES" (without quotes) if a desired pairing exists, and "NO" (without quotes) otherwise.

힌트

In the sample input, there are two test cases. For the first case, we can construct a pairing as follows:

  • Your light type monster of power level 22 can defeat the identical opposing monster.
  • Your light type monster of power level 33 can defeat the identical opposing monster.
  • Your first dark type monster of power level 11 can defeat the opposing dark type monster of power level 22.
  • Your second dark type monster of power level 11 can defeat the opposing light type monster of power level 11.

In the second case, it can be shown that no pairing exists.

예제1

  1. 예제 1

    입력
    2
    4
    2 0
    3 0
    1 1
    1 1
    1 0
    2 0
    3 0
    2 1
    2
    1 1
    5 1
    1 0
    1000000000 0
    
    예상 출력
    YES
    NO