Black And White
Time Limit: 3000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3911
64-bit integer IO format: %I64d Java class name: Main
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
2011 Multi-University Training Contest 8 - Host by HUST
解题:线段树啊。。。幸好只有一种操作,翻转啊。。。
就是给你两种操作,1 a b表示将a b区里面的01翻转,0 a b表示输出a b间连续的最长的1有多少个
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 struct node { 5 int lsum[2],rsum[2],mx[2]; 6 bool lazy; 7 } tree[maxn<<2]; 8 void pushup(int v,int k) { 9 for(int i = 0; i < 2; ++i) { 10 tree[v].lsum[i] = tree[v<<1].lsum[i]; 11 tree[v].rsum[i] = tree[v<<1|1].rsum[i]; 12 if(tree[v].lsum[i] == k - (k>>1)) 13 tree[v].lsum[i] += tree[v<<1|1].lsum[i]; 14 if(tree[v].rsum[i] == (k>>1)) 15 tree[v].rsum[i] += tree[v<<1].rsum[i]; 16 tree[v].mx[i] = max(tree[v<<1].mx[i],tree[v<<1|1].mx[i]); 17 tree[v].mx[i] = max(tree[v].mx[i],tree[v<<1].rsum[i]+tree[v<<1|1].lsum[i]); 18 } 19 } 20 21 void change(int v) { 22 swap(tree[v].lsum[0],tree[v].lsum[1]); 23 swap(tree[v].rsum[0],tree[v].rsum[1]); 24 swap(tree[v].mx[0],tree[v].mx[1]); 25 tree[v].lazy = !tree[v].lazy; 26 } 27 void pushdown(int v) { 28 if(tree[v].lazy) { 29 change(v<<1); 30 change(v<<1|1); 31 tree[v].lazy = false; 32 } 33 } 34 void build(int L,int R,int v) { 35 tree[v].lazy = false; 36 if(L == R) { 37 int tmp; 38 scanf("%d",&tmp); 39 tree[v].mx[0] = tree[v].lsum[0] = tree[v].rsum[0] = !tmp; 40 tree[v].mx[1] = tree[v].lsum[1] = tree[v].rsum[1] = tmp; 41 return; 42 } 43 int mid = (L + R)>>1; 44 build(L,mid,v<<1); 45 build(mid+1,R,v<<1|1); 46 pushup(v,R - L + 1); 47 } 48 void update(int L,int R,int lt,int rt,int v) { 49 if(lt <= L && rt >= R) { 50 change(v); 51 return; 52 } 53 pushdown(v); 54 int mid = (L + R)>>1; 55 if(lt <= mid) update(L,mid,lt,rt,v<<1); 56 if(rt > mid) update(mid+1,R,lt,rt,v<<1|1); 57 pushup(v,R - L + 1); 58 } 59 int query(int L,int R,int lt,int rt,int v) { 60 if(lt <= L && rt >= R) return tree[v].mx[1]; 61 pushdown(v); 62 int mid = (L + R)>>1,ret = 0; 63 if(lt <= mid) ret = max(ret,query(L,mid,lt,rt,v<<1)); 64 if(rt > mid) ret = max(ret,query(mid+1,R,lt,rt,v<<1|1)); 65 if(lt <= mid && rt > mid) 66 ret = max(ret,min(mid - lt + 1,tree[v<<1].rsum[1]) + min(rt - mid,tree[v<<1|1].lsum[1])); 67 pushup(v,R - L + 1); 68 return ret; 69 } 70 int main() { 71 int n,m,op,a,b; 72 while(~scanf("%d",&n)){ 73 build(1,n,1); 74 scanf("%d",&m); 75 while(m--){ 76 scanf("%d %d %d",&op,&a,&b); 77 if(op) update(1,n,a,b,1); 78 else printf("%d\n",query(1,n,a,b,1)); 79 } 80 } 81 return 0; 82 }