您的位置:首页 > 其它

POJ 1436 Horizontally Visible Segments (线段树)

2013-04-19 10:09 447 查看
Horizontally Visible Segments

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 2829Accepted: 1067
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

 

线段树成段更新问题。此题跟poj2528是类似的,把坐标平面逆时针转九十度就跟那题贴海报差不多了。只不过这题问的是每三条线段是否能彼此直接用水平线相连,所以要枚举一下。做法是先按x坐标从小到大排序,每列举一条线段就判断该区间是否已经被覆盖了(既前面是否有线段和这个区间有交集,有交集既说明可以相连,这也是为什么x坐标要升序排序的原因)。然而像样例一样,0 2 2和3 4 2这两条线段中,中间的缝隙是区间(2,3),这个区间不能用整数表示了,处理方法是将每一条的纵坐标都乘以2,这样(2,3)就变成(4,6),中间多了个整数5,这就能用线段树操作了。

邻接矩阵:60000k+ 2000ms+

邻接表:720k 125ms

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define SIZE 8008
#define ls  l,mid,rt<<1
#define rs  mid+1,r,rt<<1|1

using namespace std;

struct node
{
int l,r,x;
}line[SIZE];

struct Node
{
int to,next;
}edge[SIZE<<3];

int head[SIZE],idx;

int T,N,maxi;
int cv[SIZE<<3];
int vis[SIZE];

bool cmp(node a,node b)
{
return a.x < b.x;
}

void addNode(int from,int to)
{
edge[idx].to = to; edge[idx].next = head[from]; head[from] = idx++;
}

void pushDown(int rt)
{
if(cv[rt] != -1)
{
cv[rt<<1] = cv[rt<<1|1] = cv[rt];
cv[rt] = -1;
}
}

void update(int l,int r,int rt,int L,int R,int w)
{
if(L <= l && r <= R)
{
cv[rt] = w;
return;
}
pushDown(rt);
int mid = (l + r) >> 1;
if(L <= mid) update(ls,L,R,w);
if(R > mid) update(rs,L,R,w);
}

void query(int l,int r,int rt,int L,int R,int w)
{
if(cv[rt] != -1)
{
if(vis[cv[rt]] != w)
{
addNode(cv[rt],w);
vis[cv[rt]] = w;
}
return;
}
if(l == r) return;
int mid = (l + r) >> 1;
if(L <= mid) query(ls,L,R,w);
if(R > mid) query(rs,L,R,w);
}

int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
maxi = 0;
for(int i=1; i<=N; i++)
{
scanf("%d%d%d",&line[i].l,&line[i].r,&line[i].x);
line[i].l *= 2;
line[i].r *= 2;
maxi = max(maxi,line[i].r);
}
sort(line+1,line+1+N,cmp);
memset(cv,-1,sizeof(cv));
memset(vis,-1,sizeof(vis));
memset(head,-1,sizeof(head));
idx = 0;
for(int i=1; i<=N; i++)
{
query(0,maxi,1,line[i].l,line[i].r,i);
update(0,maxi,1,line[i].l,line[i].r,i);
}
int ans = 0;
int to;
for(int i=1; i<=N; i++)
{
for(int j=head[i]; j!=-1; j=edge[j].next)
{
to = edge[j].to;
for(int k=head[i]; k!=-1; k=edge[k].next)
{
for(int g=head[to]; g!=-1; g=edge[g].next)
{
if(edge[k].to == edge[g].to)
ans ++;
}
}
}
}
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: