UVA1232 - SKYLINE(段树部分的变化)
UVA1232 - SKYLINE(线段树区间改动)
题目大意:依照顺序盖楼。假设这个位置(当前要盖的楼覆盖范围内)要新建的楼的高度>=之前就有的最大高度,那么就+1.最后输出这个+1的总数。
解题思路:线段树区间改动值,而且每次改动的时候返回改动的位置总数。
由于可能左右子树的高度会有不同。所以这里增加一个sign来表示左右这一段是否高度一致。
代码:
#include <cstdio>
#include <cstring>
const int maxn = 1e5 + 5;
#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1) + 1)
struct Node {
int l, r, h, sign;
void set (int l, int r, int h, int sign) {
this->l = l;
this->r = r;
this->h = h;
this->sign = sign;
}
}node[4 * maxn];
void pushdown(int u) {
node[lson(u)].sign = node[rson(u)].sign = 1;
node[lson(u)].h = node[rson(u)].h = node[u].h;
node[u].sign = 0;
node[u].h = 0;
}
void pushup (int u) {
node[u].l = node[lson(u)].l;
node[u].r = node[rson(u)].r;
if (node[lson(u)].sign && node[rson(u)].sign && node[lson(u)].h == node[rson(u)].h) {
node[u].sign = 1;
node[u].h = node[lson(u)].h;
} else
node[u].sign = node[u].h = 0;
}
void build (int u, int l, int r) {
if (l == r)
node[u].set(l, r, 0, 1);
else {
int m = (l + r) / 2;
build(lson(u), l, m);
build(rson(u), m + 1, r);
pushup(u);
}
}
int update (int u, int l, int r, int h) {
if (node[u].sign && node[u].h > h)
return 0;
if (node[u].l >= l && node[u].r <= r && node[u].sign) {
node[u].h = h;
return node[u].r - node[u].l + 1;
}
int ret = 0;
int m = (node[u].l + node[u].r) / 2;
if (node[u].sign)
pushdown(u);
if (l <= m)
ret += update (lson(u), l, r, h);
if (r > m)
ret += update (rson(u), l, r, h);
pushup(u);
return ret;
}
int main () {
int T;
int n, l, r, h, ans;
scanf ("%d", &T);
while (T--) {
build(1, 1, maxn);
scanf ("%d", &n);
ans = 0;
for (int i = 0; i < n; i++) {
scanf ("%d%d%d", &l, &r, &h);
ans += update (1, l + 1, r, h);
}
printf ("%d\n", ans);
}
return 0;
}
UVA1232 - SKYLINE(段树部分的变化)的更多相关文章
- HDU ACM 4578 Transformation->段树-间隔的变化
分析:复杂的经营分部树. 只有一个查询操作,这是要求[l,r]的数量之间p钍总和.并不是所有的查询所有节点,会议TLE.最好的是查询部件[a.b].所有这个区间值我们是平等的,即能返回(b-a+1)* ...
- UVA11992 - Fast Matrix Operations(段树部分的变化)
UVA11992 - Fast Matrix Operations(线段树区间改动) 题目链接 题目大意:给你个r*c的矩阵,初始化为0. 然后给你三种操作: 1 x1, y1, x2, y2, v ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- ZOJ 1610 间隔染色段树
要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水 水可太暴力 段树: #include "stdio.h" #include ...
- HDU 1394 Minimum Inversion Number (数据结构-段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...
- BZOJ 2588 Count on a tree (COT) 是持久的段树
标题效果:两棵树之间的首次查询k大点的权利. 思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树. 由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法.详见代码. CODE: ...
- lintcode-439-线段树的构造 II
439-线段树的构造 II 线段树是一棵二叉树,他的每个节点包含了两个额外的属性start和end用于表示该节点所代表的区间.start和end都是整数,并按照如下的方式赋值: 根节点的 start ...
- lintocde-247-线段树的查询 II
247-线段树的查询 II 对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现 ...
随机推荐
- (七)unity4.6Ugui中国教程文档-------摘要-UGUI Auto Layout
大家好,我是太阳广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityman ...
- “AIR SDK 0.0: AIR SDK location “...\devsdks\AIRSDK\Win” does not exist.”问题解决~
原文同步至:http://www.waylau.com/air-sdk-0-0-air-sdk-location-does-not-exist-address/ 导入AS3项目时提示“AIR SDK ...
- ASP.NET 应用程序生命周期
1.请求到达IIS服务器,IIS根据文件后缀找到对应的ISAPI(Internet Server API)扩展来处理,这个配置可在网站属性里的“根目录”选项卡中的“配置”里看到.可以看到,ashx.a ...
- Cluster Table
对簇表来说,总是要先创建簇段(cluster segment).然后将表关联到cluster segment里.由此可知,簇表也是虚拟表,没有对应的segment,簇表对应的是cluster segm ...
- 重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter
原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresente ...
- MyBatis系列教程(六)-- 与Spring综合(Integrate with Spring)
其它工具或技术需要使用: 项目管理工具 : Maven 前台WEB图库:JSP 其他框架:Spring, Spring MVC 数据库 : Derby Maven的Web项目 Maven Depend ...
- perl操作sqlserver实现BCP
#!C:\Perl64\bin #由BCP备份和恢复SQLSERVER指定表 use 5.014; #加载用户和password型材 my $username ; my $passwd; ##得到us ...
- [LeetCode145]Binary Tree Postorder Traversal
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- [LeetCode141]Linked List Cycle
题目:Given a linked list, determine if it has a cycle in it. 判断一个链表是否有环 代码: /** * Definition for singl ...
- web引用和服务引用
原文:web引用和服务引用 在VS2010环境下开发C#的winform程序或者WPF时,会碰到调用web引用的问题. 1.添加一个服务引用时,会在app.config里生成basicHttpBind ...