Ranks in groups
InterviewTime limit5sMemory limit256 MB
Groups of students merge together and each query asks for the score rank of one student inside its current group.
- Level
Medium6 of 10
- Topics
- Union-find, Sorting, Binary search
- Solved
- No attempts yet
Problem
There are students. For each with , student scores points on the exam. The students are split into groups, and at the start group holds only student .
Write a program that supports the following two operations.
- Group merge: given two group numbers and , move every student of group into group . After the merge, group no longer exists.
- Query: given a student number , find the rank of student inside the group that holds student . Within a group, the student with the highest score has rank 1, the student with the second highest score has rank 2, and so on.
Each test case contains operations.
Input
The first line contains an integer (), the number of test cases. Each test case follows in the format below.
The first line of a test case contains two integers and (, ).
Each of the next lines describes one operation. The line starts with an integer , the type of the operation.
- If , two more integers and follow on the same line. Merge the students of group into group .
- If , one more integer follows. Print the rank of student inside the group that holds student .
Both groups given in a merge operation still exist at that moment, and and are different.
Output
For each query operation, print the rank of that student on its own line. Print the answers of all test cases in the order the queries appear.