#P5069. Sequence Zeroing via Adjacent Decrement Operations

    ID: 18307 Type: Default 1000ms 256MiB

Sequence Zeroing via Adjacent Decrement Operations

Sequence Zeroing via Adjacent Decrement Operations

You are given a positive integer sequence (a_1,a_2,\ldots,a_n). You need to support update operations on this sequence. Each update permanently changes one element of the sequence. After each update, consider the following process (note that the process does not change the updated sequence):

  1. While there is at least one nonzero element in the sequence, perform one operation:

    • Find the position (x) where (a_x) is maximum. If there are multiple positions with the same maximum value, choose the one with the smallest index.
    • Decrease (a_{x-1}), (a_x), and (a_{x+1}) by (1) each (if such positions exist). After subtraction, if any value becomes negative, reset it to (0).
  2. Count how many operations are required to turn the sequence into all zeros.

Your task is to process (q) update queries. Each query provides an index (i) and a new positive value (v), and you should update (a_i = v) permanently and then output the number of operations required as defined above for the current sequence.

Note: The simulation process does not modify the sequence permanently; it only computes the number of operations required.

Constraints (for the purpose of this problem):\(1 \le n, q \le 100\) and the values of \(a_i\) and updates are positive integers that are reasonably small.

inputFormat

The first line contains two integers \(n\) and \(q\) — the length of the sequence and the number of update queries.

The second line contains \(n\) space-separated positive integers \(a_1,a_2,\ldots,a_n\).

Each of the next \(q\) lines contains two integers \(i\) and \(v\) (\(1 \le i \le n\)) representing that the \(i\)-th element of the sequence is updated to \(v\).

outputFormat

For each query, output a single integer — the number of operations needed to reduce the current sequence to all zeros by repeatedly performing the described operation.

sample

5 2
1 2 3 2 1
3 5
1 4
7

10

</p>