code[vs]1690 switch lamp Time limit: 1 s space limit: 128000 KB title level: Diamonds Diamond
Topic description Description:Yyx House in front of the street has N (2<=n<=100000) lamp, before six o'clock in the evening, these lights are all closed, after six points, there will be M (2<=m<=100000) Individual press the switch, These switches can change the status of the light from the I lamp to the J lamp, and now Yyx want to know how much light is on from the X lamp to the y lamp (1<=i,j,x,y<=n)
Enter a description for input Description:Line 1th: Two integers separated by spaces N and M 2..m+1: Each line represents an action, there are three integers separated by spaces: instruction Number (0 for press switch, 1 for inquiry status), X and Y
outputs describe output Description:1th. Total number of queries: For each query, the result of the output query
sample Input to sample:4 50 1 20 2 41 2 30 2 41 1 4
sample output Sample outputs:12
Data Size & Hint:A total of 4 lights, 5 operations, the following is the status of each operation (x for closed, O for open): XXXX--Ooxx, Oxoo---Ooxx asked 1~4 —————————————————————— ————————————————————————
Analysis:The most violent idea of the subject is to use an array to simulate the operation of the range switch lights, but the amount of data is large, the idea of violence will time out. Consider using a segment tree with a delay tag. This problem belongs to the more obvious line tree problem. The following code: (Code comparison long ^_^)
1#include"bits/stdc++.h"2 3 #defineMAXN 1000104 5 using namespacestd;6typedefLong LongQaq;7 8 structTree9 {Ten intL, R; OneQaq sum;//the number of lights currently open A BOOLIdV//delay markers for switching lights - }; - the Qaq Min (Qaq A, Qaq b) - { - returna > B?b:a; - } + -Tree TR[MAXN <<2]; + A voidPush_down (intIintm) at { - if(TR[I].IDV)//The current node has a delay tag. - { -Tr[i <<1].idv =!tr[i <<1].idv;//left -Tr[i <<1|1].idv =!tr[i <<1|1].idv;//Right -Tr[i <<1].sum = Tr[i <<1].r-tr[i <<1].L +1-Tr[i <<1].sum;//so the light switch state is reversed inTr[i <<1|1].sum = Tr[i <<1|1].r-tr[i <<1|1].L +1-Tr[i <<1|1].sum;//Ibid . -Tr[i].idv =!tr[i].idv;//Clear 0!!! to } + } - the voidBuild_tree (intXintYinti) * { $TR[I].L = x;//Left EndpointPanax NotoginsengTR[I].R = y;//Right Endpoint - if(x = = y)return ; the Else + { AQaq mid = (tr[i].l + TR[I].R) >>1 ; theBuild_tree (x, Mid, I <<1);//left recursion +Build_tree (Mid +1, Y, I <<1|1);//right recursion - } $ } $ - voidUpdate_tree (intQintWinti) - { the if(w >= tr[i].r && q <= tr[i].l)//is fully contained - {WuyiTr[i].idv =!tr[i].idv;//delay Mark theQaq tot = tr[i].r-tr[i].l +1;//Total lights at current junction points -Tr[i].sum = Tot-tr[i].sum;//switch status all reversed Wu return ; - } About Else $ { -Push_down (i, TR[I].R-TR[I].L +1);//the information transfer function -Qaq mid = (tr[i].l + TR[I].R) >>1; - if(Q >mid) A { +Update_tree (q, W, I <<1|1); the } - Else if(W <=mid) $ { theUpdate_tree (q, W, I <<1); the } the Else the { -Update_tree (q, W, I <<1|1); inUpdate_tree (q, W, I <<1); the } theTr[i].sum = Tr[i <<1].sum + tr[i <<1|1].sum;//Backtracking Update About } the } the theQaq Query_tree (intQintWinti) + { - if(w >= TR[I].R && q <=tr[i].l) the {Bayi returnTr[i].sum;//The direct return value is fully contained the } the Else - { -Push_down (i, TR[I].R-TR[I].L +1 ); theQaq mid = (tr[i].l + TR[I].R) >>1; the if(Q >mid) the { the returnQuery_tree (q, W, I <<1|1); - } the Else if(W <=mid) the { the returnQuery_tree (q, W, I <<1);94 } the Else the { the returnQuery_tree (q, W, I <<1|1) + Query_tree (q, W, I <<1);98 } About } - }101 102 intMain ()103 {104 Qaq N, M; the intop, L, R;106scanf"%d%d", &n, &M);107Build_tree (1N1);//Build Operations108 while(m--)109 { thescanf"%d", &op);111 if( !op) the {113scanf"%d%d", &l, &R); theUpdate_tree (L, R,1);//Update Tree the } the Else117 {118scanf"%d%d", &l, &R);119printf"%lld\n", Query_tree (L, R,1));//Enquiry - }121 }122 return 0 ;123}
Finish
code[vs]1690 Switch Lamp