poj 3277(线段树+离散化)
来源:互联网 发布:深圳招聘淘宝美工学徒 编辑:程序博客网 时间:2024/06/10 08:47
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
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
Output
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.
//坐标 x离散化 离散后求相邻两点的最大值h 接着对应原始的x坐标求面积 这里需要注意离散化卡map 所以可以利用二分查找 还有由于更新会导致左右两个坐标都被覆盖,所以离散化时可以 1 3 5 7。。。这样离散 每次求2 4 6 8的值即可
#include <algorithm>#include <iostream>#include <map>#include <set>#include <cstring>#include <cstdio>using namespace std;#define ll long longconst int N=1e6+5;int n;int l[N],r[N],h[N];set<int>s;set<int>::iterator it;int Map[2*N];ll ans;void init(){ s.clear(); memset(Map,0,sizeof(Map)); memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); memset(h,0,sizeof(h));}struct Node{ int l,r; int Ma;}seg[4*N];void PushDown(int rt){ int t=seg[rt].Ma; if(t>seg[2*rt].Ma)seg[2*rt].Ma=t; if(t>seg[2*rt+1].Ma)seg[2*rt+1].Ma=t;}void Build(int rt,int l,int r){ seg[rt].l=l,seg[rt].r=r; seg[rt].Ma=0; if(l!=r) { Build(2*rt,l,(l+r)/2); Build(2*rt+1,(l+r)/2+1,r); }}void Update(int rt,int l,int r,int val){ if(seg[rt].l==l&&seg[rt].r==r) { if(val>seg[rt].Ma)seg[rt].Ma=val; return; } PushDown(rt); int mid=(seg[rt].l+seg[rt].r)/2; if(r<=mid)Update(2*rt,l,r,val); else if(l>mid)Update(2*rt+1,l,r,val); else { Update(2*rt,l,mid,val); Update(2*rt+1,mid+1,r,val); }}void solve(int rt,int l,int r){ PushDown(rt); if(l==r) { if(r%2==0) { int mi=seg[rt].r; ans+=(ll)(seg[rt].Ma)*(Map[mi+1]-Map[mi-1]); } return; } else { int mid=(l+r)/2; solve(2*rt,l,mid); solve(2*rt+1,mid+1,r); }}int main(){ while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) { scanf("%d%d%d",l+i,r+i,h+i); s.insert(l[i]),s.insert(r[i]); } int cnt=1; for(it=s.begin();it!=s.end();it++) //二分查找时由于是1 3 5 所以需要把 2 4 6补上 { int t=*it; // cout<<t<<endl; Map[cnt]=t; // cout<<cnt<<endl; Map[cnt+1]=t; cnt+=2; } cnt-=2; Build(1,1,cnt); for(int i=1;i<=n;i++) { int L=l[i],R=r[i]; L=lower_bound(Map+1,Map+cnt+1,L)-Map; R=lower_bound(Map+1,Map+cnt+1,R)-Map; Update(1,L,R,h[i]); } ans=0; solve(1,1,cnt); printf("%I64d\n",ans); } return 0;}
- POJ 3277 离散化线段树
- POJ 3277(离散化,线段树)
- poj 3277(线段树+离散化)
- [离散化+线段树] POJ
- (离散化 + 线段树) poj 3277 City Horizon
- poj 3277 City Horizon(线段树#2----离散化)
- POJ 3277 City Horizon 扫描线+离散化+线段树
- poj-3277-City Horizon-离散化+线段树区域更新
- POJ 3277 City Horizon (离散化线段树)
- poj 3277 City Horizon 线段树加上离散化
- POJ 3277线段树 离线+离散
- POJ 1151 Atlantis 线段树+离散化
- poj 3368 离散化+线段树
- poj 2528 线段树+离散化
- POJ 2528 (线段树+离散化)
- poj 2528 (线段树+离散化)
- poj~2528~离散化线段树
- POJ 2528 线段树+离散化
- MyEclipse的第一个Web Project
- MongoDB Shell 命令--进阶篇
- SIFT算法详解
- 为何说张三丰是个优秀的程序员?
- 小萌谈 RobotArt(8)——从一只猴子看如何消除离线编程的误差
- poj 3277(线段树+离散化)
- Distributed Machine Learning —— Parameter Server
- 文档与笔记利器 reStructuredText 和 Sphinx
- 自学-ES6篇-Proxy和Reflect
- SQL语句优化技巧30个
- 7 Lucene笔记(七):第三方工具,将javabean、list、Map<String,object>转成json文本
- 点圈验证码
- 利用maven发布可调用的jar包
- C4.5算法