The History of the Sith Rulers

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 50 rulers with the start and end months of each reign, report which rulers held power during each queried year, in order.
Level

Medium4 of 10

Topics
Sorting, Implementation, Intervals, Simulation
Solved
No attempts yet

Problem

As an archaeologist studying the history of the Sith rulers, you frequently need to look up quickly which ruler held power in a given galactic year. Given a list of Sith rulers and the periods during which each held power, write a program that prints which ruler (or rulers) governed in a specified year.

Note the following:

  • No two rulers ever ruled at the same time.
  • Some years and months had no ruler at all, as the empire fell into disarray.
  • Some rulers led more than once, but there was always a break in their rule before they reassumed power (someone else ruled before they came back to power).

Input

The first line of the input contains an integer nn (0<n≤500 < n \le 50), the number of Sith rulers. It is followed by nn entries, one per ruler.

The first line of each entry contains the name of the ruler, which is at most 30 characters long. The second line of the entry contains the galactic date on which the ruler assumed power, then a single space, then the galactic date on which the ruler left power.

Both dates are real numbers greater than 00 and less than 50005000, written with exactly one digit after the decimal point. The second date is always greater than the first. The single digit to the right of the decimal point represents a galactic month, numbered starting from 00.

For the date on which a ruler leaves power, the ruler is assumed to serve through the end of the listed month. More precisely, no ruler ever assumes power in the same month the previous ruler leaves it. The minimum time any ruler serves is one month (so a ruler may serve from 1.11.1 to 1.11.1, and any ruler who follows must start no earlier than 1.21.2).

After the ruler entries comes an integer cc (0<c≤500 < c \le 50), the number of year queries that follow. Each of the next cc lines contains one galactic year as an integer yy, with 0<y<50000 < y < 5000.

Output

For each galactic year in the input, print the Sith ruler (or rulers) in power during that year, in the format shown in the examples.

Specifically, print Galactic year, a space, the year, a colon, a space, and then the name of the ruler for that year. If there was more than one ruler during the year, each subsequent ruler is preceded by a comma and a space (, ). The rulers must be listed in the order in which they ruled, from earliest to latest within that year. If a ruler ruled more than once in a year, that ruler is listed once for each reign. A newline immediately follows the last ruler listed.

If no ruler is found for a given galactic year, print None after the galactic year designation.

Examples1

  1. Example 1

    Input
    8
    Tulak Hord
    1000.0 1025.9
    Ludo Kresh
    1026.0 1030.0
    Darth Revan
    2000.0 2003.0
    Darth Malak
    2003.1 2009.2
    Exar Kun
    1500.0 1550.0
    Darth Sidious
    2500.0 2500.0
    Darth Nihilus
    2500.1 2500.1
    Darth Sidious
    2500.2 2500.4
    5
    1025
    2003
    1525
    1551
    2500
    
    Expected output
    Galactic year 1025: Tulak Hord
    Galactic year 2003: Darth Revan, Darth Malak
    Galactic year 1525: Exar Kun
    Galactic year 1551: None
    Galactic year 2500: Darth Sidious, Darth Nihilus, Darth Sidious