HDU 3911 black and white segmentation tree

Source: Internet
Author: User

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

Segmentation tree is still a problem. This question can be said to be the basic operation of the segmentation tree. However, due to the many situations, the program is very long, so it takes a lot of time.

Instead of using the general dynamic programming method, we need to reduce the time efficiency of each query to (lgn ).

Segment: each segment requires 8 data records, which is very cumbersome.

The Code has been modified many times, and the code is clearer than the hacker. It is not difficult for people with segmented tree ideas to follow.

HDU does not agree to use free to release space. Otherwise, an error occurs, strange OJ.

# Pragma once # include <stdio. h> # include <stdlib. h> # include <algorithm> # include <math. h> using namespace STD; Class blackandwhite3911 {int arrsize, tsize; int * arr; struct node {int le0, le1, ri0, Ri1; int len0, len1; int totallen; bool lazy;}; node * segtree; void updatenode (int r, int L, int R) {If (segtree [l]. le0 = segtree [l]. totallen) segtree [R]. le0 = segtree [l]. le0 + segtree [R]. le0; elsesegtree [R]. le0 = segtr EE [l]. le0; If (segtree [R]. ri0 = segtree [R]. totallen) segtree [R]. ri0 = segtree [l]. ri0 + segtree [R]. ri0; else segtree [R]. ri0 = segtree [R]. ri0; If (segtree [l]. le1 = segtree [l]. totallen) segtree [R]. le1 = segtree [l]. le1 + segtree [R]. le1; else segtree [R]. le1 = segtree [l]. le1; If (segtree [R]. ri1 = segtree [R]. totallen) segtree [R]. ri1 = segtree [l]. ri1 + segtree [R]. ri1; else segtree [R]. ri1 = segtree [R]. ri1; int = Max (segtree [l]. len0, segtree [R]. len0); int B = segtree [l]. ri0 + segtree [R]. le0; segtree [R]. len0 = max (A, B); A = max (segtree [l]. len1, segtree [R]. len1); B = segtree [l]. ri1 + segtree [R]. le1; segtree [R]. len1 = max (a, B);} void conhelper (INT low, int up, int r = 0) {If (Low = up) {If (0 = arr [low]) {segtree [R]. len0 = 1, segtree [R]. len1 = 0; segtree [R]. le0 = 1, segtree [R]. ri0 = 1; segtree [R]. le1 = 0, segtree [R]. ri1 = 0;} else {segtree [R]. len0 = 0, segtree [R]. len1 = 1; segtree [R]. le0 = 0, segtree [R]. ri0 = 0; segtree [R]. le1 = 1, segtree [R]. ri1 = 1;} segtree [R]. lazy = false; segtree [R]. totallen = 1; return;} int mid = low + (up-low)> 1); int Le = (r <1) + 1; int rI = (r <1) + 2; conhelper (low, mid, le); conhelper (Mid + 1, up, RI); segtree [R]. totallen = up-low + 1; updatenode (R, le, RI); segtree [R]. lazy = false;} void Contree () {int H = (INT) Ceil (log (double) arrsize)/log (2.0) + 1; tsize = (INT) Pow (2.0, H) -1; segtree = (node *) malloc (sizeof (node) * tsize); conhelper (0, arrSize-1);} void accessnode (int r) {segtree [R]. lazy =! Segtree [R]. lazy; swap (segtree [R]. le0, segtree [R]. le1); swap (segtree [R]. ri0, segtree [R]. ri1); swap (segtree [R]. len0, segtree [R]. len1);} void segupdate (const int low, const int up, int L, int R, int r = 0) {If (Low = L & R = up) {accessnode (r); return;} int Le = (r <1) + 1; int rI = (r <1) + 2; If (segtree [R]. lazy) {segtree [R]. lazy = false; If (Le <tsize) accessnode (LE); If (RI <tsize) accessnode (RI);} int M = L + (R-L)> 1); If (up <= m) segupdate (low, up, L, M, le); else if (low> m) segupdate (low, up, m + 1, R, RI); else {segupdate (low, M, L, M, le); segupdate (m + 1, up, m + 1, R, RI);} updatenode (R, le, RI);} int getlongest (const int low, const int up, int L, int R, int r = 0) {If (Low = L & R = up) // It cannot be low <= L & R <= up {return segtree [R]. len1;} int Le = (r <1) + 1; int rI = (r <1) + 2; If (segtree [R]. lazy) {se Gtree [R]. lazy = false; If (Le <tsize) accessnode (LE); If (RI <tsize) accessnode (RI);} int M = L + (R-L)> 1); // If (up <= m) return getlongest (low, up, L, M, le) on any side of the subtree; If (low> m) return getlongest (low, up, m + 1, R, RI); // int llen = getlongest (low, M, L, M, le) across left and right subtree ); // (low, up, L, M, le); int rlen = getlongest (m + 1, up, m + 1, R, RI); // (low, up, m + 1, R, RI); int A = min (m-low + 1, segtree [le]. ri1); int B = Min (up-M, segtree [ri]. le1); int c = a + B; return max (C, max (llen, rlen);} public: blackandwhite3911 () {int N; while (scanf ("% d", & N )! = EOF) {arrsize = N; arr = (int *) malloc (sizeof (INT) * arrsize); For (INT I = 0; I <n; I ++) {scanf ("% d", & arr [I]) ;}contree (); int t; scanf ("% d", & T); int A, B, c; while (t --) {scanf ("% d", & A, & B, & C); If (0 =) printf ("% d \ n", getlongest (B-1, C-1, 0, arrSize-1); else segupdate (B-1, C-1, 0, arrSize-1 );}}}~ Blackandwhite3911 () {If (ARR) Free (ARR); If (segtree) Free (segtree );}};



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.