Segment Tree--codevs 1690 switch Light

Source: Internet
Author: User

First, a topic:

1690 Switch Light

Time limit: 1 s

Space limit: 128000 KB

Title 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 Input Description

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 (0 for press switch, 1 for inquiry status), X and Y

Output Description Output Description

1th. Total number of queries: For each query, the result of the output query

Sample Input Sample Input

4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
Sample Output sampleoutput

1
2



data range and tips Data Size & 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

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN 100000+106 structnode7   {8       intL,r;9       intVal;Ten       intD; One}a[4*N]; A intn,m,p,x,y; -  - voidUpdateint ); the voidBuildtree (int,int,int); - voidDownint); - voidAddint,int,int,int,int); - intQueryint,int,int,int,int); +  - intMain () +   { Ascanf"%d%d",&n,&m); atBuildtree (1,1, n); -        for(intI=1; i<=m;i++) -       { -scanf"%d%d%d",&p,&x,&y); -         if(p==0) -           { inAdd1,1, n,x,y); -           } to         Elseprintf"%d\n", Query (1,1, N,x,y)); +       } -     return 0; the   } * voidUpdateintk) $ {Panax Notoginseng     intlch=k*2, Rch= (k*2)+1; -a[k].val=a[lch].val+A[rch].val; the } + voidBuildtree (intKintLintR) A { theA[k].l=l;a[k].r=R; +     if(l==R) -     { $A[k].val=a[k].d=0; $         return ; -     } -     intMid= (L+R)/2, lch=k*2, Rch= (k*2)+1; the Buildtree (lch,l,mid); -Buildtree (rch,mid+1, R);Wuyi update (k); the  } - voidDownintk) Wu { -     intlch=k*2, Rch= (k*2)+1; Abouta[lch].d+=a[k].d; $a[lch].d%=2; -A[lch].val= (a[lch].r-a[lch].l+1)-A[lch].val; -a[rch].d+=a[k].d; -a[rch].d%=2; AA[rch].val= (a[rch].r-a[rch].l+1)-A[rch].val; +A[k].d=0; the } - voidAddintKintLintRintXinty) $ { the     if(x<=l&&r<=y) the     { thea[k].d++; thea[k].d%=2; -A[k].val= (r-l+1)-A[k].val; in         return; the     } the     if(A[K].D) About Down (k); the     intMid= (L+R)/2, lch=k*2, Rch= (k*2)+1; the     if(x<=mid) the Add (lch,l,mid,x,y); +     if(y>mid) -Add (rch,mid+1, r,x,y); the update (k);Bayi } the intQueryintKintLintRintXinty) the { -     if(x<=l&&r<=y) -     { the         returnA[k].val; the     } the     if(A[K].D) the Down (k); -     intMid= (L+R)/2, lch=k*2, Rch= (k*2)+1; the     intsum=0; the     if(x<=mid) thesum+=query (lch,l,mid,x,y);94     if(y>mid) theSum+=query (rch,mid+1, r,x,y); the     returnsum;  the}
View Code

Segment Tree--codevs 1690 switch Light

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.