DOJ - 1005 City Horizon (线段树扫面线)

18 篇文章 0 订阅
2 篇文章 0 订阅
问题描述

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(1N40,000) N(1≤N≤40,000) buildings. Building i's silhouette has a base that spans locations Ai Ai through Bi Bi along the horizon (1Ai<Bi1,000,000,000) (1≤Ai<Bi≤1,000,000,000) and has height Hi(1Hi1,000,000,000) Hi(1≤Hi≤1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N N buildings.

输入格式

Line 1: A single integer: N N

Lines 2N+1 2→N+1: Input line i+1 i+1 describes building i i with three space-separated integers: Ai Ai, Bi Bi, and Hi Hi

输出格式

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

样例输入
4
2 5 1
9 10 4
6 8 2
4 6 3
样例输出
16
数据范围和提示

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 31 + 14 + 22 + 23 - 1 = 16.



#include <cstdio>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define MAXN 2147483647
using namespace std;
struct line
{
	long long f,l,r;
	long long h;
	friend bool operator < (line a,line b)
	{
		return a.h < b.h;
	}
} yline[80008];
struct Segtree
{
	long long l,r,cnt;
	long long cover;
} tree[320006];
long long num,tot,n,a,b,h,xdis[80008],width[80008],ans;
unordered_map <long long,long long> f;
void build(long long i,long long l,long long r)
{
	tree[i].l = l;
	tree[i].r = r;
	tree[i].cnt = 0;
	if(l == r) return;
	long long mid = (l+r)/2;
	build(2*i,l,mid);
	build(2*i+1,mid+1,r);
}
void deal(long long i,long long l,long long r,long long f)
{
	if(l == tree[i].l && r == tree[i].r)
	{
		tree[i].cnt += f;
		if(tree[i].cnt == 0)
		{
			if(l != r) tree[i].cover = tree[i*2].cover + tree[i*2+1].cover;
			else tree[i].cover = 0;
		}
		else tree[i].cover = width[tree[i].r+1] - width[tree[i].l];
		return; 
	}
	long long mid = (tree[i].l + tree[i].r) >> 1;
	if(r <= mid) deal(2*i,l,r,f);
	else 
	 if(l <= mid) deal(2*i,l,mid,f),deal(2*i+1,mid+1,r,f);
	 else deal(2*i+1,l,r,f);
	if(tree[i].cnt == 0) tree[i].cover = tree[i*2].cover + tree[i*2+1].cover;
	else tree[i].cover = width[tree[i].r+1] - width[tree[i].l]; 
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n;
	for(int i = 1;i <= n;i++)
	{
		cin>>a>>b>>h;
		xdis[++tot] = a,yline[tot].f = 1,yline[tot].h = 0,yline[tot].l = a,yline[tot].r =b;
		xdis[++tot] = b,yline[tot].f = -1,yline[tot].h = h,yline[tot].l = a,yline[tot].r =b;
	}
	sort(xdis+1,xdis+1+2*n);
	sort(yline+1,yline+1+2*n);
	xdis[0] = -MAXN;
	for(int i = 1;i <= 2*n;i++)
	 if(xdis[i] != xdis[i-1])
	 {
	 	f[xdis[i]] = ++num;
		width[num] = xdis[i];	
	 } 
	 build(1,1,num-1);
	 for(int i = 1;i < 2*n;i++)
	 {
	 	int l = f[yline[i].l],r = f[yline[i].r] - 1;
	 	deal(1,l,r,yline[i].f);
	 	ans += 1ll*tree[1].cover*(yline[i+1].h - yline[i].h);
	 }
	 cout<<ans<<endl;
 } 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值