#9. RUST

RUST

Problem Background

You're right, but Rust is a new zero-cost abstraction programming language independently developed by Mozilla. The story takes place in a world called "RAII" (Resource Acquisition Is Initialization), where objects that leave their scope are granted the "drop" method, which allows them to reclaim resources. You play a mysterious character named "Ferris," embarking on a journey through code, encountering variables of various shapes and lifetimes, working with them to defeat powerful compilation errors, and restoring memory safety—while gradually uncovering the truth behind "Servo."

Problem Description

You have been given the task of managing memory blocks. Your goal is to write a program that simulates simplified memory management logic, ensuring that no "dangling pointers" or "double frees" occur during program execution.

Specifically, given an array of n memory blocks, each uniquely numbered, you need to allocate and free these memory blocks according to a series of operations:

  • Operation "alloc x" means to allocate the memory block with number x.
  • Operation "free x" means to free the memory block with number x.

After being freed, a memory block is considered to be unallocated.

You need to ensure that:

  1. No attempt is made to free an unallocated memory block.
  2. No attempt is made to allocate an already allocated memory block.
  3. All memory block numbers are between 11 and nn.

Please implement a program that, given a sequence of operations, determines whether any illegal operations occur. If an illegal operation is found, output Illegal operation; otherwise, output All operations are safe.

You will score points for this problem only if all test cases pass.

Input Format

  • The first line contains two integers, n and m, representing the number of memory blocks and the number of operations, respectively.
  • The next m lines each contain one operation (alloc x or free x).

Output Format

  • If all operations are legal, output All operations are safe.
  • If any illegal operation occurs, output Illegal operation.

Sample #1

Sample Input #1

5 4
alloc 1
alloc 2
free 1
free 2

Sample Output #1

All operations are safe

Sample #2

Sample Input #2

3 4
alloc 1
free 1
free 1
alloc 5

Sample Output #2

Illegal operation

Sample #3

Sample Input #3

4 3
free 2
alloc 3
alloc 3

Sample Output #3

Illegal operation

Hints

  • 1n10001 ≤ n ≤ 1000
  • The valid range of memory block numbers for each operation is between 11 and nn.

Explanation for Sample #2:

  1. alloc 1: Allocate memory block number 1, which is valid.
  2. free 1: Free memory block number 1, which is valid.
  3. free 1: Attempt to free memory block number 1 again, which is already freed—this is an illegal operation.
  4. alloc 5: Attempt to allocate memory block number 5, which is out of the valid range of 1 to 3—this is an illegal operation.

Since illegal operations occurred in steps 3 and 4, the program should output Illegal operation.