题目描述

给定一个初始元素为 \(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\)

「IOI2014」Wall 砖墙的更多相关文章

  1. 4364: [IOI2014]wall砖墙

    4364: [IOI2014]wall砖墙 链接 分析: 线段树,维护一个最大值,一个最小值. 代码: #include<bits/stdc++.h> ],*p1 = buf,*p2 = ...

  2. 「杂烩」精灵魔法(P1908逆序对弱化版)

    「杂烩」精灵魔法(P1908逆序对弱化版) 题面: 题目描述 \(Tristan\)解决了英灵殿的守卫安排后,便到达了静谧的精灵领地--\(Alfheim\) .由于$ Midgard$ 处在$ Al ...

  3. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  4. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  5. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  6. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  7. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  8. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  9. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

随机推荐

  1. Python(四)生成器 和 杨辉三角

    学习链接: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143177992 ...

  2. Java面向对象编程 -4.2

    声明static定义方法 这个时候对于程序而言方法就有了两种:static方法 非static方法. 这两个方法之间在调用上就有了限制: static方法只允许调用static属性或static方法 ...

  3. go.php

    <?php $t_url=$_GET['url']; if(!empty($t_url)) { preg_match('/(http|https):\/\//',$t_url,$matches) ...

  4. Plastic Bottle Manufacturer -Plastic Bottle Forming Process

    As a professional cosmetic bottle manufacturer, we know that plastic bottles are part of the rubber ...

  5. Codeforces Round #619 (Div. 2) A. Three Strings

    You are given three strings aa , bb and cc of the same length nn . The strings consist of lowercase ...

  6. IIS 7.5 URL重写参数

    URL 重写规则由以下部分组成: 模式 - 可以理解为规则,分通配符和正则匹配     条件 - 可以理解为字符串     操作 - 操作用于指定如果URL字符串与规则模式匹配并且满足所有规则条件时应 ...

  7. intellij idea 整合springboot和mybatis

    参考: http://blog.csdn.net/winter_chen001/article/details/77249029

  8. Springboot三层架构

    control调用service调用dao

  9. YUM方式安装LAMP

    本文介绍两种方法yum安装LAMP, 方法1: 通过httpd的php模块方式安装LAMP 方法2: 通过php-fpm方式安装LAMP 安装环境:CentOS Linux release 7.5.1 ...

  10. 【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)

    题意: 输入两个正整数N和K(N<=40000,K<=2500),分别为学生和课程的数量.接下来输入K门课的信息,先输入每门课的ID再输入有多少学生选了这门课,接下来输入学生们的ID.最后 ...