「IOI2014」Wall 砖墙

题目描述

给定一个初始元素为 \(0\) 的数列,以及 \(K\) 次操作:

  • 将区间 \([L, R]\) 中的元素对 \(h\)\(max\)
  • 将区间 \([L, R]\) 中的元素对 \(h\)\(min\)

解题思路

首先要能看出来这是一道线段树的题。
那么我们要如何建立一个节点呢?
首先,对于每一个线段树上的节点,我们记两个标记 \(Min\)\(Max\)
因为题目涉及到 \(min\)\(max\) 操作,所以应该不难想到设两个这样的标记。
这两个标记的意义:

  • \(Min[rt]\) 表示编号为 \(rt\) 的节点包含的区间的 \(min\) 值标记
  • \(Max[rt]\) 表示编号为 \(rt\) 的节点包含的区间的 \(max\) 值标记

怎么好像和没讲一样
因为题目最后只询问每一个叶子节点的信息,所以我们并不在乎节点中有些什么值。
我们只需要对与每一个节点及两个标记: \(Min\)\(Max\) ,因为我们需要这两个来更新叶子)。
而这两个标记是可以通过区间更新来维护的。

如何处理标记

处理标记有三个操作:初始化、打标记和下传标记。
简单分析一下初始化:
由于我们的标记是用来更新子节点的(即儿子的标记对父亲的 \(Max\)\(max\),对父亲的 \(Min\)\(min\)
所以我们就把 \(Max\) 赋值为极小值,\(Min\) 赋值为极大值:

Max[rt] = 0, Min[rt] = 0x3f3f3f3f;

然后再来看打标记:

inline void fMax(int rt, int h) { Min[rt] = max(Min[rt], h), Max[rt] = max(Max[rt], h); }

inline void fMin(int rt, int h) { Min[rt] = min(Min[rt], h), Max[rt] = min(Max[rt], h); }

其实这两个函数本质是是一样的
我们来分析一下:
如果我们把一段区间对 \(h\)\(max\),那么这段区间的 \(Min\) 标记和 \(Max\) 标记都应该对 \(h\)\(max\)
这个我不作具体分析:你们可以自己想一想为什么也可以感性理解一下
所以打标记就讲完了 \(QwQ\)

最后再来看下传标记 \(pushdown\)

inline void pushdown(int rt) {
	fMin(lc(rt), Min[rt]), fMin(rc(rt), Min[rt]);
	
	fMax(lc(rt), Max[rt]), fMax(rc(rt), Max[rt]);
	
	Max[rt] = 0, Min[rt] = 0x3f3f3f3f;	
}

其实这个和打标记差不多,就是用父亲的信息更新儿子。

如何输出答案

这个其实只要遍历一遍线段树,把叶子节点的 \(Max\)\(Min\) 输出即可。


细节注意事项

  • 线段树空间开 \(4\)
  • 记得 \(pushdown\) 后也要初始化

参考代码

/*--------------------------------
  Author: The Ace Bee
  Blog: www.cnblogs.com/zsbzsb
  This code is made by The Ace Bee
--------------------------------*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>

#define rg register

using namespace std;

template < typename T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while (!isdigit(c)) f |= (c == '-'), c = getchar();
	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
	s = f ? -s : s;
}

const int _ = 2000010;

int n, k, Min[_ << 2], Max[_ << 2];

inline int lc(int rt) { return rt << 1; }

inline int rc(int rt) { return rt << 1 | 1; }

inline void fMax(int rt, int h) { Min[rt] = max(Min[rt], h), Max[rt] = max(Max[rt], h); }

inline void fMin(int rt, int h) { Min[rt] = min(Min[rt], h), Max[rt] = min(Max[rt], h); }

inline void pushdown(int rt) {
	fMin(lc(rt), Min[rt]), fMin(rc(rt), Min[rt]);
	
	fMax(lc(rt), Max[rt]), fMax(rc(rt), Max[rt]);
	
	Max[rt] = 0, Min[rt] = 0x3f3f3f3f;
}

inline void update(int rt, int l, int r, int x, int y, int h, int t) {
	if (x <= l && r <= y) {
		if (t == 1)
			return fMax(rt, h);
		else
			return fMin(rt, h);
	}
	
	int mid = (l + r) >> 1;

	pushdown(rt);
 	
	if (x <= mid) update(lc(rt), l, mid, x, y, h, t);
	
	if (y > mid) update(rc(rt), mid + 1, r, x, y, h, t);
}

inline void query(int rt, int l, int r) {
	if (l == r) { printf("%d\n", Max[rt]); return ; }
	
	int mid = (l + r) >> 1;
	
	pushdown(rt);
	
	query(lc(rt), l, mid);

	query(rc(rt), mid + 1, r);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	read(n), read(k);

	for (rg int i = 1; i <= n << 2; ++i)
		Max[i] = 0, Min[i] = 0x3f3f3f3f;

	for (rg int t, l, r, h, i = 1; i <= k; ++i)
		read(t), read(l), read(r), read(h), update(1, 1, n, l + 1, r + 1, h, t);

	query(1, 1, n);
	
	return 0;
}

完结撒花 \(qwq\)

posted @ 2019-10-02 22:43  Sangber  阅读(248)  评论(1编辑  收藏  举报