Light Switching#2905

Light Switching (Editorial)

Let a_i denote the state of the lamp in stall : a_i = 1 if the lamp is on, and a_i = 0 otherwise. Initially all a_i = 0. An operation of the first type replaces each a_i on the segment [S_i; E_i] with 1 - a_i, and a query of the second type asks for the sum of the elements on a segment — which is exactly the number of lamps that are on.

If we toggle the lamps one by one, a single operation may take steps, and with n, m \le 10^5 this is too slow. We use a segment tree with lazy operations (lazy propagation), which performs both operations in O(\log n).

Information in a vertex. Each vertex of the tree corresponds to some segment [l; r]. A vertex stores two values:

Toggle. If lamps were on in the segment [l; r], then after toggling exactly the lamps that were off will be on. Therefore, the new sum is

\mathit{sum} = (r - l + 1) - \mathit{sum}

Two consecutive toggles return the lamps to their original state. Therefore, a new toggle does not overwrite the deferred one but is added to it modulo : the flag is inverted. If, while descending the tree, the query segment coincides with the segment of a vertex, we perform the toggle at this vertex and do not descend further.

Push. Before descending from a vertex with \mathit{add} = 1 to its children, we toggle each child: its sum is replaced by the length of its segment minus the previous sum, and its flag is inverted. The vertex itself gets \mathit{add} = 0:

Unlike assignment on a segment, during a push we cannot simply write new values into the children: the result depends on their current sums and flags. If a child already had a deferred toggle (a_1 = 1), then after the push its flag becomes — the two toggles cancel each other out.

Initially all lamps are off, so all vertices have \mathit{sum} = 0 and \mathit{add} = 0.

Complexity. Each operation takes O(\log n), so the total complexity is O(m \log n).

Example

Let us perform the operations from the example, n = 4. After the operations 0 1 2 and 0 2 4, the lamps are in the state (1, 0, 1, 1). The figure shows the state of the segment tree after these operations. The flag is not used in the leaves, so it is not shown for them.

The operation 0 1 2 toggled the segment of the vertex entirely: it got \mathit{sum} = 2 and \mathit{add} = 1. The operation 0 2 4 descended through the vertex , so a push was performed there first, and the leaves and got \mathit{sum} = 1. Then the leaf (\mathit{sum} = 0) and the vertex (\mathit{sum} = 2, \mathit{add} = 1) were toggled. The children of the vertex were not changed and store the outdated values \mathit{sum} = 0.

Query 1 2 3. The segment is split into the parts and . To get the value in the leaf , we have to descend from the vertex , so a push is performed first:

The answer to the query is 0 + 1 = 1.

Operation 0 2 4. The leaf (\mathit{sum} = 1) and the vertex are toggled: the latter gets \mathit{sum} = 2 - 2 = 0 and \mathit{add} = 1. The lamps are in the state (1, 1, 0, 0).

Query 1 1 4. The query segment coincides with the segment of the root, where \mathit{sum} = 2 + 0 = 2. The answer is .

Algorithm Implementation

The segment tree is stored in the vector , whose elements have the type . The field contains the number of lamps that are on in the segment of the vertex, and the field is the flag of a deferred toggle. The constructor sets \mathit{sum} = 0 and \mathit{add} = 0: initially all lamps are off and there are no deferred toggles. The number of lamps does not exceed 10^5, so the int type is sufficient.

struct node
{
  int sum, add;
  node() : sum(0), add(0) {}
};

vector<node> SegTree;

The function Push pushes the deferred toggle from vertex to its children. Vertex corresponds to the segment [\mathit{lpos}; \mathit{rpos}], and is its middle. If \mathit{add} = 0, there is nothing to push.

void Push(int v, int lpos, int mid, int rpos)
{
  if (SegTree[v].add == 0) return;

Toggle each child: invert its flag and replace its sum with the length of its segment minus the previous sum. The segment of the left child [\mathit{lpos}; \mathit{mid}] has length \mathit{mid} - \mathit{lpos} + 1, and the segment of the right child [\mathit{mid} + 1; \mathit{rpos}] has length \mathit{rpos} - \mathit{mid}.

  SegTree[2*v].add ^= 1;
  SegTree[2*v].sum = (mid - lpos + 1) - SegTree[2*v].sum;
  SegTree[2*v+1].add ^= 1;
  SegTree[2*v+1].sum = (rpos - mid) - SegTree[2*v+1].sum;

The deferred toggle has been passed to the children, so we set \mathit{add} = 0 in vertex .

  SegTree[v].add = 0;
}

The function SwitchValue toggles all lamps on the segment [\mathit{left}; \mathit{right}]. Vertex corresponds to the segment [\mathit{lpos}; \mathit{rpos}]. If the query segment is empty, we exit the function.

void SwitchValue(int v, int lpos, int rpos, int left, int right)
{
  if (left > right) return;

If the query segment coincides with the segment of vertex , we toggle the vertex: invert the flag and replace the sum with (\mathit{rpos} - \mathit{lpos} + 1) - \mathit{sum}. We do not descend further.

  if ((lpos == left) && (rpos == right))
  {
    SegTree[v].add ^= 1;
    SegTree[v].sum = (rpos - lpos + 1) - SegTree[v].sum;
    return;
  }

Find the middle of the segment \mathit{mid} = (\mathit{lpos} + \mathit{rpos}) / 2 and, before descending to the children, push the deferred toggle of vertex .

  int mid = (lpos + rpos) / 2;
  Push(v, lpos, mid, rpos);

Recursively toggle the lamps on the parts of the query segment that lie in the left and right children.

  SwitchValue(2*v, lpos, mid, left, min(mid, right));
  SwitchValue(2*v+1, mid+1, rpos, max(left, mid+1), right);

Recompute the sum in vertex as the sum of the values in its children.

  SegTree[v].sum = SegTree[2*v].sum + SegTree[2*v+1].sum;
}

The function Summa returns the number of lamps that are on in the segment [\mathit{left}; \mathit{right}]. Vertex corresponds to the segment [\mathit{lpos}; \mathit{rpos}]. For an empty segment we return .

int Summa(int v, int lpos, int rpos, int left, int right)
{
  if (left > right) return 0;

If the query segment coincides with the segment of vertex , return the sum stored in it.

  if ((lpos == left) && (rpos == right))
    return SegTree[v].sum;

Find the middle of the segment and push the deferred toggle of vertex : otherwise the sums in the children may be outdated.

  int mid = (lpos + rpos) / 2;
  Push(v, lpos, mid, rpos);

Split the query segment into the parts that lie in the left and right children, recursively compute the sums on them and add up the results.

  return Summa(2*v, lpos, mid, left, min(mid, right)) +
         Summa(2*v+1, mid+1, rpos, max(left, mid+1), right);
}

The main part of the program. Read the input values and . Create a segment tree of vertices. The constructor of the structure sets \mathit{sum} = 0 and \mathit{add} = 0 in each vertex, so there is no need to build the tree separately.

scanf("%d %d", &n, &m);
SegTree.resize(4 * n);

Process the operations one by one. For operation , print the number of lamps that are on in the segment [\mathit{left}; \mathit{right}]; for operation , toggle all lamps on this segment.

for (int i = 0; i < m; i++)
{
  scanf("%d %d %d", &type, &left, &right);
  if (type == 1)
    printf("%d\n", Summa(1, 1, n, left, right));
  else
    SwitchValue(1, 1, n, left, right);
}