WOJ1224-City Horizon

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.

 

输入格式

  • 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

输出格式

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

样例输入

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

样例输出

16


//C++98
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#define maxn 100010
using namespace std;
int t;
long long  sum;
struct node {
	int left,right,mid;
	int h;
} tree[3*maxn];
struct no {
	int a,b;
	int hi;
} s[maxn];
long long  hash[maxn];//存离散化的
struct point {
	int li,num;
	bool operator < (const struct point & other)const {
		return li < other.li;
	}
} line[maxn];

void make(int l, int r, int num) { //建树
	tree[num].left = l;
	tree[num].right = r;
	tree[num].mid = (l+r)/2;
	tree[num].h = 0;
//if(l < r)
	if(l + 1 != r) {
		make(l,tree[num].mid, 2*num);
		make(tree[num].mid, r, 2*num +1);
	}
	return ;
}

void insert(int l, int r, int h, int num) {
	if(tree[num].left == l && tree[num].right == r ) {
		if(tree[num].h < h  || tree[num].h == 0)
			tree[num].h = h;
		return ;
	} else if(tree[num].left < tree[num].right) {
		if( r <= tree[num].mid)
			insert(l,r,h,2*num);
		else if(l >= tree[num].mid)
			insert(l,r,h,2*num+1);
		else {
			insert(l,tree[num].mid,h,2*num);
			insert(tree[num].mid,r,h,2*num+1);
		}
	}
	return ;
}

long long cal(int h, int num) {
	if(h > tree[num].h)
		tree[num].h = h;
	if(tree[num].left +1 == tree[num].right) {
		return (long long)(tree[num].h *(hash[tree[num].right] - hash[tree[num].left]));
	}
	return (cal(tree[num].h , 2*num) + cal(tree[num].h, 2*num+1));
}

int main() {
	int i;
	while(scanf("%d",&t) != EOF) {
		for(i=0; i<t; i++) {
			int sa,sb,sh;
			scanf("%d%d%d",&sa, &sb,&sh);
			if(sa > sb) {
				int tem = sa;
				sa = sb;
				sb = tem;
			}
			s[i].a = sa;
			s[i].b = sb;
			s[i].hi = sh;
			line[i*2].li = s[i].a;
			line[i*2].num = -(i+1);
			line[i*2+1].li = s[i].b;
			line[i*2+1].num = i+1;
		}
		sort(line, line+t*2);
		int tp=1,temp=line[0].li;//离散化
		for(i=0; i<2*t; i++) {
			if(line[i].li != temp) {
				tp++;
				temp = line[i].li;
			}
			if(line[i].num < 0) {
				hash[tp] = s[-line[i].num -1].a;
				s[-line[i].num -1].a = tp;
			} else {
				hash[tp] = s[line[i].num -1].b;
				s[line[i].num -1].b = tp;
			}
		}
		make(1,tp,1);
		for(i=0; i<t; i++) {
			insert(s[i].a, s[i].b, s[i].hi, 1);
		}
		sum = cal(0,1);
		printf("%lld\n",sum);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值