Restrictive Filesystem
InterviewTime limit8sMemory limit512 MB
Simulate a filesystem where writes fill the first free sectors with possible fragmentation, deletes free a file, and references report which file occupies a sector.
- Level
Medium7 of 10
- Topics
- Implementation, Simulation, Intervals, Hash map
- Solved
- No attempts yet
Problem
You are a programmer on a team developing a new storage medium. This medium supports random access for reading and erasing data. Writing data, however, always proceeds sequentially from the front, and data can only be written to the first free region found.
You have started building a filesystem for this medium. Because of the medium's restriction, data is written starting from the frontmost free region. If during a write the process reaches a region occupied by other data, the remaining data is written starting from the free region after it.
Data is written in units called sectors. Sectors are numbered starting from 0, and this number indicates the physical position on the medium. Sector numbers are assigned in order 0, 1, 2, 3, … from the front of the medium toward the back.
The filesystem has three commands: write, delete, and reference a sector.
Your job is to reproduce the behavior of this filesystem and write a program that outputs which file is placed in the target sector when a reference command is executed. Initially, nothing is written on the medium.
For example, consider the first sample in the Sample Input. The first command writes a file with identifier 0 and size 2. Initially nothing is written on the medium, so every sector is free, and the write goes to the first two sectors, sector 0 and sector 1. After the write, the medium looks like this.
0 0 空 空 空 空 空 空 …
The second command writes a file with identifier 1 into sectors 2 and 3. After that, the medium looks like this.
0 0 1 1 空 空 空 空 …
The third command deletes the file with identifier 0. The medium looks like this.
空 空 1 1 空 空 空 空 …
The fourth command writes a file with identifier 2 into sectors 0, 1, 4, and 5.
2 2 1 1 2 2 空 空 …
The last command references sector 3. Sector 3 currently holds the file with identifier 1, so your program must output 1.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
N
Command1
Command2
...
CommandN
N is the number of commands to execute (1 ≤ N ≤ 10,000), and Command**i is the i-th command to execute.
Each command consists of a command name and one or two arguments. The command name is a single character, one of W, D, or R. A single space separates the command from its arguments, and one argument from the next.
W is the write command. Two arguments are given: I (0 ≤ I ≤ 10^9) and S (1 ≤ S ≤ 10^9). They are the identifier of the file to write and the number of sectors needed to store it.
D is the delete command. One argument I (0 ≤ I ≤ 10^9) is given. It is the identifier of the file to delete.
R is the reference command. One argument P (0 ≤ P ≤ 10^9) is given. It is the number of the sector to reference.
You may assume that no sector with a number greater than 10^9 needs to be accessed. It is also guaranteed that no file with the same identifier is written more than once.
The end of the input is indicated by a line containing a single 0.
Output
For each dataset, each time a reference command appears, output the identifier of the file referenced by that command on one line. If no file is written in the referenced sector, output -1 instead of a file identifier.
Put a blank line after each dataset.