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

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

——————————————————分割线————————————————


题目大意:

如果两条线段相交,则叫做水平可见,给出n条这样的线段,求有多少组的3条线段两两可见


思路:

对每条线段的横坐标从小到大排序,然后一边插入,一边统计跟当前要插入的线段是否相交,存放到vector中,每当插入新的线段,就会覆盖某些旧的线段,抽象地表示了水平可见,相当于每条线段都往左边看,可见是相互的,才能保证不重复不遗漏。插入的时候给每条线段一个颜色,来区分不同的线段,相当于区间染色


例如题目给的数据排序后是

y1 y2 x

0 3 1

3 4 2

0 2 2

0 2 3

0 4 4

(0,2,2)这条线段往左看能看到(0,3,1),而(0,2,3)这条线段看不到(0,3,1),只能看到(0,2,2),因为(0,2,2)这条线段覆盖了(0,3,1)这条线段0-2的部分


另外由于这题点表示线段,因此要把端点乘2


求出哪些线段可以两两相交之后,暴力枚举3条线段两两可见

如果线段x能见到y,z,再判断线段y能不能见到z,如果能,则符合3条线段两两可见


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define vi vector<int>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=16001;
using namespace std;
vi v[maxn+5];
int cover[maxn<<2];
int hash[maxn+5];
struct v_seg
{
    int s,t,x;
    bool operator<(const v_seg&A) const
    {
        return x<A.x;
    }
}ss[maxn+5];
void push_down(int rt)
{
    if(cover[rt]!=-1){
        cover[rt<<1]=cover[rt<<1|1]=cover[rt];
        cover[rt]=-1;
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        cover[rt]=c;
        return;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(m<R) update(L,R,c,rson);
}
void query(int L,int R,int c,int l,int r,int rt)
{
    if(cover[rt]!=-1){
        if(hash[cover[rt]]!=c){
            v[cover[rt]].push_back(c);
            hash[cover[rt]]=c;
        }
        return ;
    }
    if(l==r) return ;
    int m=(l+r)>>1;
    if(L<=m) query(L,R,c,lson);
    if(m<R) query(L,R,c,rson);
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            scanf("%d %d %d",&ss[i].s,&ss[i].t,&ss[i].x);
            ss[i].s<<=1,ss[i].t<<=1;
            v[i].clear();
        }
        sort(ss,ss+n);
        memset(hash,-1,sizeof(hash));
        memset(cover,-1,sizeof(cover));
        for(int i=0;i<n;++i){
            query(ss[i].s,ss[i].t,i,0,maxn,1);
            update(ss[i].s,ss[i].t,i,0,maxn,1);
        }
        int ans=0;
        for(int i=0;i<n;++i){
            for(int j=0;j<v[i].size();++j){
                int x=v[i][j];
                for(int k=0;k<v[i].size();++k){
                    for(int t=0;t<v[x].size();++t){
                        if(v[i][k]==v[x][t]){
                            ans++;
                            break;
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值