A logistics crew runs a field radio relay board during long expeditions. Analysts carry handheld terminals that can be tuned into a named channel. Whenever dispatch broadcasts a batch of numeric sensor readings on a channel, every terminal currently tuned into that exact channel reports back the sum of the readings it just received, and the reports are listed in the order those terminals originally tuned in (oldest first). A terminal can later be pulled off the board; once pulled, it never reports on any future broadcast, on any channel.
You will process a sequence of operations of three kinds: tuning a new terminal into a channel, broadcasting a batch of readings on a channel, and pulling a specific terminal off the board.
The first line contains a single integer q, the number of operations. Each of the next q lines describes one operation, in one of the following three forms:
TUNE channel — tune a brand-new terminal into the given channel.BROADCAST channel k v_1 v_2 ... v_k — broadcast k readings on the given channel.DROP id — permanently pull the terminal with registration number id off the board.Registration numbers are assigned in increasing order starting at 1, one per TUNE operation, in the order those operations appear in the input (numbering is shared across all channels, not restarted per channel). It is guaranteed that every DROP id refers to a terminal that was tuned in earlier in the input and has not already been dropped.
For every BROADCAST operation, output one line: the sums reported by each terminal currently tuned into that exact channel, oldest tuned-in terminal first, separated by single spaces. If no terminal is currently tuned into that channel, output the single word NONE instead. Produce no output at all for TUNE or DROP operations. The total number of output lines therefore equals the number of BROADCAST operations.
channel consists of 1 to 20 lowercase English letters and digitsid always refers to a terminal tuned in earlier in the input that has not yet been droppedExample 1
Input
6 TUNE alpha TUNE beta BROADCAST alpha 2 3 4 DROP 1 BROADCAST alpha 1 5 BROADCAST beta 2 10 20
Expected
7 NONE 30
Explanation
Terminal 1 tunes into alpha, terminal 2 tunes into beta. The first broadcast on alpha carries readings 3 and 4 (sum 7); only terminal 1 is tuned into alpha, so the output line is "7". Terminal 1 is then dropped. The next broadcast on alpha carries reading 5 (sum 5), but no terminal is tuned into alpha anymore, so the output is "NONE". The broadcast on beta carries readings 10 and 20 (sum 30); terminal 2 is tuned into beta, so the output is "30".
Example 2
Input
5 TUNE ops TUNE ops BROADCAST ops 0 TUNE ops BROADCAST ops 2 1 1
Expected
0 0 2 2 2
Explanation
Terminals 1 and 2 both tune into channel ops. A broadcast with k=0 readings has sum 0, reported by both currently-tuned terminals in tune-in order, giving "0 0". Terminal 3 then also tunes into ops. The next broadcast carries readings 1 and 1 (sum 2), reported by all three terminals now tuned into ops in tune-in order, giving "2 2 2".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →