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
解题:线段树啊。。。幸好只有一种操作,翻转啊。。。
就是给你两种操作,1 a b表示将a b区里面的01翻转,0 a b表示输出a b间连续的最长的1有多少个
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int lsum[],rsum[],mx[];
bool lazy;
} tree[maxn<<];
void pushup(int v,int k) {
for(int i = ; i < ; ++i) {
tree[v].lsum[i] = tree[v<<].lsum[i];
tree[v].rsum[i] = tree[v<<|].rsum[i];
if(tree[v].lsum[i] == k - (k>>))
tree[v].lsum[i] += tree[v<<|].lsum[i];
if(tree[v].rsum[i] == (k>>))
tree[v].rsum[i] += tree[v<<].rsum[i];
tree[v].mx[i] = max(tree[v<<].mx[i],tree[v<<|].mx[i]);
tree[v].mx[i] = max(tree[v].mx[i],tree[v<<].rsum[i]+tree[v<<|].lsum[i]);
}
} void change(int v) {
swap(tree[v].lsum[],tree[v].lsum[]);
swap(tree[v].rsum[],tree[v].rsum[]);
swap(tree[v].mx[],tree[v].mx[]);
tree[v].lazy = !tree[v].lazy;
}
void pushdown(int v) {
if(tree[v].lazy) {
change(v<<);
change(v<<|);
tree[v].lazy = false;
}
}
void build(int L,int R,int v) {
tree[v].lazy = false;
if(L == R) {
int tmp;
scanf("%d",&tmp);
tree[v].mx[] = tree[v].lsum[] = tree[v].rsum[] = !tmp;
tree[v].mx[] = tree[v].lsum[] = tree[v].rsum[] = tmp;
return;
}
int mid = (L + R)>>;
build(L,mid,v<<);
build(mid+,R,v<<|);
pushup(v,R - L + );
}
void update(int L,int R,int lt,int rt,int v) {
if(lt <= L && rt >= R) {
change(v);
return;
}
pushdown(v);
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,v<<);
if(rt > mid) update(mid+,R,lt,rt,v<<|);
pushup(v,R - L + );
}
int query(int L,int R,int lt,int rt,int v) {
if(lt <= L && rt >= R) return tree[v].mx[];
pushdown(v);
int mid = (L + R)>>,ret = ;
if(lt <= mid) ret = max(ret,query(L,mid,lt,rt,v<<));
if(rt > mid) ret = max(ret,query(mid+,R,lt,rt,v<<|));
if(lt <= mid && rt > mid)
ret = max(ret,min(mid - lt + ,tree[v<<].rsum[]) + min(rt - mid,tree[v<<|].lsum[]));
pushup(v,R - L + );
return ret;
}
int main() {
int n,m,op,a,b;
while(~scanf("%d",&n)){
build(,n,);
scanf("%d",&m);
while(m--){
scanf("%d %d %d",&op,&a,&b);
if(op) update(,n,a,b,);
else printf("%d\n",query(,n,a,b,));
}
}
return ;
}