#K7166. Playlist Operations
Playlist Operations
Playlist Operations
You are given a playlist consisting of several songs, where each song is represented by its duration in seconds. Your task is to implement a Playlist that supports the following operations:
add X
: Add a song with duration X (in seconds) to the end of the playlist.remove I
: Remove the song at the 0-based index I from the playlist. If I is an invalid index, ignore the operation.duration
: Output the total duration of all songs currently in the playlist.
The first part of the input provides the initial list of songs. You will then be given a number of operations to perform. For every duration
operation, you must print the total duration of the playlist to standard output.
Implementation Details:
- You need to implement a class or structure to handle the playlist operations.
- When removing a song, if the given index is out of bounds, simply ignore the command.
Note: Any mathematical formulas in your explanation should be written in \( \LaTeX \) format.
inputFormat
The input is given in multiple lines:
- The first line contains an integer
N
representing the number of songs initially in the playlist. - The second line contains
N
space-separated integers representing the duration (in seconds) of each song. IfN
is 0, this line may be empty. - The third line contains an integer
Q
representing the number of operations. - The following
Q
lines each contain one operation. Each operation is one of the following:add X
— Add a song of durationX
seconds.remove I
— Remove the song at indexI
(0-indexed). Ignore if invalid.duration
— Output the current total duration of the playlist.
outputFormat
For each duration
operation in the input, output the total duration (i.e., the sum of all song lengths in seconds) on a separate line.
3
120 150 180
5
duration
add 300
duration
remove 1
duration
450
750
600
</p>