#C3894. Timespan Manager
Timespan Manager
Timespan Manager
You are required to implement a Timespan Manager that maintains a list of disjoint time intervals. The manager supports three operations:
- add start end: Adds the time range \([start, end]\). When a new range overlaps with existing ranges, they must be merged into one continuous range.
- remove start end: Removes the interval \([start, end]\) from the stored ranges. This might result in splitting an existing range into two parts if the removed segment is in the middle.
- print: Prints the current list of time ranges. The output should be in the Python list format. For example, if the ranges are \([1,5]\) and \([10,20]\) then the output should be:
[[1, 5], [10, 20]]
.
Note: All numbers represent seconds. The merging and splitting should follow the rules defined above.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains an integer Q, representing the number of operations.
- The following Q lines each contain an operation in one of the following formats:
add start end
remove start end
print
Both add
and remove
operations are followed by two integers indicating the start and end of the time range. There can be multiple print
operations. For each print
command, you must output the current list of disjoint time intervals.
outputFormat
For each print
operation in the input, output a line to stdout
representing the current list of time ranges in the format of a Python list. For example: [[1, 5], [10, 25]]
6
add 1 5
print
add 10 20
print
add 15 25
print
[[1, 5]]
[[1, 5], [10, 20]]
[[1, 5], [10, 25]]
</p>