UVALive - 4108

Time Limit: 3000MS     64bit IO Format: %lld & %llu

Submit Status uDebug

Description

 

The skyline of Singapore as viewed from the Marina Promenade (shown on the left) is one of the iconic scenes of Singapore. Country X would also like to create an iconic skyline, and it has put up a call for proposals. Each submitted proposal is a description of a proposed skyline and one of the metrics that country X will use to evaluate a proposed skyline is the amount of overlap in the proposed sky-line.

<tex2html_verbatim_mark>

As the assistant to the chair of the skyline evaluation committee, you have been tasked with determining the amount of overlap in each proposal. Each proposal is a sequence of buildings, b1b2,..., bn <tex2html_verbatim_mark>, where a building is specified by its left and right endpoint and its height. The buildings are specified in back to front order, in other words a building which appears later in the sequence appears in front of a building which appears earlier in the sequence.

The skyline formed by the first k <tex2html_verbatim_mark>buildings is the union of the rectangles of the first k <tex2html_verbatim_mark>buildings (see Figure 4). The overlap of a building, bi <tex2html_verbatim_mark>, is defined as the total horizontal length of the parts of bi <tex2html_verbatim_mark>, whose height is greater than or equal to the skyline behind it. This is equivalent to the total horizontal length of parts of the skyline behind bi<tex2html_verbatim_mark>which has a height that is less than or equal to hi <tex2html_verbatim_mark>, where hi <tex2html_verbatim_mark>is the height of building bi <tex2html_verbatim_mark>. You may assume that initially the skyline has height zero everywhere.

Input

The input consists of a line containing the number c <tex2html_verbatim_mark>of datasets, followed by c <tex2html_verbatim_mark>datasets, followed by a line containing the number ` 0'.

The first line of each dataset consists of a single positive integer, n <tex2html_verbatim_mark>(0 < n < 100000) <tex2html_verbatim_mark>, which is the number of buildings in the proposal. The following n<tex2html_verbatim_mark>lines of each dataset each contains a description of a single building. The i <tex2html_verbatim_mark>-th line is a description of building bi <tex2html_verbatim_mark>. Each building bi <tex2html_verbatim_mark>is described by three positive integers, separated by spaces, namely, li <tex2html_verbatim_mark>, ri <tex2html_verbatim_mark>and hi <tex2html_verbatim_mark>, where li <tex2html_verbatim_mark>and rj <tex2html_verbatim_mark>(0 < li < ri100000) <tex2html_verbatim_mark>represents the left and right end point of the building and hi represents the height of the building.

<tex2html_verbatim_mark>

Output

The output consists of one line for each dataset. The c <tex2html_verbatim_mark>-th line contains one single integer, representing the amount of overlap in the proposal for dataset c<tex2html_verbatim_mark>. You may assume that the amount of overlap for each dataset is at most 2000000.

Note: In this test case, the overlap of building b1 <tex2html_verbatim_mark>, b2 <tex2html_verbatim_mark>and b3 <tex2html_verbatim_mark>are 6, 4 and 4 respectively. Figure 4 shows how to compute the overlap of building b3 <tex2html_verbatim_mark>. The grey area represents the skyline formed by b1 <tex2html_verbatim_mark>and b2 <tex2html_verbatim_mark>and the black rectangle represents b3 <tex2html_verbatim_mark>. As shown in the figure, the length of the skyline covered by b3 <tex2html_verbatim_mark>is from position 3 to position 5 and from position 11 to position 13, therefore the overlap of b3 <tex2html_verbatim_mark>is 4.

Sample Input

1
3
5 11 3
1 10 1
3 13 2
0

Sample Output

14

题意:在线区间[l,r]查询h是最大值的长度,并更新


很明显区间max

但怎么找这个长度overlap呢?

如果当前区间满足ql<=l&&r<=qr&&h>=t[o].mx 那么就可以被完全覆盖

用lazy维护区间完全覆盖中的最大值,h<t[o].lazy时就不用继续找了(因为h不可能覆盖这个区间了),有点像个剪枝

否则必须继续找,下传lazy(无需清空自己的标记,剪枝嘛),合并mx

lazy和mx还有点小细节,比如paint没有用lazy更新mx,其实这不影响,因为先if(h<t[o].lazy) return;了

注意点是区间[i,i+1]

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define m (l+r)/2
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int T,n,ql,qr,h;
struct node{
int mx,lazy;//totally cover
}t[N<<];
inline void pushDown(int o){
if(t[o].lazy>t[lc].lazy) t[lc].lazy=t[o].lazy;
if(t[o].lazy>t[rc].lazy) t[rc].lazy=t[o].lazy;
}
int lap=;
inline void update(int o,int l,int r,int ql,int qr,int h){
if(h<t[o].lazy) return;
if(ql<=l&&r<=qr&&h>=t[o].mx){
t[o].mx=t[o].lazy=h;
lap+=r-l+;
}else{
pushDown(o);
if(ql<=m) update(lson,ql,qr,h);
if(m<qr) update(rson,ql,qr,h);
t[o].mx=max(t[lc].mx,t[rc].mx);
}
} int main(){
T=read();
while(T--){
n=read();
int ans=;
memset(t,,sizeof(t));
for(int i=;i<=n;i++){
ql=read();qr=read()-;h=read();
lap=;
update(,,1e5,ql,qr,h);
//printf("lap %d\n",lap);
ans+=lap;
}
printf("%d\n",ans);
}
}

UVALive - 4108 SKYLINE[线段树]的更多相关文章

  1. UVALive - 4108 SKYLINE (吉司机线段树)

    题目链接 题意:在一条直线上依次建造n座建筑物,每座建筑物建造完成后询问它在多长的部分是最高的. 比较好想的方法是用线段树分别维护每个区间的最小值mi和最大值mx,当建造一座高度为x的建筑物时,若mi ...

  2. UVA 1232 - SKYLINE(线段树)

    UVA 1232 - SKYLINE option=com_onlinejudge&Itemid=8&page=show_problem&category=502&pr ...

  3. 2018牛客网暑假ACM多校训练赛(第四场)E Skyline 线段树 扫描线

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round4-E.html 题目传送门 - https://www.no ...

  4. UVaLive 11525 Permutation (线段树)

    题意:有一个由1到k组成的序列,最小是1 2 … k,最大是 k k-1 … 1,给出n的计算方式,n = s0 * (k - 1)! + s1 * (k - 2)! +… + sk-1 * 0!, ...

  5. UVALive - 3938 (线段树,区间查询)

    思路:详细分析见训练指南.注意可能答案的起点在左区间,终点在右区间 AC代码 #include <stdio.h> #include <algorithm> using nam ...

  6. LA 4108 (线段树)

    区间更新 + 统计更新长度 稍稍不注意就T了 #include<bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1 ...

  7. UVA1232 - SKYLINE(段树部分的变化)

    UVA1232 - SKYLINE(线段树区间改动) 题目链接 题目大意:依照顺序盖楼.假设这个位置(当前要盖的楼覆盖范围内)要新建的楼的高度>=之前就有的最大高度,那么就+1.最后输出这个+1 ...

  8. 数据结构习题 线段树&树状数组

    说明:这是去年写了一半的东西,一直存在草稿箱里,今天整理东西的时候才发现,还是把它发表出来吧.. 以下所有题目来自Lrj的<训练指南> LA 2191 单点修改,区间和  Fenwick直 ...

  9. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

随机推荐

  1. Web.config配置数据库连接

    web.config配置数据库连接   第一种:取连接字符串 string connString = System.Web.Configuration.WebConfigurationManager. ...

  2. 在PHP语言中使用JSON和将json还原成数组

    在之前我写过php返回json数据简单实例,刚刚上网,突然发现一篇文章,也是介绍json的,还挺详细,值得参考.内容如下 从5.2版本开始,PHP原生提供json_encode()和json_deco ...

  3. HTML5学习笔记

    参考资料:http://www.runoob.com/html/html-tutorial.html 1.html5声明.将此html文档标记为html5文档 <!DOCTYPE html> ...

  4. C++_系列自学课程_第_8_课_指针和引用_《C++ Primer 第四版》

    C语言最富有迷幻色彩的部分当属指针部分,无论是指针的定义还是指针的意义都可算是C语言中最复杂的内容.指针不但提供给了程序员直接操作硬件部分的操作接口,还提供给了程序员更多灵活的用法.C++继承这一高效 ...

  5. 【JAVA并发编程实战】12、使用condition实现多线程下的有界缓存先进先出队列

    package cn.study.concurrency.ch14; import java.util.concurrent.locks.Condition; import java.util.con ...

  6. RequireJS入门之一——实现第一个例子

    为什么学习RequireJS? 像我这种菜鸟,会提到海量文章里提到的AMD.JS模块化编程.异步... ... 等等 RequireJS是一个Javascript 文件和模块框架,它可以帮我们去管理j ...

  7. 使用 jQuery & CSS3 制作美丽的照片画廊

    在本教程中,我们将创建一个很好看的照片画廊效果.我们的想法是,以显示专辑作为一个滑块,而当这张专辑被选中,我们将使用一个美丽的照片堆栈展示专辑的图像.在照片堆栈视图,我们可以通过将最上面的图像移动到所 ...

  8. arcgis软件集合

    ArcGIS软件应有尽有,跨越各种不同的ArcGIS版本,包括ArcGIS10.3.ArcGIS10.2.2.ArcGIS10.1.ArcGIS10.0.ArcGIS9.3. 备注:更多的精彩内容请关 ...

  9. iOS 用 SDWebImage 清理图片缓存

    效果图如下: 1.找到 SDWebImage找到SDImageCache类 2.添加如下方法 - (float)checkTmpSize { ; NSDirectoryEnumerator *file ...

  10. db2start启动失败

    db2start启动失败 [db2inst1@localhost ~]$ db2start db2start: error while loading shared libraries: libaio ...