线段树模板求助
  • 板块灌水区
  • 楼主Jerry_heng
  • 当前回复14
  • 已保存回复14
  • 发布时间2024/4/13 11:32
  • 上次更新2024/4/13 13:58:15
查看原帖
线段树模板求助
763878
Jerry_heng楼主2024/4/13 11:32

站外题:link

我的代码。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mxn=2e5+10,inf=1e18;
int n,m,a[mxn],b[mxn],bb[mxn],q;
struct node{
	int tag,mn,hur,sum,tagg,mx;
}tree[mxn<<2];
//tag:  1:alive 2 leave 3 dead
void pushup(int o){
	tree[o]=(node){1,inf,0ll,0ll,tree[o].tagg,0};
	int son=o*2;
	if(tree[son].tag==1){
		tree[o].mx=max(tree[o].mx,tree[son].mx);
		tree[o].mn=min(tree[o].mn,tree[son].mn);
		tree[o].hur+=tree[son].hur;
		tree[o].sum+=tree[son].sum;
	}
	else if(tree[son].tag==2)tree[o].sum+=tree[son].sum;
	son=o*2+1;
	if(tree[son].tag==1){
		tree[o].mx=max(tree[o].mx,tree[son].mx);
		tree[o].mn=min(tree[o].mn,tree[son].mn);
		tree[o].hur+=tree[son].hur;
		tree[o].sum+=tree[son].sum;
	}
	else if(tree[son].tag==2)tree[o].sum+=tree[son].sum;
	tree[o].mx+=tree[o].tagg;
}
void pushdown(int o){
	tree[o*2].tagg+=tree[o].tagg;
	tree[o*2].mx+=tree[o].tagg;
	tree[o*2+1].tagg+=tree[o].tagg;
	tree[o*2+1].mx+=tree[o].tagg;
	tree[o].tagg=0;
}
void build(int o,int l,int r){
	if(l==r){
		tree[o]=(node){1,b[l],a[l],1,0,0};
		return;
	}
	int mid=(l+r)>>1;
	build(o*2,l,mid);
	build(o*2+1,mid+1,r);
	pushup(o);
}
void update1(int o,int l,int r,int pos){
	if(l==r){
		b[l]=bb[l];
		tree[o]=(node){1,b[l],a[l],1,0,0};
		return;
	}
	int mid=(l+r)>>1;
	pushdown(o);
	if(pos<=mid)update1(o*2,l,mid,pos);
	else update1(o*2+1,mid+1,r,pos);
	pushup(o);
}
void update2(int o,int l,int r,int pos){
	if(l==r){
		tree[o].tag=2;
		tree[o].tagg=tree[o].mx=0;
		return;
	}
	int mid=(l+r)>>1;
	pushdown(o);
	if(pos<=mid)update2(o*2,l,mid,pos);
	else update2(o*2+1,mid+1,r,pos);
	pushup(o);
}
void update3(int o,int l,int r,int val){
	if(tree[o].mx<tree[o].mn)return;
	if(l==r){
		if(tree[o].tag!=1)return;
		b[l]-=tree[o].tagg;
		tree[o].tagg=tree[o].mx=0;
		if(b[l]<=0)tree[o].tag=3,b[l]=inf;
		tree[o].mn=b[l];
		return;
	}
	pushdown(o);
	int mid=(l+r)>>1;
	update3(o*2,l,mid,val);
	update3(o*2+1,mid+1,r,val);
	pushup(o);
}
void update4(int o,int l,int r,int pos,int val){
	if(l==r){
		b[l]=min(bb[l],b[l]+val);
		tree[o].mn=b[l];
		tree[o].tagg=tree[o].mx=0;
		return;
	}
	int mid=(l+r)>>1;
	pushdown(o);
	if(pos<=mid)update4(o*2,l,mid,pos,val);
	else update4(o*2+1,mid+1,r,pos,val);
	pushup(o);
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>n>>m>>q;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i],bb[i]=b[i];
	build(1,1,n);
	while(q--){
		int op,x,y;
		cin>>op;
		if(op==1){
			cin>>x;
			update1(1,1,n,x);
		}
		if(op==2){
			cin>>x;
			update2(1,1,n,x);
		}
		if(op==3){
			cin>>x;
			tree[1].tagg+=x,tree[1].mx+=x;
			update3(1,1,n,x);
		}
		if(op==4){
			cin>>x>>y;
			update4(1,1,n,x,y);
		}
		m-=tree[1].hur;
		if(tree[1].sum==0){
			cout<<"NO";
			return 0;
		}
		if(m<=0){
			cout<<"YES\n";
			cout<<tree[1].sum;
			return 0;
		}
	}
	cout<<"NO";
	return 0;
}
2024/4/13 11:32
加载中...