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

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;
}



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

做这题才去仔细了解线段树本身的建树特征。上面的那句话虽然还不是很懂,但是慢慢来吧,有朝一日会明白的吧!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值