"BZOJ1230" [Usaco2008 nov]lites switch Light segment tree

Source: Internet
Author: User

Recursive do not add inline will be a magic error .....

1#include <iostream>2#include <cstdio>3 #defineN 1000104 using namespacestd;5 structTree6 {7     intSize,rev,l,r;8}seg[n*4];9 intn,m;TenInlineintRead () One { A     CharC; -     intans=0, f=1; -      while(!isdigit (C=getchar ()))if(c=='-') f=-1; theans=c-'0'; -      while(IsDigit (C=getchar ())) ans=ans*Ten+c-'0'; -     returnans*F; - } +InlinevoidPush_down (intx) - { + //void Push_up (); A     //if ((x>>1) +1>n*4) return; at     if(Seg[x].rev) -     { -seg[(x<<1)].rev^=1; -seg[(x<<1)+1].rev^=1; -         //Swap (seg[(x<<1)].open,seg[(x<<1)].close); -         //Swap (seg[(x<<1) +1].open,seg[(x<<1) +1].close); inseg[(x<<1)].size=seg[(x<<1)].r-seg[(x<<1)].l+1-seg[(x<<1)].size; -seg[(x<<1)+1].size=seg[(x<<1)+1].r-seg[(x<<1)+1].l+1-seg[(x<<1)+1].size; toseg[x].rev=0; +     //push_up (x); -     } the } * voidBuildtree (intNowintLintR) $ {Panax NotoginsengSeg[now].l=l,seg[now].r=R; -     if(l==R) the     { +Seg[now].size=0; Aseg[now].rev=0; the         return; +     } -     intMid= (l+r) >>1; $Buildtree ((now<<1), l,mid); $Buildtree ((now<<1)+1, mid+1, R); -     //seg[now].size=seg[(now<<1) +1].size+seg[(now<<1) +1].size; - } the voidReverse (intNowintLintRintBeginintend) - {Wuyi     if(L>=begin && r<=end) the     { -seg[now].rev^=1; Wuseg[now].size=seg[now].r-seg[now].l+1-seg[now].size; -         //swap (seg[now].close,seg[now].open); About         //Push_down (now); $         return; -     } - Push_down (now); -     intMid= (l+r) >>1; A     if(Begin<=mid) Reverse ((now<<1), l,mid,begin,end); +     if(End>mid) Reverse ((now<<1)+1, mid+1, r,begin,end); theseg[now].size=seg[(now<<1)].size+seg[(now<<1)+1].size; -     //push_up (now); $ } the intQuery (intNowintLintRintBeginintend) the { the     if(L>=begin && R<=end)returnseg[now].size; the     intMid= (l+r) >>1, ans=0; - Push_down (now); in     if(Begin<=mid) Ans+=query (now<<1), l,mid,begin,end); the     if(End>mid) Ans+=query (now<<1)+1, mid+1, r,begin,end); the     returnans; About } the intMain () the { the     intx, y, z +     //scanf ("%d%d", &n,&m); -N=read (); m=read (); theBuildtree (1,1, n);Bayi      for(intI=1; i<=m;i++) the     { theX=read (); Y=read (); z=read (); -         if(x) printf ("%d\n", Query (1,1, N,y,z)); -         ElseReverse (1,1, n,y,z); the     }     the     return 0; the}
View Code 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.

Input

* 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, s_i, and E_i

Output

1th: The number of times to ask for the result of the query, for each query.

Sample Input4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

Input explanation:
A total of 4 lamps; 5 instructions. The following are the implementation scenarios:
Lights
1 2 3 4
Init:o o o o = off * = On
0 1 2 * * o o change the status of Lights 1 and 2
0 2 4 * * O * *
1 2 3-1 output in 2.. How many lights are lit in the range of 3
0 2 4 * * o O change the status of Lights 2, 3 and 4
1 1 4-2 output in 1.. How many lights are lit in the range of 4




Sample Output1
2
HINT Source

"BZOJ1230" [Usaco2008 nov]lites switch Light segment tree

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.