#C5967. Participant Ranking Challenge

    ID: 49674 Type: Default 1000ms 256MiB

Participant Ranking Challenge

Participant Ranking Challenge

You are given a list of participants with their unique IDs and scores. Your task is to determine the rank for each queried participant based on their score. The ranking is determined as follows:

  • Participants are sorted in descending order by their scores.
  • If two or more participants have the same score, they receive the same rank, and the ranking for the next distinct score will skip the appropriate number of positions.
  • If scores are equal, sort participants in lexicographical order of their IDs (i.e. ascending alphabetical order) for ranking purpose.

Mathematically, if the sorted scores are \( s_1, s_2, \ldots, s_N \) then the rank \( r_i \) for the \(i\)-th participant in the sorted order is given by:

\[ r_i = \begin{cases} i+1, & \text{if } i=0 \text{ or } s_i \neq s_{i-1}, \\ r_{i-1}, & \text{if } s_i = s_{i-1}. \end{cases} \]

You need to process multiple queries where for each query, you return the ranking of the participant with the given ID.

inputFormat

The input is read from stdin and is formatted as follows:

N Q
ID1 S1
ID2 S2
... 
IDN SN
query1
query2
... 
queryQ

Where:

  • N is the number of participants.
  • Q is the number of queries.
  • Each participant's information consists of an ID (a string without spaces) and an integer S representing the score.
  • Each query is a participant ID for which the rank is requested.

outputFormat

For each query, output the rank of the queried participant on a separate line to stdout.

## sample
5 3
alice 95
bob 100
charlie 90
diana 95
elena 105
alice
charlie
diana
3

5 3

</p>