#P5445. [APIO2019] 路灯

[APIO2019] 路灯

Description

A self-driving taxi is driving along a street in Innopolis. There are n+1n+1 stops on the street, which divide the street into nn road segments. Each segment has a street lamp. When the ii-th lamp is on, it lights up the segment connecting stops ii and i+1i+1. Otherwise, this segment is dark.

For safety reasons, the taxi can only drive on lit segments. In other words, the taxi can travel from stop aa to stop bb (a<b)(a<b) if and only if the segments connecting stops aa and a+1a+1, a+1a+1 and a+2a+2, \ldots, b1b-1 and bb are all lit.

After some unexpected failures or repairs, the street lamps on the street may be on or off.

You are given the initial states of the street lamps at time 00. Then at times 1,2,,q1,2,\ldots,q, one of the following two events happens at each time:

  • toggle i\text{toggle}\ i: Toggle the state of the ii-th street lamp. Specifically, if it was on, it becomes off; if it was off, it becomes on.

  • query a b\text{query}\ a\ b: The head of the taxi department wants to know: from time 00 up to the current time, how many times satisfy that the taxi can travel from stop aa to stop bb.

Please help answer these queries for the head of the taxi department.

Input Format

The first line contains two integers nn and qq, representing the number of street lamps and the number of time moments.

The second line contains a string ss representing the initial states of the street lamps. si=1s_i=1 means the ii-th lamp is initially on, and si=0s_i=0 means the ii-th lamp is initially off.

The next qq lines each describe the event at a time moment. The ii-th of these lines describes the event that happens at time ii:

  • toggle i\text{toggle}\ i: At this time moment, the state of the ii-th street lamp is toggled.

  • query a b\text{query}\ a\ b: Compute, from time 00 up to this time moment, how many time moments satisfy that the taxi can travel from stop aa to stop bb.

At least one event is query\text{query}.

Output Format

For each query\text{query} event, output a single integer on its own line, which is the answer to that query.

5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
1
2
0
0
1
2

Hint

For all data, 1n,q3×1051 \leq n,q \leq 3\times 10^5, s=n|s|=n, 1in1 \leq i \leq n, 1a<bn+11 \leq a < b \leq n+1.

The detailed additional constraints and scores for subtasks are as follows.

Subtask Additional Constraints Score
1 nn, q100q \leq 100 20
2 For all query a b\text{query}\ a\ b events, a=b1a=b-1 holds
3 For all toggle i\text{toggle}\ i events, the ii-th lamp will be turned on
4 All toggle\text{toggle} events happen before the first query\text{query} event
5 No additional constraints

Translated by ChatGPT 5