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& ...
随机推荐
- 1.nginx 防注入
http://blog.kukafei520.net/html/2013/773.html
- 【嵌入式】——arm裸机开发 step by step 之 串口通信
一.在使用S5PV210的串口发送和接收的时候,首先要对S5PV210的串口进行配置,我们使用轮询方式时的配置有哪些? 1.配置GPIO,使对应管脚作为串口的发送和接收管脚 GPA0 0 1 管脚 2 ...
- 【异常】IOException parsing XML document from class path resource [xxx.xml]
1.IDEA导入项目运行出现异常 org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing ...
- 对比几种在ROS中常用的几种SLAM算法
在此因为要总结写一个文档,所以查阅资料,将总结的内容记录下来,欢迎大家指正! 文章将介绍使用的基于机器人操作系统(ROS)框架工作的SLAM算法. 在ROS中提供的五种基于2D激光的SLAM算法分别是 ...
- Kafka设计解析(二):Kafka High Availability (上)
转自:http://www.infoq.com/cn/articles/kafka-analysis-part-2/ Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦 ...
- Windows IOT 开发入门(硬件入门)
接上文,在准备工作完成之后.接下来应该要熟悉硬件和架构了. 以下是一个简易物联网架构设计图 关于微软云这里就不说太多了.有兴趣的朋友可以去这里了解更多https://www.azure.cn/. 在上 ...
- python numpy访问行列元素的方法
import numpy as np a = np.array([[2,1],[10,5]]) print(a) print(a[:,1])#col 1 print(a[1])#row 1 print ...
- 常用RAID简介_001
RAID定义 RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘, ...
- Request获取用户真实IP(用作白名单过滤)
在Servlet里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid,Nginx等反向代理软件就不能 ...
- php无限分类二
1.读取数据数据 /** * 所有分类数据 * */ public function categoryData(){ $order = 'id,sort asc'; $res = $this-> ...