【POJ 3277】 City Horizon(离散化+树状数组+二分)

【POJ 3277】 City Horizon(离散化+树状数组+二分)

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17846   Accepted: 4894

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

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 all N buildings

Sample Input

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

Sample Output

16

Hint

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.


题目大意:n个影子,分布在二维坐标中。
每个影子的表示方式为x1,x2,y,覆盖了矩形横坐标x1->x2 纵坐标为y的区间
这些影子会有重叠

问重叠后所有影子的面积。覆盖部分只统计一次


y离散化,这个没的说
影子拆成左右两边,左进右出,这个也没啥说的

按x排序,从左到右遍历。
每当遇到入边,在离散化后的数组中找到当前y的下标(二分) 然后在树状数组对应位置累加1
每当遇到出边,~,减1

当出现横向移动,也就是到了一个新x的时候,就要统计出这段影子的面积
也就是找到当前的最高的y(因为影子默认从y=0向上伸展到y 所以只统计当前最高的y即可 较低的都被覆盖掉了
这个y在树状数组内二分即可,先找到树状数组末尾的值,也就是整个区间的加和
然后找到最右端的非0位置(通过二分位置,然后求加和,跟刚才求的加和比较来得到

代码如下:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
#define Pr pair
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Node
{
	int x,y;
	bool ad;
	bool operator < (const struct Node a)const
	{
		return x < a.x;
	}
};

Node nd[88888];
int ty[44444];
int bit[44444];
int tp;

int Lowbit(int x)
{
	return x&(-x);
}

void add(int x,int d)
{
	while(x < tp)
	{
		bit[x] += d;
		x += Lowbit(x);
	}
}

int sum(int x)
{
	int ans = 0;
	while(x)
	{
		ans += bit[x];
		x -= Lowbit(x);
	}
	return ans;
}

int get()
{
	int ans = sum(tp-1);
	if(ans == 0)
		return 0;
	int c;

	int l = 1, r = tp-1;
	while(l <= r)
	{
		int mid = (l+r)>>1;
		int k = sum(mid);
		if(k >= ans)
		{
			r = mid-1;
			if(k == ans) c = mid;
		}else l = mid+1;
	}

	return ty[c];
}

int main()
{
	//fread();
	//fwrite();

	int n,x1,x2,y;
	LL ans;
	while(~scanf("%d",&n))
	{
		int tmp = 0;
		for(int i = 0; i < n; ++i)
		{
			scanf("%d%d%d",&x1,&x2,&y);
			nd[tmp].x = x1;
			nd[tmp].y = y;
			nd[tmp].ad = 1;
			ty[i+1] = y;

			tmp++;
			nd[tmp].x = x2;
			nd[tmp].y = y;
			nd[tmp].ad = 0;
			tmp++;
		}

		memset(bit,0,sizeof(bit));
		sort(ty+1,ty+n+1);
		sort(nd,nd+tmp);
		tp = unique(ty+1,ty+n+1)-ty;
		ans = 0;

		set  s;
		int x;
		for(int i = 0; i < tmp; ++i)
		{
			//printf("x:%d y:%d ad:%s\n",nd[i].x,nd[i].y,nd[i].ad? "y": "n");
			if(i && nd[i-1].x != nd[i].x)
				ans += 1LL*(nd[i].x-nd[i-1].x)*get();

			int l = 1, r = tp-1;
			while(l <= r)
			{
				int mid = (l+r)>>1;
				if(ty[mid] <= nd[i].y)
				{
					l = mid+1;
					if(ty[mid] == nd[i].y)
					{
						x = mid;
						break;
					}
				}else r = mid-1;
			}

			if(nd[i].ad)
			{
				add(x,1);
			}else add(x,-1);
		}

		printf("%lld\n",ans);
	}

	return 0;
}





你可能感兴趣的:(POJ,线段树,ACM道路之数据结构)