普通线段树再求调
  • 板块灌水区
  • 楼主feEr
  • 当前回复3
  • 已保存回复3
  • 发布时间2025/1/15 13:40
  • 上次更新2025/1/15 17:57:59
查看原帖
普通线段树再求调
1383338
feEr楼主2025/1/15 13:40

愿意看的私信交流

没问题也说一声行吗

struct tree
{
	struct node
	{
		int l,r; // 左右边界
		node *lson,*rson; // 左右儿子
		int maxs; // 区间最大值
		int set_tag; // 赋值懒标记(赋值为什么)
		bool unset_tag; // 赋值懒标记(是否有标记)
		node(){};
		node(int l,int r):l(l),r(r),lson(nullptr),rson(nullptr),maxs(0),set_tag(0),unset_tag(0){}
	} *root;
	#define init \
		int &l=Node->l,&r=Node->r; \
		node *&lson=Node->lson,*&rson=Node->rson; \
		int &maxs=Node->maxs; \
		int &set_tag=Node->set_tag; \
		bool &unset_tag=Node->unset_tag;
	void upd(node *Node) // 更新左右儿子
	{
		init;
		int mid=l+r>>1;
		if(lson==nullptr)
			lson=new node(l,mid);
		if(rson==nullptr)
			rson=new node(mid+1,r);
	}
	void set(node *Node,int v)
	{
		init;
		maxs=v;
		set_tag=v;
		unset_tag=1;
	}
	void pushdown(node *Node)
	{
		init;
		if(l==r||!unset_tag)
			return;
		upd(Node);
		set(lson,set_tag);
		set(rson,set_tag);
		unset_tag=0;
	}
	void pushup(node *Node)
	{
		init;
		if(l==r)
			return;
		upd(Node);
		pushdown(Node);
		maxs=max(lson->maxs,rson->maxs);
	}
	void modify(node *Node,int x,int y,int v)
	{
		init;
		if(y<l||r<x)
			return;
		if(x<=l&&r<=y)
		{
			set(Node,v);
			return;
		}
		upd(Node);
		pushdown(Node);
		modify(lson,x,y,v);
		modify(rson,x,y,v);
		pushup(Node);
	}
	int query(node *Node,int x,int y)
	{
		init;
		if(y<l||r<x)
			return 0;
		if(x<=l&&r<=y)
			return maxs;
		upd(Node);
		pushdown(Node);
		return max(query(lson,x,y),query(rson,x,y));
	}
	tree(){}
	tree(int l,int r):root(new node(l,r)){}
	#undef init
};
2025/1/15 13:40
加载中...