Codevs 1690 Switch Light segment tree + delay marker

Source: Internet
Author: User

1690 Switch Lighttime limit: 1 sspace limit: 128000 KBTitle 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 DescriptionLine 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 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 Sample Output1
2 Data range and hints 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

Idea: Segment tree into paragraph update, each time an interval changed to turn off the number of lights, each module 2 can be;

#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineMoD 1000000007#definePi (4*atan (1.0))Const intn=4e3+Ten, m=1e6+Ten, inf=1e9+Ten;intSum[m];intFlag[m];voidPushdown (intPosintLen) {    intlson=pos*2; intrson=pos*2+1; if(Flag[pos]) {Flag[lson]+=Flag[pos]; Flag[lson]%=2; Flag[rson]+=Flag[pos]; Flag[rson]%=2; Flag[pos]=0; Sum[lson]= (len-(len>>1))-Sum[lson]; Sum[rson]= (len>>1)-Sum[rson]; }}voidBuildtree (intLintRintPOS) {    if(l==R) {Sum[pos]=0; Flag[pos]=0; return; }    intMid= (l+r) >>1; Buildtree (L,mid,pos*2); Buildtree (Mid+1, r,pos*2+1);}intQueryintLintRintLintRintPOS) {    if(l<=l&&r<=R)returnSum[pos]; Pushdown (POS, (R-l+1)); intMid= (l+r) >>1; intans=0; if(L<=mid) Ans+=query (l,r,l,mid,pos*2); if(mid<r) Ans+=query (l,r,mid+1, r,pos*2+1); returnans;}voidUpdateintLintRintChangeintLintRintPOS) {    if(l<=l&&r<=R) {Flag[pos]+=Change ; Flag[pos]%=2; Sum[pos]= (r-l+1)-Sum[pos]; return; } pushdown (Pos,r-l+1); intMid= (L+R)/2; if(l<=mid) Update (l,r,change,l,mid,pos*2); if(r>mid) Update (l,r,change,mid+1, r,pos*2+1); Sum[pos]=sum[pos*2]+sum[pos*2+1];}intMain () {intx,y,z,i,t;  while(~SCANF ("%d%d",&x,&y)) { for(i=0; i<y;i++)        {            intFlag,l,r; scanf ("%d%d%d",&flag,&l,&R); if(flag) {printf ("%d\n", Query (L,r,1X1)); }            Else{update (l,r,1,1X1); }        }    }    return 0;}

Codevs 1690 Switch Light segment tree + delay marker

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.