poj 1436 Horizontally Visible Segments(线段树 区间的覆盖关系)

Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1942 Accepted: 736

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

题目:http://poj.org/problem?id=1436

题意:给你n条垂直的线段,这些线段不会相交,如果两条线段直接存在一条水平线可以连接到这两条线段,并且不与其他线段相交,我们说着两条线段可见,求一共有多少对三条线段两两可见。。。

分析:这题当然可以用从上往下的扫描线,加上个排序什么的,来解决,而这题的线段树写法,依旧是模拟区间的覆盖,而这题要求覆盖的关系,一开始我以为在更新线段树的时候就可以判断覆盖关系,然后直接覆盖,最后在写一个遍历整棵树,找出所有覆盖关系,然而这样是错的,因为可能之前那个区间还没找到它盖上的区间就被替换掉了。。。

而正确的做法就是每次先寻找这个区间覆盖了那些线段,然后把它加到线段树里面,这样就避免了之前的错误。。。现在就得到了线段是否可见的关系图,然后暴力枚举三条线段。。。。貌似深搜没法做的样子

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int mm=16666;
struct seg
{
    int l,r,x;
}g[mm];
int head[mm],flag[mm],ver[mm<<8],next[mm<<8];
int dly[mm<<2];
int edge,ans;
bool cmp(seg a,seg b)
{
    return a.x<b.x;
}
void prepare()
{
    edge=0;
    memset(head,-1,sizeof(head));
    memset(dly,0,sizeof(dly));
    memset(flag,0,sizeof(flag));
}
void addedge(int u,int v)
{
    ver[edge]=v,next[edge]=head[u],head[u]=edge++;
}
void pushdown(int rt)
{
    dly[rt<<1]=dly[rt<<1|1]=dly[rt];
    dly[rt]=0;
}
void updata(int L,int R,int id,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        dly[rt]=id;
        return;
    }
    if(dly[rt])pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m)updata(L,R,id,lson);
    if(R>m)updata(L,R,id,rson);
}
void query(int L,int R,int id,int l,int r,int rt)
{
    if(dly[rt])
    {
        if(flag[dly[rt]]!=id)
            addedge(dly[rt],flag[dly[rt]]=id);
        return;
    }
    if(l==r)return;
    int m=(l+r)>>1;
    if(L<=m)query(L,R,id,lson);
    if(R>m)query(L,R,id,rson);
}
int main()
{
    int i,j,k,v,n,cs;
    scanf("%d",&cs);
    while(cs--)
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
        {
            scanf("%d%d%d",&g[i].l,&g[i].r,&g[i].x);
            g[i].l<<=1,g[i].r<<=1;
        }
        sort(g,g+n,cmp);
        prepare();
        for(i=0;i<n;++i)
            query(g[i].l,g[i].r,i+1,0,mm,1),
            updata(g[i].l,g[i].r,i+1,0,mm,1);
        ans=0;
        for(i=1;i<=n;++i)
            for(j=head[i];j>=0;j=next[j])
                for(k=next[j];k>=0;k=next[k])
                {
                    for(v=head[ver[j]];v>=0;v=next[v])
                        if(ver[v]==ver[k])break;
                    if(v<0)
                    for(v=head[ver[k]];v>=0;v=next[v])
                        if(ver[v]==ver[j])break;
                    if(v>=0)++ans;
                }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值