概述
【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
Lines 2.. N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
Output
Sample Input
4
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
16
Hint
Source
解题思路
线段树+离散化 因为它们的宽度太大,无法存储所以先离散,然后再根据按从低到高的顺序更新线段树。
AC代码1:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
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|1
typedef long long ll;
const int MAXN = 4e4+5;
ll sum[MAXN<<3];
int add[MAXN<<3],sl[MAXN<<3],sr[MAXN<<3];
vector<int> v,vec;
struct Node
{
int a,b,h;
};
Node node[MAXN];
bool compare(Node a,Node b)
{
return a.h<b.h;
}
int getId(int x) {return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
void push_up(int root)
{
sum[root]=sum[lson]+sum[rson];
}
void push_down(int root)
{
if(add[root])
{
add[lson]=add[rson]=add[root];
sum[lson]=(ll)add[root]*(sr[lson]-sl[lson]);
sum[rson]=(ll)add[root]*(sr[rson]-sl[rson]);
add[root]=0;
}
}
void build(int root,int l,int r)
{
sum[root]=0;
sl[root]=v[l-1];
sr[root]=v[r-1];
if(l+1==r) return;
int mid=(l+r)>>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<n;i++)
{
cin >> 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<n;i++)
{
int x=getId(node[i].a);
int y=getId(node[i].b);
update(1,1,len,x,y,node[i].h);
}
cout << sum[1] << endl;
return 0;
}
AC代码2:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
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|1
typedef long long ll;
const int MAXN = 4e4+5;
ll sum[MAXN<<3];
int add[MAXN<<3],sl[MAXN<<3],sr[MAXN<<3];
vector<int> v,vec;
struct Node
{
int a,b,h;
};
Node node[MAXN];
bool compare(Node a,Node b)
{
return a.h<b.h;
}
int getId(int x) {return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
void push_up(int root)
{
sum[root]=sum[lson]+sum[rson];
}
void push_down(int root)
{
if(add[root])
{
add[lson]=add[rson]=add[root];
sum[lson]=(ll)add[root]*(sr[lson]-sl[lson]);
sum[rson]=(ll)add[root]*(sr[rson]-sl[rson]);
add[root]=0;
}
}
void build(int root,int l,int r)
{
sum[root]=0;
sl[root]=v[l-1];
sr[root]=v[r-1];
if(l+1==r) return;
int mid=(l+r)>>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<n;i++)
{
cin >> 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<n;i++)
update(1,1,len,node[i].a,node[i].b,node[i].h);
cout << sum[1] << endl;
return 0;
}
最后
以上就是复杂荷花为你收集整理的【POJ 3277 --- City Horizon】离散化+线段树【POJ 3277 --- City Horizon】离散化+线段树的全部内容,希望文章能够帮你解决【POJ 3277 --- City Horizon】离散化+线段树【POJ 3277 --- City Horizon】离散化+线段树所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复