Problem 592
Exclusive Time of Functions
Medium · 20 pts · Stack, Simulation
Problem
A single-threaded CPU runs N functions with ids 0 to N-1. A log 'id:start:t' means function id started at the beginning of time unit t, and 'id:end:t' means it ended at the end of time unit t. When a function calls another one, the caller pauses until the callee ends; a function can call itself. The exclusive time of a function is the total number of time units in which it was the one actually running. Print the exclusive time of every function. Keep a stack of running calls and the time of the previous event.
Input
The first line contains N and M, the number of log entries. Each of the next M lines contains one log entry, in time order.
Output
Print N integers separated by spaces: the exclusive time of functions 0 to N-1.
Constraints
1 <= N <= 100; 2 <= M <= 500; 0 <= t <= 10^9; the logs are valid and in increasing time order
Example input
2 4 0:start:0 1:start:2 1:end:5 0:end:6
Example output
3 4
Explanation
Function 0 runs during units 0 and 1, then calls function 1, which runs for units 2 to 5. Function 0 resumes and runs unit 6. So function 0 runs 3 units and function 1 runs 4 units, and the answer is 3 4.