Segment tree Interval Update HDU 3911

Source: Internet
Author: User

Segment tree Interval Update HDU 3911
Black And WhiteTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3811 Accepted Submission (s): 1129


Problem DescriptionThere are a bunch of stones on the beach; Stone color is white or black. little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. little Sheep like black very much, so she want to know the longest period of consecutive black stones in a range [I, j].
Input There are multiple cases, the first line of each case is an integer n (1 <=n <= 10 ^ 5 ), followed by n integer 1 or 0 (1 indicates black stone and 0 indicates white stone), then is an integer M (1 <= M <= 10 ^ 5) followed by M operations formatted as x I j (x = 0 or 1), x = 1 means change the color of stones in range [I, j], and x = 0 means ask the longest period of consecutive black stones in range [I, j]
OutputWhen x = 0 output a number means the longest length of black stones in range [I, j].
Sample Input

41 0 1 050 1 41 2 30 1 41 3 30 4 4

Sample Output
120

Source2011 Multi-University Training Contest 8-Host by HUST interval Interval Update. Note that the data has been written in the code.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; # define MAXN 100000 + 1000 struct node {int lsum0, rsum0, msum0, lsum1, rsum1, msum1; int ck; int l, r; int mid () {return (l + r)> 1 ;}} tree [MAXN <2]; int a [MAXN]; void PushUp (int rt) {int ll = tree [rt <1]. r-tree [rt <1]. l + 1; // The length of the Left subtree interval int rr = tree [rt <1 | 1]. r-tree [rt <1 | 1]. l + 1; // The length of the right subtree Interval tree [rt]. lsum1 = tree [rt <1]. lsum1; if (tree [rt <1]. lsum1 = ll) {tree [rt]. lsum1 = tree [rt]. lsum1 + tree [r T <1 | 1]. lsum1;} tree [rt]. rsum1 = tree [rt <1 | 1]. rsum1; if (tree [rt <1 | 1]. rsum1 = rr) {tree [rt]. rsum1 = tree [rt]. rsum1 + tree [rt <1]. rsum1;} tree [rt]. lsum0 = tree [rt <1]. lsum0; if (tree [rt <1]. lsum0 = ll) {tree [rt]. lsum0 + = tree [rt <1 | 1]. lsum0;} tree [rt]. rsum0 = tree [rt <1 | 1]. rsum0; if (tree [rt <1 | 1]. rsum0 = rr) {tree [rt]. rsum0 + = tree [rt <1]. rsum0;} tree [rt]. msum0 = max (tree [rt <1]. msum0, tree [rt <1 | 1]. msum0), tree [rt <1]. rsum0 + tree [rt <1 | 1]. lsum0); tree [rt]. msum1 = max (tree [rt <1]. msum1, tree [rt <1 | 1]. msum1), tree [rt <1]. rsum1 + tree [rt <1 | 1]. lsum1);} void PushDown (int rt) {if (tree [rt]. ck = 1) {tree [rt <1]. ck ^ = 1; // now all 01 values of the Left subtree at Father's Day need to be exchanged, so the left subtree also needs to be exchanged, so the left and right subtree also needs to be exchanged, if the left subtree is used, it is required to switch tree [rt <1 | 1]. ck ^ = 1; //, after two exchanges, it is equivalent to not needing to be exchanged. In this way, the tree [rt] is not required after an exception or exchange. ck = 0; swap (tree [rt <1]. lsum0, tree [rt <1]. lsum1 ); Swap (tree [rt <1]. rsum0, tree [rt <1]. rsum1); swap (tree [rt <1]. msum0, tree [rt <1]. msum1); swap (tree [rt <1 | 1]. lsum0, tree [rt <1 | 1]. lsum1); swap (tree [rt <1 | 1]. rsum0, tree [rt <1 | 1]. rsum1); swap (tree [rt <1 | 1]. msum0, tree [rt <1 | 1]. msum1) ;}} void build (int l, int r, int rt) {tree [rt]. l = l; tree [rt]. r = r; tree [rt]. ck = 0; // do not convert if (l = r) {if (a [l] = 0) {tree [rt] at the beginning. lsum0 = tree [rt]. rsum0 = tree [rt]. msum0 = 1; tree [Rt]. lsum1 = tree [rt]. rsum1 = tree [rt]. msum1 = 0;} else {tree [rt]. lsum1 = tree [rt]. rsum1 = tree [rt]. msum1 = 1; tree [rt]. lsum0 = tree [rt]. rsum0 = tree [rt]. msum0 = 0;} return;} // int m = (l + r)> 1; int m = tree [rt]. mid (); build (l, m, rt <1); build (m + 1, r, rt <1 | 1); PushUp (rt );} void update (int l, int r, int rt) {if (l <= tree [rt]. l & tree [rt]. r <= r) {tree [rt]. ck ^ = 1; swap (tree [rt]. lsum0, tree [rt]. lsum1); swap (tree [rt]. rsum0, Tree [rt]. rsum1); swap (tree [rt]. msum0, tree [rt]. msum1); return;} PushDown (rt); int m = tree [rt]. mid (); if (r <= m) {update (l, r, rt <1);} else if (l> m) {update (l, r, rt <1 | 1);} else {update (l, m, rt <1); update (m + 1, r, rt <1 | 1 );} pushUp (rt);} int query (int l, int r, int rt) {if (l <= tree [rt]. l & tree [rt]. r <= r) {return tree [rt]. msum1 ;}pushdown (rt); int m = tree [rt]. mid (); if (r <= m) {return query (l, r, rt <<1);} else if (l> m) {return query (l, r, rt <1 | 1);} else {int lr = query (l, m, rt <1); int rr = query (m + 1, r, rt <1 | 1); int a = tree [rt <1]. rsum1; // The longest consecutive 1 if (a> tree [rt <1]. r-l + 1) {// The following one is the maximum range of a = tree [rt <1]. r-l + 1;} int B = tree [rt <1 | 1]. lsum1; if (B> r-tree [rt <1 | 1]. l + 1) {B = r-tree [rt <1 | 1]. l + 1;} return max (lr, rr), a + B) ;}} int main () {int I; int n, m, op, aa, b; while (~ Scanf ("% d", & n) {for (I = 1; I <= n; I ++) {scanf ("% d ", & a [I]);} build (1, n, 1); scanf ("% d", & m); while (m --) {scanf ("% d", & op, & aa, & B); if (op = 0) {int ans = query (aa, B, 1); printf ("% d \ n", ans);} else {update (aa, B, 1) ;}} return 0 ;}
      
     
    
   
  
 

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.