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)
Input line 1th: two integers separated by spaces N and M 2..m+1: Each line represents an action with three integers separated by spaces: instruction Number (0 for press switch, 1 for inquiry status), X and Y Output
1th. Total number of queries: For each query, the result of the output query
Sample Input
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
Sample OUTPUT1
2 HINT
There are 4 lights, 5 operations, the following is the status of each operation (x for closed, O for open):
Ooxx, Oxoo, XXXX---Ooxx inquiry 1~4
Exercises
Segment tree, interval change state = interval Size-interval and
#include <iostream>#include<cstdio>#defineN 100010using namespacestd;structtree{intL,r; intLC,RC; intSUM,BJ;} T[n*4];intCur,tot;voidBuildintLintR) { intcur = ++tot;//.....//int k = cur;T[cur]. L=l; T[cur]. R=R; if(r!=l+1) {t[cur].lc= tot+1; Build (L, (l+R)/2); T[cur].rc= tot+1;//Build ((L+R)/2, R);//T[cur].sum=t[t[cur].lc].sum+t[t[cur].rc].sum;//}Elset[cur].sum=0;//}voidUpdata (intk) {T[T[K].LC].BJ+=1; t[t[k].rc].bj+=1; T[t[k].lc].sum= T[t[k].lc]. R-T[T[K].LC]. L-t[t[k].lc].sum; T[t[k].rc].sum= T[t[k].rc]. R-T[T[K].RC]. L-t[t[k].rc].sum; T[K].BJ=0;}intQueryintKintllintRR) { if(T[k]. L>=ll && T[k]. R<=RR)returnt[k].sum; if(t[k].bj%2!=0) Updata (k); intans=0; intMID = (T[k]. L+T[K]. R)/2; if(Ll<mid) ans+=query (T[K].LC,LL,RR); if(Rr>mid) ans+=query (T[K].RC,LL,RR); returnans;}voidChange (intKintllintRR) { if(T[k]. L>=ll && T[k]. r<=RR) {T[K].BJ++; T[k].sum= T[k]. R-T[K]. L-t[k].sum; return ; } if(t[k].bj%2!=0) Updata (k); intMID = (T[k]. L+T[K]. R)/2; if(ll<mid) Change (T[K].LC,LL,MID); if(rr>mid) Change (T[K].RC,MID,RR); T[k].sum= T[t[k].lc].sum +t[t[k].rc].sum;}intMain () {intN,m,pd,l,r; scanf ("%d%d",&n,&m); Build (1, n+1); for(intI=1; i<=m;i++) {scanf ("%d",&PD); scanf ("%d%d",&l,&R); if(PD) printf ("%d\n", Query (1, l,r+1)); ElseChange (1, l,r+1); }}
"CodeVS1690" Switch light