码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1436 Horizontally Visible Segments(线段树、区间覆盖)

时间:2015-07-30 13:37:43      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:poj

Horizontally Visible Segments
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4645   Accepted: 1706

Description

There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments. Three different vertical segments are said to form a triangle of segments if each two of them are horizontally visible. How many triangles can be found in a given set of vertical segments?


Task

Write a program which for each data set:

reads the description of a set of vertical segments,

computes the number of triangles in this set,

writes the result.

Input

The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 20. The data sets follow.

The first line of each data set contains exactly one integer n, 1 <= n <= 8 000, equal to the number of vertical line segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

yi‘, yi‘‘, xi - y-coordinate of the beginning of a segment, y-coordinate of its end and its x-coordinate, respectively. The coordinates satisfy 0 <= yi‘ < yi‘‘ <= 8 000, 0 <= xi <= 8 000. The segments are disjoint.

Output

The output should consist of exactly d lines, one line for each data set. Line i should contain exactly one integer equal to the number of triangles in the i-th data set.

Sample Input

1
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3

Sample Output

1

Source

Central Europe 2001




题目大意:在水平坐标系内,有n条垂直线段,问任意三条线段组成一组,问有多少组线段满足,其中任意俩条线段水平可见。
水平可见满足:存在一条水平线段将俩条线段连接起来,并且中间不与其他任意线段有交点。

分析:线段树区间覆盖问题。可以将线段按x轴大小排序,然后进行纵坐标的染色。可以将线段编号理解为颜色,那么判断该线段是否与前面的线段可见,只需查询判断该区间与原来的区间重复的地方有没有被染色,如果有就表明可见。注意要先查询后更新。最后将可见的线段都用数组存下,暴力查询有几组。
还有一个就是,要注意线段树的建立本身。线段树是以一个点当做区间建树的,即[1,1]是一个区间,所以会存在这样一种情况:从左往右的线段是[1,2],[3,4],[2,3],进行第三条线段查询的时候会发现[2,3]这个区间已经被覆盖了,其实本身是还没有被覆盖的。
解决办法则是:对于纵坐标都乘以2,这样的话刚才的情况就变成了[2,4],[6,8],[4,6],此时中间就有空隙[5,5]了,就不会发生刚才的错误了。


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 16005
using namespace std;
struct tree{
	int l,r,color;
}tree[M<<2];
struct node{
	int x,y1,y2;
}segment[8005];
bool a[8005][8005];     //bool型省下了大量空间,用int会MLE.
bool cmp(node a,node b){
	return a.x<b.x;
}
void build(int l,int r,int root)
{
	tree[root].l=l;
	tree[root].r=r;
	tree[root].color=-1;
	if(l==r)return;
	int mid=l+r>>1;
	build(l,mid,root<<1);
	build(mid+1,r,root<<1|1);
}
void pushdown(int root)
{
	if(tree[root].l==tree[root].r)return;
	if(tree[root].color!=-1)
	{
	tree[root<<1].color=tree[root<<1|1].color=tree[root].color;
	 tree[root].color=-1;
	}
	return;
}
void update(int l,int r,int z,int root)
{
	if(tree[root].l==l&&tree[root].r==r){
		 tree[root].color=z;
		return ;
	}
	pushdown(root);
	int mid=tree[root].l+tree[root].r>>1;
	if(r<=mid)update(l,r,z,root<<1);
	else if(l>mid)update(l,r,z,root<<1|1);
	else {
		update(l,mid,z,root<<1);
		update(mid+1,r,z,root<<1|1);
	}
}
void query(int l,int r,int c,int root)
{
	
		if(tree[root].color!=-1)
		{
		a[tree[root].color][c]=true;
		return;
		}
		if(tree[root].l==tree[root].r)return;
	int mid=tree[root].l+tree[root].r>>1;
	if(r<=mid) query(l,r,c,root<<1);
	else if(l>mid) query(l,r,c,root<<1|1);
	else {
	query(l,mid,c,root<<1);
	query(mid+1,r,c,root<<1|1);
	}
}
int main()
{
	int d,i,j,k,n;
	scanf("%d",&d);
	while(d--)
	{
		scanf("%d",&n);
		memset(a,false,sizeof(a));
		build(0,16000,1);
		for(i=0;i<n;i++)
		{
			scanf("%d%d%d",&segment[i].y1,&segment[i].y2,&segment[i].x);
			
		}
		sort(segment,segment+n,cmp);
	
		for(i=0;i<n;i++)
		{
			query(2*segment[i].y1,2*segment[i].y2,i,1);
			update(2*segment[i].y1,2*segment[i].y2,i,1);
			
		}
		int ans=0;
		for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
		{
			if(a[i][j]){
				for(k=j+1;k<n;k++)
				if(a[j][k]&&a[i][k])ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}



就浏览别人博客的时候发现这样一句话://又是一个偶数代表点,奇数代表区间的线段树、或许这就是线段树处理区间和点问题的方法吧

做这题才去仔细了解线段树本身的建树特征。上面的那句话虽然还不是很懂,但是慢慢来吧,有朝一日会明白的吧!

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 1436 Horizontally Visible Segments(线段树、区间覆盖)

标签:poj

原文地址:http://blog.csdn.net/aaaaacmer/article/details/47147087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!