神犇救命,5分调试一周了
查看原帖
神犇救命,5分调试一周了
111349
bobzbh楼主2022/12/29 08:36
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define int long long
#define N 200086 
#define ls(x) (x<<1)
#define rs(x) ((x<<1)+1)
using namespace std;
struct node
{
	int val,ans,pre,bac,tag,len;
	//val 值 ans 最大hole   pre 前驱   bac 后继    len 区间长度 
	//tag=1 全部填满 tag=2全挖掉 
}tree[N*4];
int n,m;
//建树过程 
void build(int pl,int l,int r)
{
	tree[pl].len=r-l+1;
	if(l==r)
	{
		tree[pl].val=1;
		return ;
	}
	int lson=ls(pl),rson=rs(pl),mid=(l+r)>>1;
	build(lson,l,mid),build(rson,mid+1,r);
	tree[pl].val=tree[lson].val+tree[rson].val;
	return ;
}
//tag=2时候的操作 
void fix2(int pl)
{
	tree[pl].pre=tree[pl].len;
	tree[pl].bac=tree[pl].len;
	tree[pl].ans=tree[pl].len;
	tree[pl].tag=2;
	tree[pl].val=0;
}
//tag=1时候操作 
void fix1(int pl)
{
	tree[pl].pre=0;
	tree[pl].bac=0;
	tree[pl].ans=0;
	tree[pl].tag=1;
	tree[pl].val=tree[pl].len;
}
 
void push_up(int pl)
{
	clear(pl);
	int lson=ls(pl),rson=rs(pl);
	if(tree[lson].val==0)
	{
		tree[pl].pre=tree[lson].len+tree[rson].pre;
	}
	else
	{
		tree[pl].pre=tree[lson].pre;
	}
	if(tree[rson].val==0)
	{
		tree[pl].bac=tree[rson].len+tree[lson].bac;
	}
	else
	{
		tree[pl].bac=tree[rson].bac;
	}
	tree[pl].val=tree[lson].val+tree[rson].val;
	tree[pl].ans=max(tree[lson].ans,tree[rson].ans);
	tree[pl].ans=max(tree[pl].ans,tree[lson].bac+tree[rson].pre);
	return ;
}

void push_down(int pl)
{
	if(tree[pl].tag==0)
	{
		return ;
	}
	int lson=ls(pl),rson=rs(pl);
	if(tree[pl].tag==2)
	{
		fix2(lson);
		fix2(rson);
	}
	else
	{
		fix1(lson);
		fix1(rson);
	}
}
//填充 p当前位置,l区间左,r区间右,pl补充区间左 pr 补充区间右 val可使用的脑细胞数量 
int fill(int p,int l,int r,int pl,int pr,int val)
{
	if(l>pr||r<pl||val==0)
	{
		return val;
	}
	int mid=(l+r)>>1,lson=ls(p),rson=rs(p),num=tree[lson].len-tree[lson].val;
	if(r<=pr&&l>=pl)
	{
		if(l==r)
		{
			fix1(p);
			return val-1;
		}
		int left;
		if(num<val)
		{
			fix1(lson);
			left=fill(rson,mid+1,r,pl,pr,val-num);
		}
		else
		{
			left=fill(lson,l,mid,pl,pr,val);
		}
		push_up(p);
		return left;
	}
	push_down(p);
	int left=fill(rson,mid+1,r,pl,pr,fill(lson,l,mid,pl,pr,val));
	push_up(p);
	return left;
}
//同上,不过是挖 ,返回值是挖出的脑细胞数量 
int dig(int p,int l,int r,int pl,int pr)
{
	if(r<pl||l>pr)
	{
		return 0; 
	}
	if(r<=pr&&l>=pl)
	{
		int num=tree[p].val;
		fix2(p);
		return num;
	}
	int mid=(l+r)>>1,lson=ls(p),rson=rs(p);
	push_down(p);
	int num=dig(lson,l,mid,pl,pr)+dig(rson,mid+1,r,pl,pr);
	push_up(p);
	return num;
}
//同上 
void work(int l,int r,int pl,int pr)
{
	int val=dig(1,1,n,l,r);
	fill(1,1,n,pl,pr,val);
	return;
}
//找最大hole 返回值就是最打的脑洞 
int find(int p,int l,int r,int pl,int pr)
{
	if(l>pr||r<pl)
	{
		return 0;
	}
	if(r<=pr&&l>=pl)
	{
		return tree[p].ans;
	}
	push_down(p);
	int mid=(l+r)>>1,lson=ls(p),rson=rs(p);
	int an=0;
	if(mid>=pl&&mid<pr)
	{
		an=min(mid+tree[rson].pre,pr)-max(mid-tree[lson].bac+1,pl)+1;
	}
	int an_=max(find(lson,l,mid,pl,pr),find(rson,mid+1,r,pl,pr));
	push_up(p);
	return max(an,an_);
}

signed main()
{
	cin.tie(0),cout.tie(0);
	ios::sync_with_stdio(false);
	cin>>n>>m;
	build(1,1,n);
	for(int i=1; i<=m; ++i)
	{
		int ope;
		cin>>ope;
		if(ope==0)
		{
			int l,r;
			cin>>l>>r;
			dig(1,1,n,l,r);
		}
		else if(ope==1)
		{
			int l,r,l_,r_;
			cin>>l>>r>>l_>>r_;
			work(l,r,l_,r_);
		}
		else
		{
			int l,r;
			cin>>l>>r;
			cout<<find(1,1,n,l,r)<<endl;
		}
	}
	return 0;
}
//附上第二个样例,错在了最后一个点上TAT 
/*
100 100
0 1 3
1 4 6 1 100
2 1 5
0 1 3
1 7 9 1 100
2 1 8
0 1 3
1 10 12 1 100
2 1 11
0 1 3
1 13 15 1 100
2 1 14
0 1 3
1 16 18 1 100
2 1 17
0 1 3
1 19 21 1 100
2 1 20
0 1 3
1 22 24 1 100
2 1 23
0 1 3
1 25 27 1 100
2 1 26
0 1 3
1 28 30 1 100
2 1 29
0 1 3
1 31 33 1 100
2 1 32
0 1 3
1 34 36 1 100
2 1 35
0 1 3
1 37 39 1 100
2 1 38
0 1 3
1 40 42 1 100
2 1 41
0 1 3
1 43 45 1 100
2 1 44
0 1 3
1 46 48 1 100
2 1 47
0 1 3
1 49 51 1 100
2 1 50
0 1 3
1 52 54 1 100
2 1 53
0 1 3
1 55 57 1 100
2 1 56
0 1 3
1 58 60 1 100
2 1 59
0 1 3
1 61 63 1 100
2 1 62
0 1 3
1 64 66 1 100
2 1 65
0 1 3
1 67 69 1 100
2 1 68
0 1 3
1 70 72 1 100
2 1 71
0 1 3
1 73 75 1 100
2 1 74
0 1 3
1 76 78 1 100
2 1 77
0 1 3
1 79 81 1 100
2 1 80
0 1 3
1 82 84 1 100
2 1 83
0 1 3
1 85 87 1 100
2 1 86
0 1 3
1 88 90 1 100
2 1 89
0 1 3
1 91 93 1 100
2 1 92
0 1 3
1 94 96 1 100
2 1 95
0 1 3
1 97 99 1 100
2 1 98
0 1 3
1 100 100 1 100
2 1 100
0 1 3
*/
2022/12/29 08:36
加载中...