1230: [Usaco2008 nov]lites switch lamp

Source: Internet
Author: User

1230: [Usaco2008 nov]lites switch Light time limit:10 Sec Memory limit:162 MB
submit:1162 solved:589
[Submit] [Status] Description

Farmer John tries to keep his cows mentally agile by playing educational toys with the cows. One of the big toys is the lamp in the barn. N (2 <= n <= 100,000) Each head of a cow is consecutively numbered 1. N, standing under a colored lamp. All the lights were off when it was late in the evening. The cows use n buttons to control the light switch; Press the I button to change the status of the I light. Cows Execute m (1 <= m <= 100,000) instructions, each of which is one of two integers (0 <= instruction number <= 1). The 1th directive (denoted by 0) contains two digital s_i and e_i (1 <= s_i <= e_i <= N), which represent the start and stop switches. The cows just need to press the button between S_i and e_i to complete this instruction. The 2nd directive (denoted by 1) also contains two digits s_i and e_i (1 <= s_i <= e_i <= N), but this instruction asks how much of the light from s_i to e_i is lit. Help FJ to make sure his cows get the right answer.

Input

* Line 1th: Two integers separated by a space N and M

* Line 2..m+1: Each line represents an action, there are three integers separated by spaces: instruction number, s_i, and E_i

Output

1th: The number of times to ask for the result of the query, for each query.

Sample Input4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

Input explanation:
A total of 4 lamps; 5 instructions. The following are the implementation scenarios:
Lights
1 2 3 4
Init:o o o o = off * = On
0 1 2 * * o o change the status of Lights 1 and 2
0 2 4 * * O * *
1 2 3-1 output in 2.. How many lights are lit in the range of 3
0 2 4 * * o O change the status of Lights 2, 3 and 4
1 1 4-2 output in 1.. How many lights are lit in the range of 4




Sample Output1
2
HINT Source

Gold

Puzzle: This is a more interesting line-segment tree problem-the general interval plus a number into the interval reversal, but it is also difficult to how many, or the same dozen Lazytag, the other did not (phile: It seems you this program and lazy crazy with ext push AH hehe hansbug: that is, But actually do not have to seem to be able to engage, just a little more.

1 var2 I,j,k,l,m,n:longint;3B:Array[0..1000000] ofLongint;4 functionMax (x,y:longint): Longint;inline;5          begin6               ifX>y ThenMax:=xElsemax:=y;7          End;8 functionmin (x,y:longint): Longint;inline;9          beginTen               ifX<y ThenMin:=xElsemin:=y; One          End; A procedurebuilt (z,x,y:longint), inline; -           begin -                ifX=y Then thea[z]:=0 -                Else -                    begin -Built (z*2, X, (X+y)Div 2); +Built (z*2+1, (X+y)Div 2+1, y); -a[z]:=a[z*2]+a[z*2+1]; +                    End; Ab[z]:=0; at           End; - procedureext (z,x,y:longint); inline; -           begin -                ifb[z]=0  Thenexit; -A[z]:= (y-x+1)-A[z]; -                ifX<>y Then in                   begin -b[z*2]:=1-b[z*2]; tob[z*2+1]:=1-b[z*2+1]; +                   End; -b[z]:=0; the           End; * functionop (z,x,y,l,r:longint): Longint;inline; $          varA1,a2,a3,a4:longint;Panax Notoginseng          begin -               ifL>r ThenExit0); the ext (z,x,y); +               if(x=l) and(Y=R) Then A                  begin theb[z]:=1; +Exit (((r-l+1)-a[z])-a[z]); -                  End; $A2:=op (z*2, X, (X+y)Div 2, L,min (R, (x+y)Div 2)); $A3:=op (z*2+1, (X+y)Div 2+1, Y,max (L, (x+y)Div 2+1), R); -a[z]:=a[z]+a2+A3; -Exit (a2+A3); the          End; - functioncal (Z,x,y,l,r:longint): Longint;inline;Wuyi          varA1,a2,a3,a4:longint; the          begin -               ifL>r ThenExit0); Wu ext (z,x,y); -               if(x=l) and(Y=R) Thenexit (A[z]); AboutExit (Cal (z*2, X, (X+y)Div 2, L,min (R, (x+y)Div 2)) +cal (z*2+1, (X+y)Div 2+1, Y,max (L, (x+y)Div 2+1) , R)); $          End; - begin - readln (n,m); -Built1,1, n); A       fori:=1  toM Do +          begin the readln (j,k,l); -                CaseJ of $                    0:begin theOp1,1, n,k,l); the                    End; the                    1:begin theWriteln (Cal (1,1, n,k,l)); -                    End; in               End; the          End; the Readln; About End. the         

1230: [Usaco2008 nov]lites switch lamp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.