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 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现 ...
随机推荐
- HQApi命令行接口配置
执行的命令行前准备 在您的个人文件夹中第一次创建 型材client.properties 如下面 cd C:\Users\scnyli\ mkdir ".hq" 创建一个 clie ...
- JQuery+CSS3实现封装弹出登录框效果
原文:JQuery+CSS3实现封装弹出登录框效果 上次发了一篇使用Javascript来实现弹出层的效果,这次刚好用了JQuery来实现,所以顺便记录一下: 因为这次使用了Bootstrap来做一个 ...
- 查看hadoop管理页面,修改本地hosts,Browse the filesystem
问题: hadoop管理界面,ip:50070,中点击Browse the filesystem会出现网页无法访问,看地址栏,是集群中的主机名::50075/browseDirectory.jsp?n ...
- c# ThreadPoold使用心得
于c#多线程编程经常使用的线程,但是,因为线程的创建和销毁是非常资源 - 成本非常大.因此,我们使用线程池来解决问题, 在线程池的开始是申请一定数量的线程系统.和维护,有任务时间,假设你有空闲的线程, ...
- 探索Android该Parcel机制(上)
一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象. 主要应用是利用外部存储设备保存对象状 ...
- LeetCode: Multiply Strings. Java
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 怎样设制 select 不可编辑 仅仅读
1. <select style="width:195px" name="role" id="role" onfocus=" ...
- eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge found
eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge fou ...
- 【Linux探索之旅】第一部分第二课:下载Linux,免费的噢
内容简介 1.第一部分第二课:下载Linux,免费的噢 2.第一部分第三课预告:测试并安装Ubuntu 下载Linux,免费的噢 大家好,上一课我们认识了非常“霸气侧漏”的Linux操作系统. 也知道 ...
- 注意事项: Solr设备 Hello World
试用 Solr-4.10.2 一 shards, 这两款机器 一是垃圾 rm -r example/solr/collection1/data/* 启动一个 node cd example java ...