博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ 3277 --- City Horizon】离散化+线段树
阅读量:2038 次
发布时间:2019-04-28

本文共 3344 字,大约阅读时间需要 11 分钟。

【POJ 3277 --- City Horizon】离散化+线段树

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

 

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 Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.

Input

Line 1: A single integer: N
Lines 2.. N+1: Input line i+1 describes building i with three space-separated integers: AiBi, and Hi

Output

Line 1: The total area, in square units, of the silhouettes formed by all N buildings

Sample Input

42 5 19 10 46 8 24 6 3

Sample Output

16

Hint

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.

Source

 

解题思路

线段树+离散化 因为它们的宽度太大,无法存储所以先离散,然后再根据按从低到高的顺序更新线段树。

AC代码1:

#include 
#include
#include
#include
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1typedef long long ll;const int MAXN = 4e4+5;ll sum[MAXN<<3];int add[MAXN<<3],sl[MAXN<<3],sr[MAXN<<3];vector
v,vec;struct Node{ int a,b,h;};Node node[MAXN];bool compare(Node a,Node b){ return a.h
>1; build(lson,l,mid); build(rson,mid,r);}void update(int root,int l,int r,int L,int R,int num){ if(l>=L && r<=R) { sum[root]=(ll)num*(sr[root]-sl[root]); add[root]=num; return; } if(l+1==r) return; push_down(root); int mid=(l+r)>>1; if(L<=mid) update(lson,l,mid,L,R,num); if(R>=mid) update(rson,mid,r,L,R,num); push_up(root);}int main(){ SIS; int n; cin >> n; for(int i=0;i
> node[i].a >> node[i].b >> node[i].h; v.push_back(node[i].a); v.push_back(node[i].b); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); int len=v.size(); build(1,1,len); sort(node,node+n,compare); for(int i=0;i

AC代码2:

#include 
#include
#include
#include
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1typedef long long ll;const int MAXN = 4e4+5;ll sum[MAXN<<3];int add[MAXN<<3],sl[MAXN<<3],sr[MAXN<<3];vector
v,vec;struct Node{ int a,b,h;};Node node[MAXN];bool compare(Node a,Node b){ return a.h
>1; build(lson,l,mid); build(rson,mid,r);}void update(int root,int l,int r,int L,int R,int num){ if(L<=sl[root] && R>=sr[root]) { sum[root]=(ll)num*(sr[root]-sl[root]); add[root]=num; return; } if(l+1==r) return; push_down(root); int mid=(l+r)>>1; if(L<=sr[lson]) update(lson,l,mid,L,R,num); if(R>=sl[rson]) update(rson,mid,r,L,R,num); push_up(root);}int main(){ SIS; int n; cin >> n; for(int i=0;i
> node[i].a >> node[i].b >> node[i].h; v.push_back(node[i].a); v.push_back(node[i].b); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); int len=v.size(); build(1,1,len); sort(node,node+n,compare); for(int i=0;i

 

转载地址:http://qiyof.baihongyu.com/

你可能感兴趣的文章
ColorMatrix
查看>>
Android图片处理(Matrix,ColorMatrix)
查看>>
xcode import<xx/xx.h> 头文件报错
查看>>
各种开源协议License明细
查看>>
cmake命令 安装、用法简介
查看>>
Android数据库安全解决方案,使用SQLCipher进行加解密
查看>>
cocos2d-x 环境配置-Mac配置篇
查看>>
GoldWave用法简介
查看>>
cocos2d-x避免手动修改android.mk文件来编译
查看>>
XMPPFramewok的使用
查看>>
快速傅立叶变换算法 FFT
查看>>
Android™ 2.1 android.R.drawable Icon Resources
查看>>
Java加密技术(三)——PBE算法
查看>>
ZXingObjC 崩溃问题解决方法
查看>>
iOS视图创建初始化的一些工厂方法
查看>>
iphone开发中sqlite3的操作说明(转载)
查看>>
File Upload Download For iOS
查看>>
iOS关闭App带动画退出
查看>>
Android性能优化的——Java(Android)代码优化
查看>>
Eclipse启动时fail to create Java Virtual Machine问题的解决
查看>>