http://www.lydsy.com/JudgeOnline/problem.php?id=1645

这题的方法很奇妙啊。。。一开始我打了一个“离散”后的线段树。。。。。。。。。。。。。果然爆了。。(因为压根没离散)

这题我们可以画图知道,每2个点都有一个区间,而这个区间的高度是一样的,因此,我们只需要找相邻的两个点,用他们的距离×这个区间的高度就是这块矩形的面积。

将所有这样的矩形累计起来就是答案了。

因此线段树就离散到了O(n)的大小。。真神。。

只需要维护点的位置,然后维护区间最值即可(因为这是线段长度而不是两点长度,即r-l+1,所以要用一定的技巧,将这些坐标都往左边缩1,原因很简单,画图得知)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%lld", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
typedef long long ll;
#define lc x<<1
#define rc x<<1|1
#define lson l, m, lc
#define rson m+1, r, rc
#define MID (l+r)>>1
const int N=40005;
int n, b[N+N];
ll ans;
struct dat { int x, y, h; }a[N];
struct nod { int h, flg; }t[N<<4];
void pushup(int x) { t[x].h=max(t[lc].h, t[rc].h); }
void pushdown(int x) {
if(t[x].flg) {
int flg=t[x].flg;
t[x].flg=0;
t[lc].flg=max(t[lc].flg, flg); t[lc].h=max(t[lc].h, flg);
t[rc].flg=max(t[rc].flg, flg); t[rc].h=max(t[rc].h, flg);
}
}
void update(int l, int r, int x, int L, int R, int fix) {
pushdown(x);
if(L<=l && r<=R) {
t[x].flg=fix;
t[x].h=max(t[x].h, fix);
return;
}
int m=MID;
if(L<=m) update(lson, L, R, fix); if(m<R) update(rson, L, R, fix);
pushup(x);
}
int query(int l, int r, int x, int L) {
pushdown(x);
if(l==r) return t[x].h;
int m=MID;
if(L<=m) return query(lson, L);
else return query(rson, L);
}
int main() {
read(n);
for1(i, 1, n) read(a[i].x), read(a[i].y), read(a[i].h), b[i<<1]=a[i].x, b[(i<<1)-1]=a[i].y;
int sz=n<<1;
sort(b+1, b+1+sz);
for1(i, 1, n) a[i].x=lower_bound(b+1, b+1+sz, a[i].x)-b, a[i].y=lower_bound(b+1, b+1+sz, a[i].y)-b;
for1(i, 1, n)
update(1, sz, 1, a[i].x, a[i].y-1, a[i].h);
for1(i, 1, sz-1)
ans+=(ll)query(1, sz, 1, i)*(ll)(b[i+1]-b[i]);
print(ans);
return 0;
}

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

4
2 5 1
9 10 4
6 8 2
4 6 3

Sample Output

16

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.

HINT

Source

【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)的更多相关文章

  1. BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化

    Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...

  2. bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】

    bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...

  3. [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树

    链接 题意:N个矩形块,交求面积并. 题解 显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值 将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可 ...

  4. 1645: [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: ...

  5. BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

    BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...

  6. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  7. 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 ...

  8. 【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. [POJ] 3277 .City Horizon(离散+线段树)

    来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...

随机推荐

  1. 加密PDF为只读模式

        文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  2. Java计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)

    程序员都很懒,你懂的! java程序员在实际的开发中会遇到很多的单位换算问题.今天我给大家带来的是关于计算机硬盘大小的换算.多数情况下,一般要求 b,kb,mb,gb,tb,pb之间的大小转换,我们都 ...

  3. UNIX环境编程初步认识——编程环境搭建

     前言 前期学习了Linux的一些基本知识后,在借助前期的学习的基础上想再初步认识一下操作系统的一些环境编程体系相关知识,当中环境的配置和搭建费了非常大的劲,须要一点点摸索和尝试,下边是环境搭建的 ...

  4. 关于RHEL6下桥网配置的写法(ifcfg-eth0,ifcfg-br0) / 在阿里云的CentOS上安装docker

    Posted on 2011-07-28 16:46 zhousir1991 阅读(1978) 评论(0) 编辑 收藏 以下仅仅是我在做练习的时候下的环境,参照写即可:  [root@desktop2 ...

  5. Cordova 快速入门记录

    本篇文章由:http://xinpure.com/cordova-quick-start-recording/ 记一笔 Cordova 官网入门文档 Get Started Fast,言简意该.通俗易 ...

  6. Nginx+Windows负载均衡(转载)

    一.下载Nginxhttp://nginx.org/download/nginx-1.0.8.zip解压到C:\nginx目录下二.在两台服务器上分别建一个网站:S1:192.168.16.35:80 ...

  7. HttpClient设置编码类型

    笔者引用的是commons-httpclient这个jar包httpclient 可是通过get/post方式获取带有中文页面的html文件时.返回的是乱码,在网上找了非常久.最终找到一个合适的: H ...

  8. Objective-C的内存管理(一)黄金法则的理解

    转自:http://blog.csdn.net/lonelyroamer/article/details/7666851 一.内存管理黄金法则: The basic rule to apple is ...

  9. 动态更新highcharts数据

    <!doctype html> <html> <head> <script type="text/javascript" src=&quo ...

  10. @XStreamAlias使用

    @XStreamAlias使用 一. 特点: 简化的API; 无映射文件; 高性能,低内存占用; 整洁的XML; 不需要修改对象;支持内部私有字段,不需要setter/getter方法 提供序列化接口 ...