This page is still under construction.

Parts of this page are still being built. What you see may change.

Switching Lights

Interview

Time limit1sMemory limit128 MB

Summary
Maintain a binary array of N lights under M range-toggle and range-count operations, and print each query result.
Level

Medium5 of 10

Topics
Segment tree, Array, Prefix sum
Solved
No attempts yet

Problem

Farmer John keeps his cows sharp by letting them play with intellectual toys. One of the larger toys is the set of lights in the barn. Each of the N (2≤N≤5002 \le N \le 500) cow stalls, conveniently numbered 1…N1 \ldots N, has a colorful light above it.

At the beginning of the evening, all lights are off. The cows control the lights with a set of N pushbutton switches. Pushing switch ii changes the state of light ii from off to on, or from on to off (a toggle).

The cows read and execute a list of M (1≤M≤20001 \le M \le 2000) operations. Each operation is distinguished by a leading integer that is either 00 or 11.

An operation of type 00 is followed by two integers SS and EE (1≤S≤E≤N1 \le S \le E \le N) indicating a starting and an ending switch. It is executed by pushing each switch from SS through EE inclusive exactly once.

An operation of type 11 is followed by two integers SS and EE (1≤S≤E≤N1 \le S \le E \le N) specifying an inclusive range; the cows count how many lights are on within that range.

Process the entire list and produce the correct count for every type-11 operation.

Input

  • Line 1: Two space-separated integers, NN and MM.
  • Lines 2…M+12 \ldots M+1: Each line describes one operation as three space-separated integers: the operation type, SS, and EE.

Output

  • For each type-11 (counting) operation, print the count as a single integer on its own line.

Hint

An example run with four lights and five commands:

          Lights
              1 2 3 4
  Init:       O O O O   O = off, * = on
  0 1 2  ->   * * O O   toggle lights 1 and 2
  0 2 4  ->   * O * *   toggle lights 2, 3, and 4
  1 2 3  ->   1         count lit lights in range 2..3
  0 2 4  ->   * * O O   toggle lights 2, 3, and 4
  1 1 4  ->   2         count lit lights in range 1..4

Examples3

  1. Example 1

    Input
    4 5
    0 1 2
    0 2 4
    1 2 3
    0 2 4
    1 1 4
    
    Expected output
    1
    2
    
  2. Example 2

    Input
    2 2
    0 1 2
    1 1 2
    
    Expected output
    2
    
  3. Example 3

    Input
    3 3
    0 1 3
    0 1 3
    1 1 3
    
    Expected output
    0