"Segment Tree" Bzoj1230 [Usaco2008 nov]lites switch Light

Source: Internet
Author: User

Description

Farmer John tries to keep his cows mentally agile by playing educational toys with the cows. One of the big toys is the lamp in the barn. N (2 <= n <= 100,000) Each head of a cow is consecutively numbered 1. N, standing under a colored lamp. All the lights were off when it was late in the evening. The cows use n buttons to control the light switch; Press the I button to change the status of the I light. Cows Execute m (1 <= m <= 100,000) instructions, each of which is one of two integers (0 <= instruction number <= 1). The 1th directive (denoted by 0) contains two digital s_i and e_i (1 <= s_i <= e_i <= N), which represent the start and stop switches. The cows just need to press the button between S_i and e_i to complete this instruction. The 2nd directive (denoted by 1) also contains two digits s_i and e_i (1 <= s_i <= e_i <= N), but this instruction asks how much of the light from s_i to e_i is lit. Help FJ to make sure his cows get the right answer.

Solution

Record two volume tag and sum, maintain with lazy.

Brush the Water + Review the line tree.

Code

Lazy processing ancestors to the descendants of two methods, recursive time record or every time pushdown, the former constant small but the latter is more brain-free error.

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 Const intmaxn=4e5+5;5 6 intTAG[MAXN],SUM[MAXN];7 intn,m;8 intX,p,q,ret;9 Ten intPushup (into) { Onesum[o]=sum[o*2]+sum[o*2+1]; A } -  - intPushdown (intOintLintR) { the     intMid= (l+r) >>1; -tag[o]=0; -tag[o*2]^=1, tag[o*2+1]^=1; -sum[o*2]= (mid-l+1-sum[o*2]); +sum[o*2+1]= (r-mid-sum[o*2+1]); - } +  A voidAddintOintLintR) { at     if(p<=l&&r<=q) { -tag[o]^=1; -Sum[o]= (r-l+1-Sum[o]); -         return; -     } -     if(Tag[o]) pushdown (o,l,r); in     intMid= (l+r) >>1; -     if(p<=mid) Add (o*2, l,mid); to     if(q>mid) Add (o*2+1, mid+1, R); + pushup (o); - } the  * voidAskintOintLintRintk) { $     if(p<=l&&r<=q) {Panax Notoginseng         if(!k) ret+=Sum[o]; -         Elseret+= (r-l+1-Sum[o]); the         return; +     } A     intMid= (l+r) >>1; the     if(P<=mid) Ask (o*2, l,mid,k^Tag[o]); +     if(Q>mid) Ask (o*2+1, mid+1, r,k^Tag[o]); - } $  $ intMain () { -scanf"%d%d",&n,&m); -      for(intI=1; i<=m;i++){ thescanf"%d%d%d",&x,&p,&q); -         if(x==0) Add (1,1, n);Wuyi         Else{ theret=0; -Ask1,1N0); Wuprintf"%d\n", ret); -         } About     } $     return 0; -}

Pushdown code every time

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 Const intmaxn=4e5+5;5 6 intTAG[MAXN],SUM[MAXN];7 intn,m;8 intX,p,q,ret;9 Ten intPushup (into) { Onesum[o]=sum[o*2]+sum[o*2+1]; A } -  - intPushdown (intOintLintR) { the     intMid= (l+r) >>1; -tag[o]=0; -tag[o*2]^=1, tag[o*2+1]^=1; -sum[o*2]= (mid-l+1-sum[o*2]); +sum[o*2+1]= (r-mid-sum[o*2+1]); - } +  A voidAddintOintLintR) { at     if(p<=l&&r<=q) { -tag[o]^=1; -Sum[o]= (r-l+1-Sum[o]); -         return; -     } -     if(Tag[o]) pushdown (o,l,r); in     intMid= (l+r) >>1; -     if(p<=mid) Add (o*2, l,mid); to     if(q>mid) Add (o*2+1, mid+1, R); + pushup (o); - } the  * voidAskintOintLintR) { $     if(p<=l&&r<=q) {Panax Notoginsengret+=Sum[o]; -         return; the     } +     if(Tag[o]) pushdown (o,l,r); A     intMid= (l+r) >>1; the     if(P<=mid) Ask (o*2, l,mid); +     if(Q>mid) Ask (o*2+1, mid+1, R); - pushup (o); $ } $  - intMain () { -scanf"%d%d",&n,&m); the      for(intI=1; i<=m;i++){ -scanf"%d%d%d",&x,&p,&q);Wuyi         if(x==0) Add (1,1, n); the         Else{ -ret=0; WuAsk1,1, n); -printf"%d\n", ret); About         } $     } -     return 0; -}
View Code

"Segment Tree" Bzoj1230 [Usaco2008 nov]lites 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.