#K14906. Mystical Forest Energy Transformation
Mystical Forest Energy Transformation
Mystical Forest Energy Transformation
You are given a mystical forest with n trees, each with an initial magical energy. The trees are arranged in a straight line and are numbered from 1 to n.
There are two types of operations:
- update i: Cast a transformation spell on the tree at position i (1-indexed). The spell affects the tree at position i as well as its immediate neighbors (i-1 and i+1 if they exist). For each affected tree, if its current energy is even, its energy increases by 1; if odd, its energy decreases by 1. Formally, for each affected tree with energy \( E \): \[ E = \begin{cases} E+1, & \text{if } E \equiv 0 \pmod{2} \\ E-1, & \text{if } E \equiv 1 \pmod{2} \end{cases} \]
- query l r: Output the sum of the energies of the trees from index l to r (inclusive), where \(1 \le l \le r \le n\).
You are required to process a sequence of \(q\) operations and produce the results for all query operations.
Note: The update operation directly modifies the state of the forest which affects subsequent operations.
inputFormat
The first line contains two integers \(n\) and \(q\) separated by a space, where \(n\) is the number of trees and \(q\) is the number of operations.
The second line contains \(n\) integers representing the initial energies of the trees.
The next \(q\) lines each describe an operation. Each operation is either in the format update i
or query l r
, where i
, l
, and r
are integers.
outputFormat
For each query
operation, print the corresponding sum of tree energies on a new line.
5 5
2 3 4 5 6
update 3
query 1 5
update 1
query 1 3
query 2 4
19
11
12
</p>