Weights (En)
Weights (En)
Thus, the organizers initially assumed that the N teams have the same relative strength, which starts at
1. Then, as the tournament progresses, there will be Q events, given in chronological order, that are of
one of two types:
• 1 a b: Two teams with relative strengths a and b play against each other. The winner will have
a relative strength equal to a + b from now on, while the loser is eliminated from the tournament
(thus, one less team remains in the tournament). It is guaranteed that there exists at least one
team with strength a and at least one team with strength b (if a = b, then there exist at least two
teams with that strength level).
• 2 k: The Krkvan Football Federation wants to compute all pairwise differences in relative strength
among the remaining teams and find the k th smallest such difference, where k is between 1 and the
total number of such pairs (initially, this number would be at most N ·(N2 −1) ).
As an example, if the relative strengths of the teams are [4, 3, 1, 1], then the pairwise differences are
[|4 − 3|, |4 − 1|, |4 − 1|, |3 − 1|, |3 − 1|, |1 − 1|] = [1, 3, 3, 2, 2, 0], or [0, 1, 2, 2, 3, 3] when sorted in increasing
order. If k is 4, then the answer is 2.
The tournament is coming soon, so Krkva calls for your help to find these answers, or otherwise, the
teams will fail to win any silverware for another year.
☞ Among the attachments of this task you may find a template file weights.* with a sample
incomplete implementation.
weights Page 1 of 3
Input
The first line of the input contains N and Q, the number of teams and the number of events.
The next Q lines contain the description of the events, as mentioned in the statement.
Output
The output shall contain one line for each event of type 2, representing the k th smallest difference in that
respective event.
Constraints
• 1 ≤ N, Q ≤ 300 000.
x·(x−1)
• For operations of type 2, 1 ≤ K ≤ 2 , where x is the number of teams still in the event at the
given moment.
Scoring
Your program will be tested against several test cases grouped in subtasks. In order to obtain the score
of a subtask, your program needs to correctly solve all of its test cases.
– Subtask 1 (0 points) Examples.
– Subtask 2 (9 points) 1 ≤ N, Q ≤ 100.
– Subtask 3 (14 points) 1 ≤ N, Q ≤ 1000.
– Subtask 4 (12 points) All events of type 1 take place before all events of type 2 and there will
be at most 500 teams remaining after performing the events of type 1.
– Subtask 5 (10 points) All events of type 1 take place before all events of type 2.
– Subtask 6 (21 points) 1 ≤ N, Q ≤ 50 000.
– Subtask 7 (34 points) No additional limitations.
weights Page 2 of 3
Examples
input output
10 15 0
2 45 0
1 1 1 1
1 1 1 0
1 1 1 1
2 9 2
2 10 1
1 1 2 2
2 1 3
2 5
2 13
1 1 1
1 2 2
2 1
2 4
2 6
weights Page 3 of 3