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

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

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

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

时间: 2024-10-09 01:28:51

poj 1436 Horizontally Visible Segments(线段树、区间覆盖)的相关文章

(中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。

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.

POJ 1436 Horizontally Visible Segments (线段树&amp;#183;区间染色)

题意   在坐标系中有n条平行于y轴的线段  当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交  就视为它们是可见的  问有多少组三条线段两两相互可见 先把全部线段存下来  并按x坐标排序  线段树记录相应区间从右往左当前可见的线段编号(1...n)  超过一条就为0  然后从左往右对每条线段  先查询左边哪些线段和它是可见的  把可见关系存到数组中  然后把这条线段相应区间的最右端可见编号更新为这条线段的编号  最后暴力统计有多少组即可了 #include <cstdio>

POJ 1436 Horizontally Visible Segments(线段树建图+枚举)

题目连接:http://poj.org/problem?id=1436 题意:给一些线段,每个线段有三个值y1, y2, x代表起点为(x, y1),终点为(x, y2)的线段.当从一个线段可以作水平线到另一个线段并且不穿过其他线段时,就称这两个线段时水平可见的.当三个线段可以两两水平可见,就称为形成一个线段三角.问:在这些线段中有多少个这样的线段三角? 分析:可以把每条线段看做是一个点,如果它和其他线段是水平可见的,就将这两个点相连,由于是无向图,就是你能看到我,我也能看到你,所以需要连接两次

POJ 1436 Horizontally Visible Segments(线段树)

POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序,从左往右扫描处理出每个线段能看到的右边的线段,然后利用bitset维护枚举两个线段,找出另一个两个都有的线段 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> #include <vector> using namesp

POJ - 1436 Horizontally Visible Segments

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.

POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力)

Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4130   Accepted: 1511 Description There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they

POJ 1436 Horizontally Visible Segments(线段树区间修改)

题目链接:点击打开链接 题意:n条竖直线段,如果两条线段之间可见(即可以用一条水平线段连接而不触碰其他线段),则称它们可见.   如果三条线段任意两条都可见, 则称它们为a triangle of segments, 求a triangle of segments的个数 思路: 一开始真没想到n^3的复杂度可以过...  如果这样的话, 问题的关键就是怎样判断任意两个线段是否可见. 那么如果对y坐标建立区间, 这就成了线段树的区间覆盖问题. 另外, 由于是用点代替线段, 所以对于"可见"

poj 2528 Mayor&#39;s posters 线段树区间更新

Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at al

poj 2528 Mayor&#39;s posters(线段树区间覆盖、离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49385   Accepted: 14304 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post