Solution
The signal has only two possible states: and .
Therefore, the behaviour of any segment can be represented by a 2\times2 matrix.
For a matrix , let be the maximum reward obtainable if the signal enters the segment in state and leaves it in state .
If such a transition is impossible, its value is .
Suppose two consecutive segments have matrices and .
Then their concatenation is represented by max-plus multiplication:
Thus, two segment matrices can be merged in constant time.
For every segment tree node, we maintain five matrices.
— no gate is flipped;
— the flipped interval starts at the left boundary;
— the flipped interval ends at the right boundary;
— the flipped interval is strictly inside the segment;
— the whole segment is flipped.
Suppose a node consists of a left child and a right child .
If no gate is flipped, then
For , there are two possibilities.
The flipped interval can end inside the left child:
Or the whole left child is flipped and the interval continues into the right child:
Therefore,
Similarly,
For , there are five possibilities.
The flipped interval can lie inside the left child:
It can lie inside the right child:
It can end exactly at the right boundary of the left child:
It can start exactly at the left boundary of the right child:
Finally, it can cross the boundary between the children:
Therefore,
If the whole segment is flipped, then
Leaf
For one gate, let be its transition matrix for the original value d_i.
Let be its transition matrix after replacing
Then
and
For a segment consisting of one element, it is impossible to have a non-empty flipped interval strictly inside it, so all values of are initially .
To construct the transition matrix for a digit , consider both possible input states.
If
the state is flipped.
If
the state does not change.
If
the state becomes .
If
the state becomes .
If the resulting state is , we add reward w_{i,s}.
Queries
For a query [l,r], obtain the five matrices corresponding to this segment from the segment tree.
Before gate , the signal is in state .
Therefore, the answer is the maximum value among both possible final states in all five matrices:
Matrix represents performing no replacement.
The other four matrices together represent every possible non-empty replacement interval.
Updates
For an update of d_i, rebuild the corresponding leaf.
Then recompute all segment tree nodes on the path from this leaf to the root.
Correctness
Every possible replacement interval relative to a segment belongs to exactly one of the types represented by , , , or .
The merge formulas consider every possible position of this interval relative to the boundary between the two children.
Max-plus matrix multiplication correctly combines both the signal transition and the total reward of consecutive segments.
Therefore, every matrix stores the maximum reward for its corresponding type of replacement.
For a query, we take the maximum value reachable from initial state over , , , , and .
This considers both performing no replacement and every possible non-empty replacement interval.
Hence, the returned value is exactly the required maximum reward.
Complexity
Every matrix has constant size, so merging two segment tree nodes takes time.
Each update and each query processes O(\log n) nodes.
Therefore, the total complexity is
time and
memory.