#K39037. Knight Combat Queries
Knight Combat Queries
Knight Combat Queries
You are given an initial set of knights and a series of queries that simulate knight combat operations. Each knight is identified by a unique integer and has a corresponding skill score. The queries can either add a new knight or ask how many knights have a strictly higher skill score than a given knight.
The problem is defined as follows:
- The first line of the input contains two integers \(m\) and \(q\): the initial number of knights and the number of queries, respectively.
- The next \(m\) lines each contain two integers: the knight's identifier and their skill score.
- The following \(q\) lines each describe a query in one of the following formats:
Add k s
: Add a new knight with identifier \(k\) and skill score \(s\) to the system.Query k
: Query the number of knights whose skill score is strictly greater than that of the knight with identifier \(k\).
For each Query
instruction, print the result on a new line.
Note: All arithmetic comparisons, insertions, and queries should be performed efficiently. The algorithm uses binary search on a maintained sorted list of skills, and when adding new knights, the skills are inserted using a binary search based insertion method. The formula used for the count is:
[ \text{count} = |\text{sorted_skills}| - \text{upper_bound}(\text{sorted_skills}, s) ]
where \(s\) is the queried knight's skill score.
inputFormat
The first line contains two space-separated integers \(m\) and \(q\). The next \(m\) lines contain two integers each, representing a knight's identifier and their skill score. This is followed by \(q\) lines, each containing a query in one of the formats: Add k s
or Query k
.
outputFormat
For each Query
command, output a single line containing an integer — the number of knights whose skill score is strictly greater than the queried knight's.
5 4
1 150
2 200
3 50
4 175
5 100
Query 1
Add 6 220
Query 2
Query 6
2
1
0
</p>