Mirror Circuit#13046

Mirror Circuit (Editorial)

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:

(A\otimes B)[x][z] = \max_{y\in\{0,1\}} (A[x][y]+B[y][z]).

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

N=N_L\otimes N_R.

For , there are two possibilities.

The flipped interval can end inside the left child:

P_L\otimes N_R.

Or the whole left child is flipped and the interval continues into the right child:

W_L\otimes P_R.

Therefore,

P= \max( P_L\otimes N_R, W_L\otimes P_R ).

Similarly,

S= \max( N_L\otimes S_R, S_L\otimes W_R ).

For , there are five possibilities.

The flipped interval can lie inside the left child:

F_L\otimes N_R.

It can lie inside the right child:

N_L\otimes F_R.

It can end exactly at the right boundary of the left child:

S_L\otimes N_R.

It can start exactly at the left boundary of the right child:

N_L\otimes P_R.

Finally, it can cross the boundary between the children:

S_L\otimes P_R.

Therefore,

F= \max( F_L\otimes N_R, N_L\otimes F_R, S_L\otimes N_R, N_L\otimes P_R, S_L\otimes P_R ).

If the whole segment is flipped, then

W=W_L\otimes W_R.

Leaf

For one gate, let be its transition matrix for the original value d_i.

Let be its transition matrix after replacing

d_i\leftarrow9-d_i.

Then

N=A,

and

P=S=W=B.

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

d\bmod4=0,

the state is flipped.

If

d\bmod4=1,

the state does not change.

If

d\bmod4=2,

the state becomes .

If

d\bmod4=3,

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:

N,\ P,\ S,\ F,\ W.

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

O((n+q)\log n)

time and

memory.