In Bruce's class, there are
Bruce finds that some students are friends. Note that friendship is a bidirectional relationship. For example, if Alex is Tim's friend, then conversely, Tim is also Alex's friend. Students are grouped together through friendship. Now, Bruce wants to motivate his students to solve more questions on DMOJ. He will perform a sequence of operations, each of which is one of the following:
, build a friendship between student and student ; , query the student number who has the -th lowest rank in the group that contains student (note that the group of student includes student himself).
Input Specification
The first line of input will consist of two integers
The second line of input is a permutation of
The next
The next line of input is a single integer
Output Specification
For each query operation, i.e. -1
.
Sample Input
5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
Sample Output
-1
2
5
1
2
Comments
what is the best solution for this problem? i can only think of
. is there a better one?
hahhahahh, i modified from reading with cin to reading with scanf and my execution time reduced 10 times
Am i on the right track to the accepted solution? Thanks
About halfway. Your
.
merge
function isI've been having a tough time finding a sublinear merge