有没有区间染色单点查询的题?
查看原帖
有没有区间染色单点查询的题?
511811
崛起的滑稽楼主2024/11/3 13:19

rt,想hack下我的区间染色单点查询线段树

#include <bits/stdc++.h>
//#define int int64_t
//#define int __int128
//#define MOD (1000000007)
//#define eps (1e-6)
#define endl '\n'
#define debug_endl cout<<endl;
#define debug cout<<"debug"<<endl;
using namespace std;
const int MAXN=1e5+10;
struct LazySegmentTree{
#define lc(x) (x<<1)
#define rc(x) ((x<<1)|1)
#define mid(l,r) ((l+r)>>1)
	struct Node{
		int color;
	};
	
	Node tr[MAXN<<2];
	
	void push_up(int p){
		tr[p].color=(tr[lc(p)].color==tr[rc(p)].color&&tr[lc(p)].color!=-1?tr[lc(p)].color:-1);
		//左右子树颜色相同:取子树颜色,否则-1表示不相同
	}
	
	void push_down(int p,int l,int r){
		if(tr[p].color!=-1&&l<r){
			tr[lc(p)].color=tr[p].color;
			tr[rc(p)].color=tr[p].color;
		}
	}
	
	void build(int p,int l,int r){
		tr[p]={0};//初始没有颜色
		if(l==r){
			return ;
		}
		build(lc(p),l,mid(l,r));
		build(rc(p),mid(l,r)+1,r);
		push_up(p);
	}
	
	void update(int p,int l,int r,int L,int R,int k){
	//	cout<<p<<endl;
		if(tr[p].color==k){//修改优化,当当前大区间和染色颜色一样,不需要染色
			return ;
		}
		if(l>=L&&r<=R){
			tr[p].color=k;
			push_down(p,l,r);
			return ;
		}
		push_down(p,l,r);
		if(L<=mid(l,r))
			update(lc(p),l,mid(l,r),L,R,k);
		if(R>mid(l,r))
			update(rc(p),mid(l,r)+1,r,L,R,k);
		push_up(p);
	}
	
	int query(int p,int l,int r,int k){//本题只需要单点查询
		if(tr[p].color!=-1){
			return tr[p].color;//查询优化,当一个大区间是齐色不需要向下查询
		}
		if(l==k&&k==r){//到底了
			return tr[p].color;
		}
		push_down(p,l,r);
		int res=0;
		if(k<=mid(l,r))
			res=query(lc(p),l,mid(l,r),k);
		else
			res=query(rc(p),mid(l,r)+1,r,k);
		push_up(p);
		return res;
	}
};
LazySegmentTree lst;
2024/11/3 13:19
加载中...