1706: City Horizon
Total Submit: 44 Accepted:11
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. Buildingi's silhouette has a base that spans locations Ai throughBi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has heightHi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by allN buildings.
Input
Line 1: A single integer:N
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers:Ai, Bi, and Hi
Output
Line 1: The total area, in square units, of the silhouettes formed by allN buildings
Sample Input
4
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
16
Hint
Source
分析:这道题用线段树做很简单,不过偶然看到自己以前写的代码,那时还不知道线段树,甚至不知道STL。。。于是排序算法和树操作都是自己写的(只是普通的二叉树,那时应该也不会写平衡树。。。)反正就这样把这题给写出来了。。。
思路很简单,把所有坐标排序,然后从左向右扫描,始终记录当前最高高度。遇到区间起点就向树中插入结点,同时更新高度,遇到区间终点就删除结点,同时找到现有的最大高度。因为要用DELETE和EXTRACT-MAX操作,就用的二叉树。
#include
using namespace std;
struct point {
int cor;
int num;
bool start;
};
struct tree {
int h;
tree *left;
tree *right;
};
const int MAX = 40010;
int height[MAX], heap[MAX], len = 0;;
point *pt[MAX * 2];
void change(point *&p1, point *&p2) {
point *tmp = p1;
p1 = p2;
p2 = tmp;
}
void pushdown(point *p[], int m, int n) {
while (m <= n / 2) {
if (m * 2 == n) {
if (p[m]->cor < p[n]->cor) {
change(p[m], p[n]);
}
break;
}
else if (p[m]->cor < p[m * 2]->cor && p[m * 2]->cor >= p[m * 2 + 1]->cor) {
change(p[m], p[m * 2]);
m *= 2;
}
else if (p[m]->cor < p[m * 2 + 1]->cor && p[m * 2 + 1]->cor > p[m * 2]->cor) {
change(p[m], p[m * 2 + 1]);
m = m * 2 + 1;
}
else {
break;
}
}
}
void heapsort(point *p[], int n) {
int i;
for (i = n / 2; i > 0; i--) {
pushdown(p, i, n);
}
for (i = n; i > 1; i--) {
change(p[i], p[1]);
pushdown(p, 1, i - 1);
}
}
void insert(tree *&head, int h) {
if (head == NULL) {
head = new tree;
head->h = h;
head->left = head->right = NULL;
}
else {
tree *p = head, *q = NULL;
while (p != NULL) {
q = p;
if (p->h >= h) {
p = p->left;
}
else {
p = p->right;
}
}
p = new tree;
p->h = h;
p->left = p->right = NULL;
if (p->h <= q->h) {
q->left = p;
}
else {
q->right = p;
}
}
}
int del(tree *&head, int h) { //tree not empty required
tree *p = head, *q = NULL, *r = NULL;
while (p != NULL && p->h != h) {
q = p;
if (p->h > h) {
p = p->left;
}
else {
p = p->right;
}
}
if (p->left == NULL || p->right == NULL) {
if (p->left != NULL) {
r = p->left;
}
else {
r = p->right;
}
if (q == NULL) {
head = r;
}
else if (p == q->left) {
q->left = r;
}
else {
q->right = r;
}
}
else {
r = p->right;
tree *s = NULL;
while (r->left != NULL) {
s = r;
r = r->left;
}
if (s != NULL) {
s->left = r->right;
r->right = p->right;
}
r->left = p->left;
if (q == NULL) {
head = r;
}
else if (p == q->left) {
q->left = r;
}
else {
q->right = r;
}
}
delete p;
if (head == NULL) {
return 0;
}
else {
p = head;
while (p->right != NULL) {
p = p->right;
}
return p->h;
}
}
int main(void) {
// freopen("D:\\in.txt", "r", stdin);
int n, i, x1, x2, tmp, curh = 0, tt;
__int64 out = 0, area;
tree *head = NULL;
cin >> n;
for (i = 1; i <= n; i++) {
scanf("%d %d %d", &x1, &x2, &height[i]);
tmp = 2 * i - 1;
pt[tmp] = new point;
pt[tmp]->cor = x1;
pt[tmp]->num = tmp;
pt[tmp]->start = true;
tmp = 2 * i;
pt[tmp] = new point;
pt[tmp]->cor = x2;
pt[tmp]->num = tmp;
pt[tmp]->start = false;
}
heapsort(pt, n * 2);
x1 = pt[1]->cor;
curh = height[(pt[1]->num + 1) / 2];
for (i = 1; i <= tmp; i++) { //tmp = n * 2
if (pt[i]->start) {
tt = height[(pt[i]->num + 1) / 2];
insert(head, tt);
if (curh < tt) {
area = pt[i]->cor - x1;
area *= curh;
out += area;
x1 = pt[i]->cor;
curh = tt;
}
}
else {
tt = del(head, height[(pt[i]->num + 1) / 2]);
if (curh > tt) {
area = pt[i]->cor - x1;
area *= curh;
out += area;
x1 = pt[i]->cor;
curh = tt;
}
}
}
printf("%I64d\n", out);
return 0;
}
本文介绍了一种解决计算城市天际线中多个矩形建筑物轮廓总面积问题的方法。通过坐标排序与二叉树数据结构的应用,实现了从左到右扫描并记录最高建筑高度的过程,最终得出所有建筑物轮廓形成的总面积。
869

被折叠的 条评论
为什么被折叠?



