Streetlights
Time limit2sMemory limit1024 MB
Maintain a binary string under range set-to-0 and set-to-1 operations, and after each report how many substrings have ever been all ones at some point in time.
- Level
Hard8 of 10
- Topics
- Segment tree, Intervals, Implementation, Math
- Solved
- No attempts yet
Problem
The Underwater Canal street is lit by streetlights, numbered 1 to along the street. A segment is one or more streetlights that stand consecutively. Thus the total number of segments is . A segment is healthy if the bulbs in all of its streetlights are working.
Events of one of two types happen to the streetlights regularly:
- in some segment, a voltage surge burns out all the bulbs at once;
- Arkhenergo picks some segment and sends repair crews to replace every burnt-out bulb in it with a working one.
After each event, the city hall demands that Arkhenergo report the number of healthy segments. To make their performance look better, the repair crews include in the report every segment that is healthy now or was ever healthy before.
Write a program that determines, after each event, the number of segments that are healthy at that moment or were healthy at any time before that event.
Input
The first line of the input contains two numbers and , the number of streetlights and the number of events that happened. The next line of the input consists of characters 0 and 1 describing the initial state of the streetlights, where 1 denotes a streetlight with a working bulb and 0 denotes one with a burnt-out bulb.
Each of the following lines contains a description of an event as three numbers , and , which mean that after this event all bulbs in the streetlights numbered :
- burn out if ,
- become working if .
In the descriptions of all events , and takes the value or .
Output
In the first line of the output, print a single number: the number of healthy segments in the initial state. Then print numbers, one per line: for each event that happened, print the number of segments listed in the report after that event.