Waiting for a Higher Tick
Alice runs the overnight review of a trading desk. The exchange feed for one day is a log of trades, one per line, many symbols interleaved, in the order the trades happened. For every trade she wants to know: after this trade, how long did it take until the same symbol traded at a strictly higher price?
Formally, let the trades be numbered 1..n in input order. For trade i, find the smallest
j > i such that trade j has the same symbol as trade i and a strictly greater price. The
answer for trade i is t_j − t_i, the difference of their timestamps in milliseconds, or -1
if no such j exists. Note that j is chosen by position in the log, and that two trades can share
a timestamp, so an answer can be 0.
Input
The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of trades.
Each of the next n lines describes one trade and consists of three fields:
TIME SYMBOL PRICE
Fields are separated by one or more space characters. A line may also start and end with any number of spaces (including none). No other whitespace appears inside a line, and no line is longer than 100 characters.
TIMEhas the formHH:MM:SS.mmm: exactly two digits of hours00..23, a colon, exactly two digits of minutes00..59, a colon, exactly two digits of seconds00..59, a dot, and exactly three digits of milliseconds000..999. All times belong to the same day, and they are non-decreasing from line to line.SYMBOLis 1 to 10 characters. Its first character is a letter (A..Zora..z); the others are letters, digits0..9, dots.or underscores_. Symbols are case-sensitive:abcandABCare different symbols.PRICEis a non-negative decimal number: an integer part of 1 to 20 digits, optionally followed by a dot and a fractional part of 1 to 4 digits. The integer part may have leading zeros and the fractional part may have trailing zeros, so007.50,7.5and7.5000are the same price. The value of every price is less than 10^14.
Output
Print n lines. The i-th line must contain the answer for trade i: the number of milliseconds
until the next strictly higher trade of the same symbol, or -1 if there is none.
Notes
In the first example, the ACME trades have prices 10, 9.5, 10, 10.25. The first one waits for
the fourth one (09:30:02.000 − 09:30:00.000 = 2000 ms), because the third trade has an equal,
not higher, price. ZETA trades only once and gets -1.
In the second example, prices are compared exactly: 99999999999999.0001 is lower than
99999999999999.0002, 0100 equals 100.0000 but is lower than 100.0001, and LO.W is a
different symbol from lo.w.