Sphere Online Judge

SPOJ Problem Set (classical)

7259. Light Switching

Problem code: LITE



Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of the larger toys is the lights in the barn.  Each of the N (2 <= N <= 100,000) cow stalls conveniently numbered 1..N has a colorful light above it.

At the beginning of the evening, all the lights are off. The cows control the lights with a set of N pushbutton switches that toggle the lights; pushing switch i changes the state of light i from off to on or from on to off.

The cows read and execute a list of M (1 <= M <= 100,000) operations expressed as one of two integers (0 <= operation <= 1).

The first kind of operation (denoted by a 0 command) includes two subsequent integers S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting switch and ending switch. They execute the operation by pushing each pushbutton from S_i through E_i inclusive exactly once.

The second kind of operation (denoted by a 1 command) asks the cows to count how many lights are on in the range given by two integers S_i and E_i (1 <= S_i <= E_i <= N) which specify the inclusive range in which the cows should count the number of lights that are on.

Help FJ ensure the cows are getting the correct answer by processing the list and producing the proper counts.

Input


Line 1: Two space-separated integers: N and M
Lines 2..M+1: Each line represents an operation with three space-separated integers: operation, S_i, and E_i

Output

Lines 1..number of queries: For each output query, print the count as an integer by itself on a single line.

Example

Input:
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

Output:
1
2

Added by:Iqram Mahmud
Date:2010-09-03
Time limit:1s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All
Resource:USACO November 08

hide comments
2014-10-22 07:39:59 Umair Khan
Anyone going wrong at 10th test case, update the lazy node everywhere in the update method (I hope you get it).

Corner case is when you are going to mark a lazy node again to be lazy.
2014-10-07 23:29:00 caesar
getting wrong answer on Test case 10. Any suggestion
http://ideone.com/bz0TqH
Used Segment tree along with lazy propagation

Last edit: 2014-10-07 23:30:14
2014-07-08 21:18:54 maniAC
Finally got AC after few TLE, just optimize the program using inline, TL should be 2.0 second :(.
2014-07-01 10:40:28 Archit Jain
nice question
lazy optimization....
2014-04-16 16:25:08 Atul Vaibhav
i just copy-pasted my FLIPCOIN solution and got WA! but then read the problem details again "carefully" and got AC!
Learned to read problems carefully atleast once before submitting! ;)
2014-04-05 20:25:11 C. Sai Prajeeth
Same here..AC on FLIPCOINS and WA here
2014-02-02 18:15:26 Adamos Ttofari
I solved the Flipping Coins in CodeChef
Why I get WA here?
2014-01-01 21:57:43 ABHISHEK004
phew...!!
after lots of tle finally acc.. :)
learnt new thing about lazy propogation
2013-12-04 17:13:55 Praneeth Reddy
TLE for 10th test case???
where is the mistake???
ca anybody explain?
2013-02-23 06:55:50 chansiddharth
@Sivaraman Nagarajan - ur algo wud never pass . Check the worst case time limit .
This can be solved by Segment tree with lazy propagation !
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.