1 10 10 0 0 0 1 1 0 1 0 1 1 1 0 2 3 0 5 2 2 2 4 0 4 0 3 6 2 3 7 4 2 8 1 0 5 0 5 6 3 3 9
5 2 6 5
题目大意:给一个01串,给3种操作,分别是将一个区间置0,置1和取反。给2种查询,分别是查询某个区间1的个数和查询某个区间最长连续1的个数。
题目分析:线段树好题。
对区间的3种操作中,置0和置1操作很简单,直接将区间lazy标记,强制覆盖。如果是对区间取反的话,就要考虑区间原来的状态了。如果原来该区间已经取反了,再取反相当于还原,如果该区间没有标记,标记上取反直接返回,如果有标记,则当前区间lazy标记取反更新相应值返回即可。
可以看出,每个区间每次操作只可能有一种逻辑结果,所以只需要一个lazy标记即可。lazy有4个取值:-1,0,1,2,分别表示当前区间无标记,当前区间覆盖0,当前区间覆盖1意见当前区间取反。
其他的就是简单的线段树成段更新了。
不过查询操作有个查询某个区间最大连续1的个数。这稍微复杂些,不过也好办。如果没有取反操作,那就和这道Hotel一样了,直接标记左右连续1的个数即可。但是有了取反操作,那么还要维护当前区间左右连续0的个数,因为取反的时候,直接交换当前区间连续0和连续1的个数即可。
详情请见代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 1000005; int n,m; struct node { int sum;//区间1的个数 int lazy;//区间更新延迟标记和翻转标记 int long1,long0;//最长连续的1和0 int l[2],r[2];//记录区间左右连续的1,0长度 }tree[N]; int Max(int a,int b) { return a > b?a:b; } int Min(int a,int b) { return a > b?b:a; } void pushup(int num,int s,int e) { int ls = num<<1; int rs = num<<1|1; int mid = (s + e)>>1; tree[num].sum = tree[ls].sum + tree[rs].sum; tree[num].l[0] = tree[ls].l[0]; if(tree[ls].sum == 0) tree[num].l[0] += tree[rs].l[0]; tree[num].l[1] = tree[ls].l[1]; if(tree[ls].sum == mid - s + 1) tree[num].l[1] += tree[rs].l[1]; tree[num].r[0] = tree[rs].r[0]; if(tree[rs].sum == 0) tree[num].r[0] += tree[ls].r[0]; tree[num].r[1] = tree[rs].r[1]; if(tree[rs].sum == e - mid) tree[num].r[1] += tree[ls].r[1]; tree[num].long0 = Max(tree[ls].long0,Max(tree[rs].long0,tree[ls].r[0] + tree[rs].l[0])); tree[num].long1 = Max(tree[ls].long1,Max(tree[rs].long1,tree[ls].r[1] + tree[rs].l[1])); } void pushdown(int num,int s,int e) { if(s == e || tree[num].lazy == -1) return; int ls = num<<1; int rs = num<<1|1; int mid = (s + e)>>1; if(tree[num].lazy == 0) { tree[ls].lazy = tree[rs].lazy = 0; tree[ls].sum = tree[rs].sum = 0; tree[ls].long1 = tree[rs].long1 = 0; tree[ls].l[1] = tree[ls].r[1] = tree[rs].l[1] = tree[rs].r[1] = 0; tree[ls].l[0] = tree[ls].r[0] = tree[ls].long0 = mid - s + 1; tree[rs].l[0] = tree[rs].r[0] = tree[rs].long0 = e - mid; } if(tree[num].lazy == 1) { tree[ls].lazy = tree[rs].lazy = 1; tree[ls].sum = mid - s + 1;tree[rs].sum = e - mid; tree[ls].long0 = tree[rs].long0 = 0; tree[ls].l[0] = tree[ls].r[0] = tree[rs].l[0] = tree[rs].r[0] = 0; tree[ls].l[1] = tree[ls].r[1] = tree[ls].long1 = mid - s + 1; tree[rs].l[1] = tree[rs].r[1] = tree[rs].long1 = e - mid; } if(tree[num].lazy == 2) { if(tree[ls].lazy == -1 || tree[ls].lazy == 2) { tree[ls].lazy = 1 - tree[ls].lazy; tree[ls].sum = mid - s + 1 - tree[ls].sum; swap(tree[ls].long0,tree[ls].long1); swap(tree[ls].l[0],tree[ls].l[1]); swap(tree[ls].r[0],tree[ls].r[1]); } else if(tree[ls].lazy == 0) { tree[ls].lazy = 1; tree[ls].sum = tree[ls].long1 = mid - s + 1; tree[ls].long0 = 0; tree[ls].l[0] = tree[ls].r[0] = 0; tree[ls].l[1] = tree[ls].r[1] = mid - s + 1; } else if(tree[ls].lazy == 1)//....少了2个else 跪了这么久。。。 { tree[ls].lazy = 0; tree[ls].sum = tree[ls].long1 = 0; tree[ls].long0 = tree[ls].l[0] = tree[ls].r[0] = mid - s + 1; tree[ls].l[1] = tree[ls].r[1] = 0; } if(tree[rs].lazy == -1 || tree[rs].lazy == 2) { tree[rs].lazy = 1 - tree[rs].lazy; tree[rs].sum = e - mid - tree[rs].sum; swap(tree[rs].long0,tree[rs].long1); swap(tree[rs].l[0],tree[rs].l[1]); swap(tree[rs].r[0],tree[rs].r[1]); } else if(tree[rs].lazy == 0)//....sb { tree[rs].lazy = 1; tree[rs].sum = tree[rs].long1 = e - mid; tree[rs].long0 = 0; tree[rs].l[0] = tree[rs].r[0] = 0; tree[rs].l[1] = tree[rs].r[1] = e - mid; } else if(tree[rs].lazy == 1) { tree[rs].lazy = 0; tree[rs].sum = tree[rs].long1 = 0; tree[rs].long0 = tree[rs].l[0] = tree[rs].r[0] = e - mid; tree[rs].l[1] = tree[rs].r[1] = 0; } } tree[num].lazy = -1; } void build(int num,int s,int e) { tree[num].lazy = -1;tree[num].sum = 0; tree[num].long0 = tree[num].l[0] = tree[num].r[0] = 0; tree[num].long1 = tree[num].l[1] = tree[num].r[1] = 0; if(s == e) { scanf("%d",&tree[num].sum); if(tree[num].sum) tree[num].long1 = tree[num].l[1] = tree[num].r[1] = 1; else tree[num].long0 = tree[num].l[0] = tree[num].r[0] = 1; return; } int mid = (s + e)>>1; build(num<<1,s,mid); build(num<<1|1,mid + 1,e); pushup(num,s,e); } void update(int num,int s,int e,int l,int r,int op) { if(s == l && e == r) { if(op == 0) { tree[num].sum = tree[num].long1 = tree[num].lazy = 0; tree[num].long0 = tree[num].l[0] = tree[num].r[0] = e - s + 1; tree[num].l[1] = tree[num].r[1] = 0; } if(op == 1) { tree[num].lazy = 1; tree[num].sum = tree[num].long1 = e - s + 1; tree[num].long0 = tree[num].l[0] = tree[num].r[0] = 0; tree[num].l[1] = tree[num].r[1] = e - s + 1; } if(op == 2) { switch(tree[num].lazy) { case 0: tree[num].lazy = 1; tree[num].long1 = tree[num].sum = e - s + 1; tree[num].l[1] = tree[num].r[1] = e - s + 1; tree[num].long0 = tree[num].l[0] = tree[num].r[0] = 0; break; case 1: tree[num].lazy = 0; tree[num].long0 = tree[num].l[0] = tree[num].r[0] = e - s + 1; tree[num].long1 = tree[num].sum = 0; tree[num].l[1] = tree[num].r[1] = 0; break; case 2: case -1: tree[num].lazy = 1 - tree[num].lazy; tree[num].sum = e - s + 1 - tree[num].sum; swap(tree[num].long0,tree[num].long1); swap(tree[num].l[0],tree[num].l[1]); swap(tree[num].r[0],tree[num].r[1]); } } return; } pushdown(num,s,e); int mid = (s + e)>>1; if(r <= mid) update(num<<1,s,mid,l,r,op); else { if(l > mid) update(num<<1|1,mid + 1,e,l,r,op); else { update(num<<1,s,mid,l,mid,op); update(num<<1|1,mid + 1,e,mid + 1,r,op); } } pushup(num,s,e); } int query(int num,int s,int e,int l,int r,int op) { if(s == l && r == e) { if(op == 3) return tree[num].sum; else return tree[num].long1; } pushdown(num,s,e); int mid = (s + e)>>1; if(r <= mid) return query(num<<1,s,mid,l,r,op); else { if(l > mid) return query(num<<1|1,mid + 1,e,l,r,op); else { if(op == 3) return query(num<<1,s,mid,l,mid,op) + query(num<<1|1,mid + 1,e,mid + 1,r,op); else { int ret = Min(tree[num<<1].r[1],mid - l + 1) + Min(tree[num<<1|1].l[1],r - mid); return Max(query(num<<1,s,mid,l,mid,op),Max(query(num<<1|1,mid + 1,e,mid + 1,r,op),ret)); } } } } int main() { int i,t; int op,a,b; scanf("%d",&t); while(t --) { scanf("%d%d",&n,&m); build(1,1,n); while(m --) { scanf("%d%d%d",&op,&a,&b); a ++;b ++; if(op <= 2) update(1,1,n,a,b,op); else printf("%d\n",query(1,1,n,a,b,op)); } } return 0; } //843MS 8840K
引以为戒。。。