线段树学习

一、维护区间最值

struct node
{
	int l, r;
	ll maxv;
	ll lazy;
}tree[maxn << 2];
ll num[maxn];
inline void update(int k)
{
	tree[k].maxv = max(tree[k << 1].maxv, tree[k << 1 | 1].maxv);
}
void build(int k, int l, int r)
{
	tree[k].r = r, tree[k].l = l;
	if (l == r)
	{
		tree[k].maxv = num[l];
		return;
	}
	int mid = (l + r) / 2;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	update(k);
}
void pushdown(int k)//将点k的懒惰标记下传
{
	if (!tree[k].lazy)
		return;
	tree[k << 1].maxv += tree[k].lazy;
	tree[k << 1 | 1].maxv += tree[k].lazy;
	tree[k << 1].lazy += tree[k].lazy;
	tree[k << 1 | 1].lazy += tree[k].lazy;			//下传点k的标记
	tree[k].lazy = 0;					//清空点k的标记
}
void changeSegment(int k, int l, int r, ll x)
{
	if (tree[k].l >= l && tree[k].r <= r)//如果找到了全部元素都要被修改的区间
	{
		tree[k].maxv += x;
		tree[k].lazy += x;
		return;
	}
	pushdown(k);
	if (l <= tree[k << 1].r)
		changeSegment(k << 1, l, r, x);
	if (r >= tree[k << 1 | 1].l)
		changeSegment(k << 1 | 1, l, r, x);
	update(k);
}
ll query(int k, int l, int r)
{
	if (tree[k].l >= l && tree[k].r <= r)
	{
		return tree[k].maxv;
	}
	pushdown(k);            //只有查询时可以不加
	ll ans = -inf;
	if (tree[k << 1].r >= l)
		ans = max(ans, query(k << 1, l, r));
	if (tree[k << 1 | 1].l <= r)
		ans = max(ans, query(k << 1 | 1, l, r));
	return ans;
}

二、个数取反,询问操作后01串中1的个数

struct node
{
	int l, r;
	int cnt1;
	int lazy;
}tree[maxn << 2];
int num[maxn];
inline void update(int k)
{
	tree[k].cnt1 = tree[k << 1].cnt1 + tree[k << 1 | 1].cnt1;
}
void build(int k, int l, int r)
{
	tree[k].l = l, tree[k].r = r;
	if (l == r)
	{
		tree[k].cnt1 = num[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	update(k);
}
void pushdown(int k)
{
	if (tree[k].lazy == 0)
		return;
	tree[k << 1].cnt1 = tree[k << 1].r - tree[k << 1].l + 1 - tree[k << 1].cnt1;
	tree[k << 1 | 1].cnt1 = tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1 - tree[k << 1 | 1].cnt1;
	tree[k << 1].lazy ^= 1;
	tree[k << 1 | 1].lazy ^= 1;
	tree[k].lazy = 0;
}
void changerange(int k, int l, int r)
{
	if (tree[k].l >= l && tree[k].r <= r)
	{
		tree[k].cnt1 = tree[k].r - tree[k].l + 1 - tree[k].cnt1;
		tree[k].lazy ^= 1;
		return;
	}
	pushdown(k);
	if (tree[k << 1].r >= l)
		changerange(k << 1, l, r);
	if (tree[k << 1 | 1].l <= r)
		changerange(k << 1 | 1, l, r);
	update(k);
}
ll query(int k, int l, int r)
{
	if (tree[k].l >= l && tree[k].r <= r)
	{
		return tree[k].cnt1;
	}
	pushdown(k);
	ll ans = 0;
	if (tree[k << 1].r >= l)
		ans += query(k << 1, l, r);
	if (tree[k << 1 | 1].l <= r)
		ans += query(k << 1 | 1, l, r);
	return ans;
}

三、区间修改,单点修改,区间查询

 可以省略pushdown,直接全部下放(可能会T)

struct node
{
	int l, r;   //该节点的区间下标
	int maxv;	
}tree[maxn << 2];
int num[maxn];
inline void update(int k)	
{
	tree[k].maxv = max(tree[k << 1].maxv, tree[k << 1 | 1].maxv);
	
}
inline void build(int k, int l, int r)
{
	tree[k].l = l, tree[k].r = r;
	if (l == r)		
	{
		tree[k].maxv = num[l];
		return;
	}
	int mid = (l + r) >>1;				
	build(k << 1, l, mid);				
	build(k << 1 | 1, mid + 1, r);		
	update(k);			
}
inline void changeone(int k, int x, int y)		
{
	if (tree[k].l == tree[k].r)		
	{
		tree[k].maxv = y;
		return;
	}
	int mid = (tree[k].l + tree[k].r) >>1;
	if (x <= mid)
		changeone(k << 1, x, y);
	else
		changeone(k << 1 | 1, x, y);
	update(k);
}

inline void changeSegment(int k, int l, int r, int x)
{
	if (!tree[k].maxv)        //******//
		return;
	if (tree[k].l==tree[k].r)
	{
		tree[k].maxv &= x;
		return;
	}
	if (tree[k << 1].r >= l)
		changeSegment(k << 1, l, r, x);
	if (tree[k << 1 | 1].l <= r)
		changeSegment(k << 1 | 1, l, r, x);
	update(k);
}

inline int query(int k, int l, int r)
{
	if (tree[k].l >= l && tree[k].r <= r)
		return tree[k].maxv;
	int ans = 0;
	if (tree[k << 1].r >= l)
		ans = max(ans, query(k << 1, l, r));
	if (tree[k << 1 | 1].l <= r)
		ans = max(ans, query(k << 1 | 1, l, r));
	return ans;
}

四、初始时,区间1~10^18都是0,有以下三种操作

但是可以归结为,区间赋值和区间反转,主要处理离散化、重叠标记下放问题和树上二分

struct node
{
	int l, r;
	int val;			//权值线段树,维护的值是区间的1的个数
	int cov, rev;		//cov是-1代表没变化,0代表赋值0,1代表赋值1
}tree[maxn << 4];
struct Q
{
	ll l, r;
	int ope;
}que[maxn];
void pushup(int k)
{
	tree[k].val = tree[k << 1].val + tree[k << 1 | 1].val;
}
void build(int k, int l, int r)
{
	tree[k].l = l, tree[k].r = r;
	tree[k].cov = -1, tree[k].rev = 0;
	if (l == r)
	{
		tree[k].val = 0;
		return;
	}
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	pushup(k);
}
void settag(int k, int cov, int rev)
{
    //处理结点k的标记
	if (cov != -1)        //如果有覆盖,直接覆盖
	{
		tree[k].cov = cov;
		tree[k].rev = 0;
	}
	else if (rev)         //如果有反转
	{
		if (tree[k].cov != -1)    //本身有覆盖
		{
			tree[k].cov = tree[k].cov ^ 1;
			tree[k].rev = 0;
		}
		else                      //本身无覆盖
		{
			if (tree[k].rev)
				tree[k].rev = 0;
			else
				tree[k].rev = 1;
		}
	}

	if (cov == 1)				//处理子节点k的值
		tree[k].val = tree[k].r - tree[k].l + 1;
	else if (rev == 1)
		tree[k].val = tree[k].r - tree[k].l + 1 - tree[k].val;
	else if (cov == 0)
		tree[k].val = 0;
}
void pushdown(int k)
{
	settag(k << 1, tree[k].cov, tree[k].rev);			//push到两个叶子节点并重置当前点标记
	settag(k << 1 | 1, tree[k].cov, tree[k].rev);
	tree[k].cov = -1, tree[k].rev = 0;
}
void changerange(int k, int l, int r, int ope)
{
	if (tree[k].l >= l && tree[k].r <= r)
	{
		if (ope == 1)
			settag(k, 1, 0);
		else if (ope == 2)
			settag(k, 0, 0);
		else
			settag(k, -1, 1);
		return;
	}
	pushdown(k);
	if (tree[k << 1].r >= l)
		changerange(k << 1, l, r, ope);
	if (tree[k << 1 | 1].l <= r)
		changerange(k << 1 | 1, l, r, ope);
	pushup(k);
}
int query(int k)
{
	if (tree[k].l == tree[k].r)
	{
		return tree[k].l;
	}
	pushdown(k);
	//树上二分,要么往左走,要么往右走
	if (tree[k << 1].val < (tree[k << 1].r - tree[k << 1].l + 1))	
		return query(k << 1);
	else
		return query(k << 1 | 1);
}
ll a[maxn * 3];
void solve()
{
	int n;
	cin >> n;
	ll num = 1;
	a[1] = 1;
	for (int i = 1; i <= n; i++)
	{
		cin >> que[i].ope >> que[i].l >> que[i].r;
		//答案有可能的数就是修改区间的两端及右端点加一
		a[++num] = que[i].l;		
		a[++num] = que[i].r;
		a[++num] = que[i].r + 1;
	}
	//将答案有可能的区间离散化,最后通过a数组查询原值
	sort(a + 1, a + num + 1);
	num = unique(a + 1, a + num + 1) - (a + 1);
	for (int i = 1; i <= n; i++)
	{
		que[i].l = lower_bound(a + 1, a + num, que[i].l) - a;
		que[i].r = lower_bound(a + 1, a + num, que[i].r) - a;
	}
	build(1, 1, num);
	for (int i = 1; i <= n; i++)
	{
		changerange(1, que[i].l, que[i].r, que[i].ope);
		int ans = query(1);
		cout << a[ans] << '\n';
	}
}

可以将lazy合并成一个 

struct node
{
	int l, r;
	int val;			//权值线段树,维护的值是区间的1的个数
	//int cov, rev;		//cov是-1代表没变化,0代表赋值0,1代表赋值1
	int lazy;        //如果是01代表置01,3代表反转
}tree[maxn << 4];
struct Q
{
	ll l, r;
	int ope;
}que[maxn];
void pushup(int k)
{
	tree[k].val = tree[k << 1].val + tree[k << 1 | 1].val;
}
void build(int k, int l, int r)
{
	tree[k].l = l, tree[k].r = r;
	//tree[k].cov = -1, tree[k].rev = 0;
	tree[k].lazy = -1;
	if (l == r)
	{
		tree[k].val = 0;
		return;
	}
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	pushup(k);
}
void settag(int k, int lazy)
{
	//处理结点k的标记
	if (lazy != -1&&lazy!=3)        //如果有覆盖,直接覆盖
	{
		tree[k].lazy = lazy;
	}
	else if (lazy==3)         //如果有反转
	{
		if (tree[k].lazy != -1&&tree[k].lazy!=3)    //本身有覆盖
		{
			tree[k].lazy = tree[k].lazy ^ 1;
		}
		else                      //本身无覆盖
		{
			if (tree[k].lazy==3)
				tree[k].lazy = -1;
			else
				tree[k].lazy = 3;
		}
	}

	if (lazy == 1)				//处理子节点k的值
		tree[k].val = tree[k].r - tree[k].l + 1;
	else if (lazy == 3)
		tree[k].val = tree[k].r - tree[k].l + 1 - tree[k].val;
	else if (lazy == 0)
		tree[k].val = 0;
}
void pushdown(int k)
{
	settag(k << 1, tree[k].lazy);			//push到两个叶子节点并重置当前点标记
	settag(k << 1 | 1, tree[k].lazy);
	tree[k].lazy = -1;
}
void changerange(int k, int l, int r, int ope)
{
	if (tree[k].l >= l && tree[k].r <= r)
	{
		if (ope == 1)
			settag(k, 1);
		else if (ope == 2)
			settag(k, 0);
		else
			settag(k, 3);
		return;
	}
	pushdown(k);
	if (tree[k << 1].r >= l)
		changerange(k << 1, l, r, ope);
	if (tree[k << 1 | 1].l <= r)
		changerange(k << 1 | 1, l, r, ope);
	pushup(k);
}

另一种风格的线段树 

struct qu
{
	int ty;
	ll l, r;
}q[maxn];
struct tag
{
	int re, co;
};
struct node
{
	tag t;
	int val, sz;
}seg[maxn << 4];
tag operator + (const tag& l, const tag& r)
{
	tag a = { 0, -1 };
	if (r.co != -1)
		a.co = r.co;
	else if (r.re)
	{
		if (l.co != -1)
			a.co = l.co ^ 1;
		else
		{
			if (!l.re)
				a.re = 1;
		}
	}
	else
		return l;
	return a;
}
void settag(int k, tag t)
{
	seg[k].t = seg[k].t + t;
	if (t.co == 1)
		seg[k].val = seg[k].sz;
	else if (t.re == 1)
		seg[k].val = seg[k].sz - seg[k].val;
	else if (t.co == 0)
		seg[k].val = 0;
}
void update(int k)
{
	seg[k].val = seg[k << 1].val + seg[k << 1 | 1].val;
}
void pushdown(int k)
{
	settag(k << 1, seg[k].t);
	settag(k << 1 | 1, seg[k].t);
	seg[k].t = { 0, -1 };
}
void build(int k, int l, int r)
{
	seg[k].t = { 0, -1 };
	seg[k].sz = r - l + 1;
	if (l == r)
	{
		seg[k].val = 0;
		return;
	}
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	update(k);
}

void modify(int k, int l, int r, int ql, int qr, tag t)
{
	if (l == ql && r == qr)
	{
		settag(k, t);
		return;
	}
	int mid = (l + r) >> 1;
	pushdown(k);
	if (qr <= mid)
		modify(k << 1, l, mid, ql, qr, t);
	else if (mid < ql)
		modify(k << 1 | 1, mid + 1, r, ql, qr, t);
	else
		modify(k << 1, l, mid, ql, mid, t), modify(k << 1 | 1, mid + 1, r, mid + 1, qr, t);
	update(k);
}
int query(int k, int l, int r)
{
	if (l == r)
	{
		return l;
	}
	int mid = (l + r) >> 1;
	pushdown(k);
	if (seg[k << 1].val < seg[k << 1].sz)
		return query(k << 1, l, mid);
	else
		return query(k << 1 | 1, mid + 1, r);
}
ll a[3 * maxn];
void solve()
{
	int n;
	cin >> n;
	ll num = 2;
	a[1] = 1;
	for (int i = 1; i <= n; i++)
	{
		cin >> q[i].ty >> q[i].l >> q[i].r;
		a[num++] = q[i].l;
		a[num++] = q[i].r;
		a[num++] = q[i].r + 1ll;
	}
	sort(a + 1, a + num);
	num = unique(a + 1, a + num) - (a + 1);
	for (int i = 1; i <= n; i++)
	{
		q[i].l = lower_bound(a + 1, a + num, q[i].l) - a;
		q[i].r = lower_bound(a + 1, a + num, q[i].r) - a;
	}
	build(1, 1, num);
	for (int i = 1; i <= n; i++)
	{
		if (q[i].ty <= 2)
			modify(1, 1, num, q[i].l, q[i].r, tag({ 0, q[i].ty & 1 }));
		else
			modify(1, 1, num, q[i].l, q[i].r, tag({ 1, -1 }));
		int ans = query(1, 1, num);
		cout << a[ans] << endl;
	}
}

五、对区间进行染色,求n次染色(每次颜色都不同)后有多少有多少种不同的颜色

struct node
{
	int l, r;
	int lazy;
}tree[maxn << 2];
int x[maxn], y[maxn], a[maxn * 3];
set<int>st;
void build(int k, int l, int r)
{
	tree[k].l = l, tree[k].r = r, tree[k].lazy = 0;
	if (l == r)
		return;
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
}
void pushdown(int k)
{
	if (!tree[k].lazy)
		return;
	tree[k << 1].lazy = tree[k << 1 | 1].lazy = tree[k].lazy;
	tree[k].lazy = 0;
}
void change(int k, int l, int r, int x)
{
	if (tree[k].l >= l && tree[k].r <= r)
	{
		tree[k].lazy = x;
		return;
	}
	pushdown(k);
	if (tree[k << 1].r >= l)
		change(k << 1, l, r, x);
	if (tree[k << 1 | 1].l <= r)
		change(k << 1 | 1, l, r, x);
}
void query(int k)
{
	if (tree[k].l == tree[k].r && !tree[k].lazy)
		return;
	if (tree[k].lazy)
	{
		st.insert(tree[k].lazy);
		return;
	}
	pushdown(k);
	query(k << 1);
	query(k << 1 | 1);
}
void solve()
{
	st.clear();
	int n;
	cin >> n;
	int cnt = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i] >> y[i];
		a[++cnt] = x[i];
		a[++cnt] = y[i];
		a[++cnt] = y[i] + 1;
	}
	sort(a + 1, a + cnt + 1);
	cnt = unique(a + 1, a + cnt + 1) - (a + 1);
	for (int i = 1; i <= n; i++)
	{
		x[i] = lower_bound(a + 1, a + cnt + 1, x[i]) - a;
		y[i] = lower_bound(a + 1, a + cnt + 1, y[i]) - a;
	}
	build(1, 1, cnt);
	for (int i = 1; i <= n; i++)
	{
		change(1, x[i], y[i], i);
	}
	query(1);
	cout << st.size() << '\n';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值