Topic Connection: http://www.spoj.com/problems/LITE/en/.
Test instructions: There are a number of light bulbs, each time for a period of operation, the paragraph was originally bright, closed, originally closed, opened. Ask about the number of open bulbs in a certain section.
Analysis: It is obvious that the paragraph is updated, but at first think of a paragraph is not the same to maintain, knocking for a long time have not been realized. Later after the strong seniors know that there are lazy labeled nodes of the light bulb and so update can:C[o]=r-l+1-c[o]; What a mystery!
In addition, it can be seen from this question, I do not understand the adulthood paragraph update lazy mark deep enough. It should be understood as follows: when a pushdown operation is performed on a point, this sum value is not updated, and the sum and add values of the child nodes are updated .
See the code for details:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4#include <Set>5#include <iostream>6 #defineT_mid (l+r>>1)7 #defineLS (o<<1)8 #defineRS (o<<1 | 1)9 #defineLson Ls,l,t_midTen #defineRson Rs,t_mid+1,r One using namespacestd; A Const intN =100000+5; - - intn,m,c[n<<2]; the BOOLadd[n<<2]; - voidBuildintOintLintR) - { -c[o]=0, add[o]=0; + if(L==R)return; - build (Lson); + build (Rson); A } at voidPushdown (intOintlen) - { - if(Add[o]) - { -C[ls]= (len-(len>>1))-C[ls]; -C[rs]= (len>>1)-C[rs]; inadd[ls]^=1; -add[rs]^=1; toadd[o]=0; + } - } the voidPushup (into) * { $C[o] = C[ls] +C[rs];Panax Notoginseng } - voidUpdateintOintLintRintQlintqr) the { + if(Ql<=l && qr>=R) A { theadd[o]^=1; +C[o] = r-l+1-C[o]; - return; $ } $Pushdown (o,r-l+1); - if(ql<=t_mid) Update (LSON,QL,QR); - if(qr>t_mid) Update (RSON,QL,QR); the - pushup (o);Wuyi } the intQueryintOintLintRintQlintqr) - { Wu if(Ql<=l && qr>=r)returnC[o]; -Pushdown (o,r-l+1); About intres =0; $ if(ql<=t_mid) Res + =query (LSON,QL,QR); - if(qr>t_mid) Res + =query (RSON,QL,QR); - returnRes; - } A + intMain () the { - while(SCANF ("%d%d", &n,&m) = =2) $ { theBuild1,1, n); the the while(m--) the { - intop,x,y; inscanf"%d%d%d",&op,&x,&y); the if(!OP) Update (1,1, n,x,y); the Elseprintf"%d\n", Query (1,1, N,x,y)); About } the } the return 0; the}
Light switching (Spoj LITE)--Update XOR values for segment tree segments