#P8518. Candy Box Operations
Candy Box Operations
Candy Box Operations
Aunt Khong is preparing \( n \) boxes of candies for a school. The boxes are numbered from \( 0 \) to \( n-1 \), and initially, all boxes are empty. The \( i \)-th box (\(0 \leq i \leq n-1\)) can hold at most \( c[i] \) candies.
Over \( q \) days, Aunt Khong performs operations on these boxes. On day \( j \) (\(0 \leq j \leq q-1\)), she is given three integers \( l[j] \), \( r[j] \), and \( v[j] \) (with \( v[j] \neq 0 \)). For every box \( k \) with \( l[j] \leq k \leq r[j] \), the following operation is performed:
- If \( v[j] > 0 \), she adds candies one by one until she adds exactly \( v[j] \) candies or the box becomes full. Mathematically, if the box had \( p \) candies before the operation, it will contain \( \min(c[k], p + v[j]) \) candies afterward.
- If \( v[j] < 0 \), she removes candies one by one until she has removed exactly \( -v[j] \) candies or the box becomes empty. In this case, the new number of candies becomes \( \max(0, p + v[j]) \).
Your task is to determine the number of candies in each box after all \( q \) days of operations.
inputFormat
The first line contains two integers \( n \) and \( q \): the number of boxes and the number of days respectively.
The second line contains \( n \) integers \( c[0], c[1], \ldots, c[n-1] \), where \( c[i] \) denotes the capacity of the \( i \)-th box.
Each of the following \( q \) lines contains three integers \( l[j] \), \( r[j] \), and \( v[j] \) representing the operation on day \( j \) (0-indexed). It is guaranteed that \( 0 \leq l[j] \leq r[j] \leq n-1 \) and \( v[j] \neq 0 \).
outputFormat
Output a single line with \( n \) integers separated by spaces, representing the final number of candies in each box after all operations.
sample
5 3
10 5 8 7 9
1 3 4
0 2 -3
2 4 5
0 1 6 7 5