HDU 3911 Black and White (segment tree interval merge)

Source: Internet
Author: User

Problem Descriptionthere is a bunch of stones on the beach; Stone color is white or black. Little Sheep have 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 was multiple cases, the first line of each case was an integer n (1<= n <= 10^5), followed by n integer 1 or 0 (1 indicates black stone and 0 indicates white stone), then was an integer M (1<=m<=10^5) followed by M operation s 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 perio D 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
segment Tree interval merging: for RS corresponding to each interval [l,r], we give 6 quantities:lmax_1: The maximum prefix of 1 from the left, Lmax_0: The maximum prefix of 0 from the left;rmax_1: The maximum suffix of 1 from right up rmax_0: The maximum suffix of 0 on the right;max_1: The maximum continuous value of 1 of the interval, max_0: The maximum continuous value of 0 of the intervalour task is to continually update the evaluation.
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack>  #include <bitset>using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define REP (i, n ) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef long long Ll;typedef pair& lt;int,int>pil;const int mod = 1000000007;const int maxn=1e5+10;int lmax_1[maxn<<2],rmax_1[maxn<<2]; int Lmax_0[maxn<<2],rmax_0[maxn<<2];int Max_1[maxn<<2],max_0[maxn<<2];int col[maxn< <2];int n,m,os;void pushup (int rs,int m) {lmax_1[rs]=lmax_1[rs<<1];//handles the left class prefix lmax_0[rs]=lmax_0[rs<<1    ];    if (lmax_1[rs<<1]==m-(m>>1)) lmax_1[rs]+=lmax_1[rs<<1|1];    if (lmax_0[rs<<1]==m-(m>>1)) lmax_0[rs]+=lmax_0[rs<<1|1]; rmax_1[rs]=rmax_1[rs<<1|1];//processing the right class suffix rmax_0[rs]=rmax_0[rs<<1|1];    if (rmax_1[rs<<1|1]== (m>>1)) rmax_1[rs]+=rmax_1[rs<<1];    if (rmax_0[rs<<1|1]== (m>>1)) rmax_0[rs]+=rmax_0[rs<<1]; Max_1[rs]=max (max_1[rs<<1],max_1[rs<<1|1]);//Handle the value of the entire interval Max_1[rs]=max (max_1[rs],rmax_1[rs<<1]+    LMAX_1[RS&LT;&LT;1|1]);    Max_0[rs]=max (max_0[rs<<1],max_0[rs<<1|1]); Max_0[rs]=max (max_0[rs],rmax_0[rs<<1]+lmax_0[rs<<1|1]);}    void work (int rs) {swap (lmax_1[rs],lmax_0[rs]);    Swap (Rmax_1[rs],rmax_0[rs]); Swap (Max_1[rs],max_0[rs]);}        void Push_down (int rs) {if (Col[rs]) {col[rs<<1]^=1;col[rs<<1|1]^=1;    Col[rs]=0;work (rs<<1); work (rs<<1|1);    }}void Build (int rs,int l,int r) {col[rs]=0;        if (l==r) {scanf ("%d", &os);            if (os==1) {lmax_1[rs]=rmax_1[rs]=max_1[rs]=1;        lmax_0[rs]=rmax_0[rs]=max_0[rs]=0; } else {lmax_1[rs]=rmax_1[rs]=max_1[rs]=0;        Lmax_0[rs]=rmax_0[rs]=max_0[rs]=1;    } return;    } int mid= (L+R) >>1;    Build (Rs<<1,l,mid);    Build (Rs<<1|1,mid+1,r); Pushup (rs,r-l+1);}        void update (int x,int y,int l,int r,int rs) {if (l>=x&&r<=y) {col[rs]^=1;    Work (RS);//exchange value return;    } push_down (RS);    int mid= (L+R) >>1;    if (x<=mid) update (X,Y,L,MID,RS&LT;&LT;1);    if (y>mid) update (X,Y,MID+1,R,RS&LT;&LT;1|1); Pushup (rs,r-l+1);}    int query (int x,int y,int l,int r,int rs) {if (l>=x&&r<=y) return max_1[rs];    Push_down (RS);    int mid= (L+R) >>1;    if (x>mid) return query (X,Y,MID+1,R,RS&LT;&LT;1|1);    if (y<=mid) return query (x,y,l,mid,rs<<1);    int T1=query (x,y,l,mid,rs<<1);    int T2=query (X,Y,MID+1,R,RS&LT;&LT;1|1);    int r1=min (mid-x+1,rmax_1[rs<<1]);//with mid as the dividing point int r2=min (y-mid,lmax_1[rs<<1|1]); int Res=max (max (T1,T2), r1+r2); return res;}    int main () {int op,x,y;       while (~SCANF ("%d", &n)) {build (1,1,n);       scanf ("%d", &m);           while (m--) {scanf ("%d%d%d", &op,&x,&y);           if (op==1) update (x,y,1,n,1);       else printf ("%d\n", Query (x,y,1,n,1)); }} return 0;}


HDU 3911 Black and White (segment tree interval merge)

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.