#C11511. Perform Operations on Array
Perform Operations on Array
Perform Operations on Array
You are given a series of operations to perform on an array. The operations include appending an integer, removing all occurrences of a specified integer, and querying the maximum subarray sum within a specified range. For the query operation, the maximum subarray sum is defined as the largest sum of any contiguous subarray within the given segment. Mathematically, if (a_1, a_2, \dots, a_n) represent the subarray, you must find (\max_{1 \leq i \leq j \leq n}\sum_{k=i}^j a_k).
The operations are provided in the following format:
- Append(x): Append the integer (x) to the end of the array.
- Remove(x): Remove all occurrences of the integer (x) from the array.
- MaxSubArray(L, R): Compute the maximum subarray sum for the subarray from index (L) to (R) (both inclusive, with 1-indexing).
It is recommended to use Kadane's algorithm to solve the maximum subarray problem efficiently. You may assume that every query operation refers to a non-empty subarray.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- The first line contains an integer \(Q\) denoting the number of operations.
- The next \(Q\) lines each contain an operation in one of the formats:
1 x
— Append the integer \(x\) to the array.2 x
— Remove all occurrences of the integer \(x\) from the array.3 L R
— Output the maximum subarray sum for the subarray from index \(L\) to \(R\) (1-indexed).
outputFormat
For each operation of type 3 (MaxSubArray query), output the resulting maximum subarray sum on a new line to standard output (stdout).## sample
7
1 1
1 -2
1 3
1 2
3 1 3
2 -2
3 1 3
3
6
</p>