UVA 1232 - SKYLINE(线段树)
UVA 1232 - SKYLINE
题意:按顺序建房。在一条线段上,每一个房子一个高度。要求出每间房子建上去后的轮廓线
思路:线段树延迟更新。一个setv作为高度的懒标记,此外还要在开一个cover表示当前结点一下是否都为同一高度
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) const int N = 100005; int t, n; struct Node {
int l, r, h, setv;
bool cover;
Node() {}
Node(int l, int r) {
this->l = l; this->r = r;
h = 0; setv = 0; cover = true;
}
} node[4 * N]; void pushup(int x) {
node[x].cover = ((node[lson(x)].h == node[rson(x)].h) && node[lson(x)].cover && node[rson(x)].cover);
node[x].h = node[lson(x)].h;
} void pushdown(int x) {
node[lson(x)].setv = max(node[lson(x)].setv, node[x].setv);
node[rson(x)].setv = max(node[rson(x)].setv, node[x].setv);
node[lson(x)].h = max(node[lson(x)].setv, node[lson(x)].h);
node[rson(x)].h = max(node[rson(x)].setv, node[rson(x)].h);
} void build(int l, int r, int x = 0) {
node[x] = Node(l, r);
if (l == r) return;
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
} int query(int l, int r, int v, int x = 0) {
if (node[x].cover && node[x].h > v) return 0;
if (node[x].l >= l && node[x].r <= r && node[x].cover) {
node[x].setv = v;
node[x].h = v;
return node[x].r - node[x].l + 1;
}
int mid = (node[x].l + node[x].r) / 2;
int ans = 0;
pushdown(x);
if (l <= mid) ans += query(l, r, v, lson(x));
if (r > mid) ans += query(l, r, v, rson(x));
pushup(x);
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
build(1, N - 1);
int l, r, h;
int ans = 0;
while (n--) {
scanf("%d%d%d", &l, &r, &h);
ans += query(l, r - 1, h);
}
printf("%d\n", ans);
}
return 0;
}
UVA 1232 - SKYLINE(线段树)的更多相关文章
- 2018牛客网暑假ACM多校训练赛(第四场)E Skyline 线段树 扫描线
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round4-E.html 题目传送门 - https://www.no ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
- UVA1232 - SKYLINE(段树部分的变化)
UVA1232 - SKYLINE(线段树区间改动) 题目链接 题目大意:依照顺序盖楼.假设这个位置(当前要盖的楼覆盖范围内)要新建的楼的高度>=之前就有的最大高度,那么就+1.最后输出这个+1 ...
- UVA 11992 Fast Matrix Operations(线段树:区间修改)
题目链接 2015-10-30 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=s ...
- UVA 11983 Weird Advertisement(线段树求矩形并的面积)
UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...
- UVA 12436 - Rip Van Winkle's Code(线段树)
UVA 12436 - Rip Van Winkle's Code option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- UVa 1471 Defense Lines - 线段树 - 离散化
题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...
- uva 11525(线段树)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- Windows API 常用函数
.Net中虽然类库很强的,但还是有些时候功能有限,掌握常用的api函数,会给我们解决问题提供另一种思路,下面给出自己常用到的Api函数,以备查询. 知道api函数,但却不知道c#或VB.net该如何声 ...
- VB6学习笔记
1.数据库读取 [工程]菜单的[引用]菜单项,打开引用对话框,选中[Microsoft ActiveX Data Objects 6.1 Library] [工程]菜单的[引用]菜单项,打开引用对话框 ...
- 【oneday_onepage】——Ten Changes To Make A Difference In Your Life
When you want to change something in your life, it can feel overwhelming. Whether it’s losing 50lbs ...
- jQuery(十二);事件绑定
一.bind() bing()用来绑定事件,例如: 二.unbind() unbind()用来解除事件的绑定.例如: 三.on() on()方法用来绑定事件,例如: 四.off() off()方法用来 ...
- SpringMVC深度探险(四) —— SpringMVC核心配置文件详解
在上一篇文章中,我们从DispatcherServlet谈起,最终为读者详细分析了SpringMVC的初始化主线的全部过程.整个初始化主线的研究,其实始终围绕着DispatcherServlet.We ...
- jquery+easyui开发、培训文档
目 录 1.... Accordion(可折叠标签)......................................................................... ...
- WPF中ComboBox使用
1.数据绑定 前台代码: <ComboBox Height="23" HorizontalAlignment="Left" Margin="86 ...
- hashMap put方法 第二行代码 1
public interface Zi<K,V> { //我是接口 //通过Zi<K,V> zi的方式可以让我实现多态 //多态的好处是: //1. 应用程序不必为每一个派生类 ...
- 关于Unity中的光照(五)
Mobile Diffuse Unity自带的一种shader,用的比较多,性能还可以.我们默认创建的unit shader基本和它一致,但是没有参与光照计算,看起来和Mobile Diffuse有区 ...
- Mayi_Maven安装与配置Myeclipse、Idea
一.需要准备的东西 1. JDK 2. Eclipse 3. Maven程序包 二.下载与安装 1. 前往https://maven.apache.org/download.cgi下载最新版的Mave ...