Black And White
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5583 Accepted Submission(s): 1649
Problem Description
There 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]
Output
When x=0 output a number means the longest length of black stones in range [i,j].
Sample Input
4 1 0 1 0 5 0 1 4 1 2 3 0 1 4 1 3 3 0 4 4
Sample Output
1 2 0
Source
Recommend
其实就是建两颗树 SWAP一下 维护左边最长连续 右边最长连续 中间最长连续 得一个最大值连续
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAX_N = 100005; struct node { int len; int len_l; int len_r; int down; }arr1[4*MAX_N],arr0[4*MAX_N]; void up(int p,int l,int r){ int mid = l+(r-l)/2,len_black,len_white; if(arr1[p*2].len_l==(mid-l+1)){ arr1[p].len_l = arr1[p*2+1].len_l+arr1[p*2].len_l; } else { arr1[p].len_l = arr1[p*2].len_l; } if(arr1[p*2+1].len_r==r-mid){ arr1[p].len_r = arr1[p*2+1].len_r+arr1[p*2].len_r; } else { arr1[p].len_r = arr1[p*2+1].len_r; } len_black = arr1[p*2].len_r+arr1[p*2+1].len_l; arr1[p].len=max(max(max(arr1[p*2].len_l,arr1[p*2+1].len_r),max(arr1[p*2+1].len,arr1[p*2].len)),len_black); if(arr0[p*2].len_l==(mid-l+1)){ arr0[p].len_l = arr0[p*2+1].len_l+arr0[p*2].len_l; } else { arr0[p].len_l = arr0[p*2].len_l; } if(arr0[p*2+1].len_r==r-mid){ arr0[p].len_r = arr0[p*2+1].len_r+arr0[p*2].len_r; } else { arr0[p].len_r = arr0[p*2+1].len_r; } len_white = arr0[p*2].len_r+arr0[p*2+1].len_l; arr0[p].len=max(max(max(arr0[p*2].len_l,arr0[p*2+1].len_r),max(arr0[p*2+1].len,arr0[p*2].len)),len_white); } void SWAP(int p){ int len_l = arr1[p].len_l; int len_r = arr1[p].len_r; int len_len =arr1[p].len; arr1[p].len_l = arr0[p].len_l; arr1[p].len_r = arr0[p].len_r; arr1[p].len = arr0[p].len; arr0[p].len_l = len_l; arr0[p].len_r = len_r; arr0[p].len = len_len; } void down(int p,int l,int r){ if(arr1[p].down){ arr1[p].down^=1; arr1[p*2].down^=1; arr1[p*2+1].down^=1; SWAP(p); SWAP(p*2); SWAP(p*2+1); } } void build(int p,int l,int r){ if(l==r){ scanf("%d",&arr1[p].len); if(arr1[p].len){ arr1[p].len_l = 1; arr1[p].len_r = 1; } else { arr0[p].len = 1; arr0[p].len_l = 1; arr0[p].len_r = 1; } return; } down(p,l,r); int mid =l+(r-l)/2; build(p*2,l,mid); build(p*2+1,mid+1,r); up(p,l,r); } void modify(int p,int l,int r,int x,int y){ if(x<=l&&r<=y) { arr1[p].down^=1; SWAP(p); return; } int mid = l+(r-l)/2; down(p,l,r); if(x<=mid) modify(p*2,l,mid,x,y); if(y>mid) modify(p*2+1,mid+1,r,x,y); up(p,l,r); } node query(int p,int l,int r,int x,int y){ if(x<=l&&r<=y){ return arr1[p]; } down(p,l,r); int mid = l+(r-l)/2; up(p,l,r); if(x<=mid&&y>mid){ node nod1 = query(p*2,l,mid,x,y); node nod2 = query(p*2+1,mid+1,r,x,y); node nod; if(nod1.len_l==(mid-l+1)){ nod.len_l = nod1.len_l+nod2.len_l; } else { nod.len_l = nod1.len_l; } if(nod2.len_r==r-mid){ nod.len_r = nod1.len_r+nod2.len_r; } else { nod.len_r = nod2.len_r; } int len_len = nod1.len_r+nod2.len_l; nod.len=max(max(max(nod.len_r,nod.len_l),max(nod1.len,nod2.len)),len_len); return nod; } else if(x<=mid){ return query(p*2,l,mid,x,y); } else if(x>mid){ return query(p*2+1,mid+1,r,x,y); } } int main(){ int n,m; while(~scanf("%d",&n)){ memset(arr1,0,sizeof(arr1)); memset(arr0,0,sizeof(arr0)); build(1,1,n); scanf("%d",&m); while(m--){ int d,a,b; scanf("%d%d%d",&d,&a,&b); if(d==1){ modify(1,1,n,a,b); } else if(d==0) { printf("%d\n",query(1,1,n,a,b).len); } } } return 0; }