Amplitude Hackathon '23 Problem 2 - Gigatron Lag

View as PDF

Submit solution

Points: 3 (partial)
Time limit: 5.0s
Memory limit: 1G

Problem type

Kurt has built a cool new service called Gigatron. He's not sure how well it's performing relative to its predecessor, Megatron. Your job is to help him determine how much Gigatron is lagging.

Here's a simplified model of how Gigatron works. Gigatron has 1024 partitions, numbered 1 through 1024. Each partition corresponds to a queue of events that need to be processed. We define the "lag" of a partition to be the number of active events in its corresponding queue.

There are six types of actions that can happen:

  1. Events can be added to a single partition.
  2. Events can be added to all partitions.
  3. Gigatron can process events on a single partition.
  4. Gigatron can process events on all partitions.
  5. Kurt wants to know the lag of a specific partition.
  6. Kurt wants to know the maximum lag over all partitions.

When Gigatron is initialized, all partitions have zero lag. Help Kurt analyze Gigatron's performance!

Constraints

1 \le N \le 10^4

1 \le p \le 1024

1 \le x \le 10^5

There is at least one event of the form LAG p or MAXLAG.

Input Specification

The first line of input contains a single integer, N. The number of actions is N.

Each of the next N lines contains a line either of the form ADD p x, ADDALL x, or PROCESS p x, PROCESSALL x, LAG p, or MAXLAG.

If the line is of the form ADD p x, partition p has x additional events to process.

If the line is of the form ADDALL x, all partitions have x additional events to process.

If the line is of the form PROCESS p x, Gigatron processes x events from partition p. If x is greater than the number of events in partition p's queue, all events are processed.

If the line is of the form PROCESSALL x, Gigatron processes x events from every partition. If x is greater than the number of events in some partition's queue, all events are processed from that queue.

If the line is of the form LAG p, print the lag of partition p.

If the line is of the form MAXLAG, print the maximum lag over all partitions.

Output Specification

For every LAG line, print Gigatron's lag at the time of the request. For every MAXLAG line, print the maximum lag over all partitions.

Print each lag amount on a separate line.

Sample Input

15
LAG 39
MAXLAG
ADDALL 17
LAG 42
MAXLAG
PROCESS 1 2
LAG 1
MAXLAG
PROCESSALL 1
LAG 39
MAXLAG
PROCESSALL 100
MAXLAG
ADD 17 1
MAXLAG

Sample Output

0
0
17
17
15
17
16
16
0
1

Sample Explanation

Gigatron starts out with no lag over all partitions. The third event causes all partitions to have a lag of 17. After processing 2 events from partition 1, partition 1 has a lag of 15. After processing 1 event from all partitions, the maximum lag is 16 and all partitions except for partition 1 have a lag of 16. After attempting to process 100 events from all partitions, the maximum lag is 0. After adding one event to partition 17, Gigatron ends with a maximum lag of 1.


Comments

There are no comments at the moment.