【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线
Description
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings. The entire horizon is represented by a number line with N (1 <= N <= 40,000) buildings. Building i's silhouette has a base that spans locations A_i through B_i along the horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1 <= H_i <= 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
N个矩形块,交求面积并.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i
Output
* Line 1: The total area, in square units, of the silhouettes formed by all N buildings
Sample Input
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
OUTPUT DETAILS:
The first building overlaps with the fourth building for an area of 1
square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.
题解:一看就是离散化+线段树的题,可以先将h离散,再按a,b排序,用线段树维护扫描线来做,不过这样比较麻烦。
因为本题中所有的矩形底边都在同一直线上,所以我们可以把这些矩形整体看成一棵线段树,线段树的区间就是每个a,b,线段树的权值就是h,然后按h排序,从小到大一个一个加到线段树里就行了。
注意:a和b离散化后是2*40000的空间,所以线段树要开8*40000!2*4==8!!!
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=40010;
typedef long long ll;
struct edges
{
ll pos,org;
}s[maxn<<1];
struct house
{
ll sa,sb,sh;
}p[maxn];
int n,m;
ll ts[maxn<<3],tag[maxn<<3],ref[maxn<<1];
bool cmp1(edges a,edges b)
{
return a.pos<b.pos;
}
bool cmp2(house a,house b)
{
return a.sh<b.sh;
}
void pushdown(int l,int r,int x)
{
if(tag[x])
{
int mid=l+r>>1;
ts[lson]=(ref[mid]-ref[l])*tag[x],ts[rson]=(ref[r]-ref[mid])*tag[x];
tag[lson]=tag[rson]=tag[x];
tag[x]=0;
}
}
void pushup(int x)
{
ts[x]=ts[lson]+ts[rson];
}
void updata(int l,int r,int x,int a,int b,ll v)
{
if(a<=l&&r<=b)
{
ts[x]=(ref[r]-ref[l])*v;
tag[x]=v;
return ;
}
pushdown(l,r,x);
int mid=l+r>>1;
if(b<=mid) updata(l,mid,lson,a,b,v);
else if(a>=mid) updata(mid,r,rson,a,b,v);
else updata(l,mid,lson,a,b,v),updata(mid,r,rson,a,b,v);
pushup(x);
}
int main()
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&p[i].sa,&p[i].sb,&p[i].sh);
s[i*2-1].pos=p[i].sa,s[i*2].pos=p[i].sb;
s[i*2-1].org=s[i*2].org=i;
}
sort(s+1,s+2*n+1,cmp1);
for(i=1;i<=2*n;i++)
{
if(s[i].pos>ref[m]) ref[++m]=s[i].pos;
if(s[i].pos==p[s[i].org].sa) p[s[i].org].sa=m;
else p[s[i].org].sb=m;
}
sort(p+1,p+n+1,cmp2);
for(i=1;i<=n;i++)
updata(1,m,1,p[i].sa,p[i].sb,p[i].sh);
printf("%lld",ts[1]);
return 0;
}
【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树的更多相关文章
- BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化
Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- bzoj1645 [Usaco2007 Open]City Horizon 城市地平线
Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at ...
- [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树
链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
随机推荐
- WPF路由事件二:路由事件的三种策略
一.什么是路由事件 路由事件是一种可以针对元素树中的多个侦听器而不是仅仅针对引发该事件的对象调用处理程序的事件.路由事件是一个CLR事件. 路由事件与一般事件的区别在于:路由事件是一种用于元素树的事件 ...
- stm32独立看门狗
转载:http://blog.sina.com.cn/s/blog_7f1dc53b01010mqa.html 实验现象: 开始LED1亮,LED2熄灭,若不隔时间按KEY1则发现LED2因独立看门狗 ...
- 等待事件 wait event
衡量数据库状况, 优化数据库等 当一个进程连接到数据库后, 进程所经历的种种等待就开始被记录, 并且通过一系列的性能视图进行展示, 通过等待事件用户可以很快发现数据库的性能瓶颈, 从而进行针对性能的优 ...
- jQuery 中的编程范式
浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...
- 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)
背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...
- MVC路由机制(转)
今天我来缕一下MVC的路由机制,因为使用MVC已经快一年了,之前也只是上手,没有系统去理会.项目完了,结合实际使用,回过头来深入一下. MVC 学习索引(点击即可) 一个请求进入IIS后 传统ASP. ...
- linux命令详解之netstat
今天在使用linux的时候,要查看端口号,但是不知道要使用哪一个命令所以就学习了一下,原来是使用netstat,接下来给大家一起来学习. 一.netstat介绍 1.1.简介 Netstat 命令用于 ...
- mysql中如何在创建数据库的时候指定数据库的字符集?
需求描述: 在创建DB的时候指定字符集. 操作过程: 1.使用create database语句创建数据库 mysql> create database if not exists test03 ...
- oracle_存储过程_没有参数_更新过期申请单以及写日志事务回滚
CREATE OR REPLACE PROCEDURE A_MEAS_MIINSP_PLAN_UPDATEASvs_msg VARCHAR2(4000);log_body VARCHAR2(400); ...
- Java精选笔记_面向对象(构造方法、this关键字、static关键字、内部类)
构造方法constructor构造器 构造方法的定义 [修饰符] 类名 (形式参数列表){ //语句 } 构造器用于构造该类的实例.作用:用来初始化对象!一般由系统在创建对象(即类的 ...