(代码求调)可不可以在节点里塞区间最左边的合法区间左端点
查看原帖
(代码求调)可不可以在节点里塞区间最左边的合法区间左端点
367991
Arctic_1010楼主2022/9/24 10:42

ps:借鉴题解 A 了

但是这份代码没过(

#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
int n,m;
struct node
{
	int l,r;//区间左端点 
	int pre,suf,Max,pos;//左、右、总共(最大)连续0的个数 
	int lazy;//懒标记 
}t[N];
#define lson rt<<1
#define rson rt<<1|1
int getlen(int rt)
{
	return t[rt].r-t[rt].l+1;
}
void pushup(int rt)
{
	t[rt].pre=t[lson].pre;
	if(t[lson].pre==getlen(lson)) t[rt].pre=t[lson].pre+t[rson].pre;
	t[rt].suf=t[rson].suf;
	if(t[rson].suf==getlen(rson)) t[rt].suf=t[lson].suf+t[rson].suf;
	t[rt].Max=t[lson].Max,t[rt].pos=t[lson].pos;
	if(t[rt].Max<t[rson].Max)
		t[rt].Max=t[rson].Max,t[rt].pos=t[rson].pos;
	if(t[rt].Max<t[lson].suf+t[rson].pre)
		t[rt].Max=t[lson].suf+t[rson].pre,t[rt].pos=t[lson].r-t[lson].suf+1;	
}
void pushdown(int rt)
{
	if(!t[rt].lazy) return;
	if(t[rt].lazy==1)
	{
		t[lson].pre=t[lson].suf=t[lson].Max=0;
		t[lson].pos=t[lson].l;
		t[rson].pre=t[rson].suf=t[rson].Max=0;
		t[rson].pos=t[rson].l;
		t[lson].lazy=t[rson].lazy=1;		
	}
	else
	{
		t[lson].pre=t[lson].suf=t[lson].Max=getlen(lson);
		t[lson].pos=t[lson].l;
		t[rson].pre=t[rson].suf=t[rson].Max=getlen(rson);
		t[rson].pos=t[rson].l;
		t[lson].lazy=t[rson].lazy=2;
	}
	t[rt].lazy=0;
}
void build(int l,int r,int rt)
{
	t[rt].l=t[rt].pos=l,t[rt].r=r;
	t[rt].pre=t[rt].suf=t[rt].Max=r-l+1;
	if(l==r) return ;
	int mid=(l+r)>>1;
	build(l,mid,lson);
	build(mid+1,r,rson);
	pushup(rt);
}
void modify(int rt,int posl,int posr,int k)
{
	if(posl<=t[rt].l && t[rt].r<=posr)
	{
		if(k==1) t[rt].lazy=1,t[rt].pre=t[rt].suf=t[rt].Max=0;
		else t[rt].lazy=2,t[rt].pre=t[rt].suf=t[rt].Max=getlen(rt);
		return ;
	}
	int mid=(t[rt].l+t[rt].r)>>1;
	pushdown(rt);
	if(posl<=mid) modify(lson,posl,posr,k);
	if(posr>mid)  modify(rson,posl,posr,k);
	pushup(rt);
}
signed main()
{
	cin>>n>>m;
	build(1,n,1);
	
	for(int i=1;i<=m;i++)
	{
		int op,x,y;
		cin>>op;
		if(op==1)
		{
			cin>>x;
			if(t[1].Max>=x)
				cout<<t[1].pos<<'\n',
				modify(1,t[1].pos,t[1].pos+x-1,1);
			else cout<<0<<'\n';
		}
		else
		{
			cin>>x>>y;
			modify(1,x,x+y-1,0);
		}
	}	
	return 0;
}
2022/9/24 10:42
加载中...