P6492 [COCI2010-2011#6] STEP(区间合并维护区间连续01串)

https://www.luogu.com.cn/problem/P6492


思路:类比维护连续的1.模板

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+100;
typedef int LL;
struct Tree{
	LL l,r,pre,suf,sum;
}tree[maxn*4];
LL a[maxn];
LL rmax(LL A,LL B,LL C){
	return max(A,max(B,C));
}
void push_up(LL p){
	tree[p].pre=tree[p*2].pre;
	tree[p].suf=tree[p*2+1].suf;
	tree[p].sum=max(tree[p*2].sum,tree[p*2+1].sum);
	if(a[tree[p*2].r]!=a[tree[p*2+1].l]) tree[p].sum=max(tree[p].sum,tree[p*2].suf+tree[p*2+1].pre);
	if(tree[p*2].pre==tree[p*2].r-tree[p*2].l+1&&a[tree[p*2].r]!=a[tree[p*2+1].l]) tree[p].pre+=tree[p*2+1].pre;
	if(tree[p*2+1].suf==tree[p*2+1].r-tree[p*2+1].l+1&&a[tree[p*2].r]!=a[tree[p*2+1].l]) tree[p].suf+=tree[p*2].suf;
}
void build(LL p,LL l,LL r){
	tree[p].l=l;tree[p].r=r;tree[p].pre=tree[p].suf=tree[p].sum=1;
	if(l==r) {return;}
	LL mid=(l+r)>>1;
	build(p*2,l,mid);build(p*2+1,mid+1,r);
	push_up(p);	
}
void modify(LL p,LL l,LL r){
	if(tree[p].l==tree[p].r){
		if(a[tree[p].l]==1){
			a[tree[p].r]=0;
		}	
		else if(a[tree[p].l]==0){
			a[tree[p].l]=1;
		}
		return;
	}
	LL mid=(tree[p].l+tree[p].r)>>1;
	if(l<=mid) modify(p*2,l,r);
	if(r>mid) modify(p*2+1,l,r);
	push_up(p);
}
LL query(LL p,LL l,LL r){
	return tree[p].sum;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++){
  	a[i]=1;
  }
  build(1,1,n);
  while(m--){
  	LL x;cin>>x;
  	modify(1,x,x);
  	cout<<query(1,1,n)<<endl;
  }
return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值