Three Competitions
Time limit5sMemory limit1024 MB
Each person has ranks in three competitions; a directly wins against b if a beats b in at least two, and queries ask whether a reaches b through such direct wins.
- Level
Hard8 of 10
- Topics
- Graph, Sorting, DFS, Two pointers
- Solved
- No attempts yet
Problem
Last month, people participated in three competitions. The people are labeled with distinct integers from to . In each competition, the people were sorted by performance and got ranked from to . The lower the rank, the better the player. There were no ties in any of the rankings.
Today, instead of people participating at once, two people compete head-to-head. The winner of the match is the person who wins at least two out of three competitions. The winner then proceeds to compete with another person. This turned out to be quite interesting: even if a person cannot directly win against another person , it's possible that another person wins against and then wins against . That way, we can say that "indirectly" wins against . It's also possible that two people can indirectly win against each other!
Formally, a person is said to directly win against another person if has a lower rank than in at least two competitions. Also, is said to indirectly win against if there exists a sequence of people () such that directly wins against for all , and .
Given the ranks of the people in each competition, answer questions asking whether person indirectly wins against another person .
Input
The first line contains a single integer , the number of people.
Each of the next lines contains three integers, which represent the ranks of each person in each of the three competitions, in order from person to person . For each competition, each integer rank from to appears exactly once.
The next line contains a single integer , the number of questions.
Each of the next lines contains two integers and , , asking whether person indirectly wins against person .
Output
Output lines. The -th line should be either \verb YES \ or \verb NO . If person indirectly wins against person , output \verb YES , otherwise output \verb NO .
Hint
Person 1 directly (and indirectly) wins against 2. Person 2 doesn't directly win against 1, but 2 directly wins against 3 and 3 directly wins against 1, so 2 indirectly wins against 1.