poj 3277 City Horizon

求在同一水平线上的矩形面积并,按高度从小到大排序,然后变成普通线段覆盖即可。

需要 lazy 标记。所有下标貌似都得用long long。

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const long long MAX = 80100;
struct Tnode{ long long l,r,h;};
struct Sline{ long long x,y,h;};
Tnode node[MAX<<2];
Sline l[MAX<<1];
long long x[MAX<<2];

void init()
{
	memset(node,0,sizeof(node));
}
void Build(long long t,long long l,long long r)
{
	node[t].l = l; node[t].r = r;
	node[t].h = 0;
	if( l == r - 1 ) return ;
	long long mid = MID(l,r);
	Build(L(t),l,mid);
	Build(R(t),mid,r);
}

void Updata(long long t,long long l,long long r,long long h)
{
	if( h < node[t].h ) return ;
	if( node[t].l >= l && node[t].r <= r )
	{
		node[t].h = max(node[t].h,h);
		return ;
	}
	if( node[t].l == node[t].r - 1 ) return ;
	if( node[t].h > 0 )
	{
		node[R(t)].h = node[L(t)].h = node[t].h;
		node[t].h = 0;
	}	
	long long mid = MID(node[t].l,node[t].r);
	if( l <= mid )
		Updata(L(t),l,r,h);
	if( r > mid )
		Updata(R(t),l,r,h);
}

long long Query(long long t,long long l,long long r)
{
	if( node[t].h > 0 )
		return ( x[node[t].r] - x[node[t].l] )*1ll*node[t].h;
	if( node[t].l == node[t].r - 1 ) return 0;
	int mid = MID(node[t].l,node[t].r);
	long long ans = 0ll;
    if( l <= mid )
        ans += Query(L(t),l,r);
    if( r > mid )
        ans += Query(R(t),l,r);
	return ans;
}
long long solve(int n,int cnt)
{
	init();
	Build(1,0,cnt-1);
	for(int i=0; i<n; i++)
	{
		long long xx = lower_bound(x,x+cnt,l[i].x) - x;
		long long yy = lower_bound(x,x+cnt,l[i].y) - x;
		Updata(1,xx,yy,l[i].h);
	}
	long long ans = Query(1,0,cnt-1);
	return ans;
}
bool cmp(Sline a,Sline b)
{
	return a.h < b.h;
}
int main()
{
	int n;
	while( ~scanf("%d",&n) )
	{
		int cnt = 0;
		for(int i=0; i<n; i++)
		{
			scanf("%lld%lld%lld",&l[i].x,&l[i].y,&l[i].h);
			x[cnt++] = l[i].x; x[cnt++] = l[i].y;
		}
		sort(x,x+cnt);
		sort(l,l+n,cmp);
		cnt = unique(x,x+cnt) - x;
		long long ans = solve(n,cnt);
		printf("%lld\n",ans);
	}
return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值