poj1436(线段树区间染色)

Horizontally Visible Segments

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条垂直于x轴的线段,给出他们y范围。当两条线段之间有一条垂直于他们并且可以不经过其他线段,则可定义两条线段互相可见,求出有多少三条线段两两互相可见==

==思路:可以把每个线段看成区间,按y轴建树,然后根据线段的x位置从小到大对区间进行染色,在染色之前先对其区间内的颜色进行标记,这些颜色与当前要染的颜色一定互相可见。最后三重for循环求出两两互相可见的三条线段。但要注意有些区间如1-2,3-4,其染色之后2-3这个会被忽略,因为2-3区间没有点,而线段树储存的颜色是在点上。所以要对y扩大两倍,相当于在原先的两点间在插入一点。==

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0x7fffffff
using namespace std;
typedef long long ll;
const int MAXN=1e9+10;
const int MAX=16100+10;
int n;
bool vis[MAX>>1][MAX>>1];
struct Line{
    int y1,y2,x;
}line[MAX>>1];
struct NODE{
    int l,r,color;
}tree[MAX*4];

int cmp(Line a,Line b){return a.x<b.x;}

void init(int node,int l,int r){
    tree[node].l=l;
    tree[node].r=r;
    tree[node].color=0;
    if(l==r)    return ;
    int mid=(l+r)>>1;
    init(node<<1,l,mid);
    init(node<<1|1,mid+1,r);    
}

void push_down(int node){
    if(tree[node].color!=-1){
        tree[node<<1].color=tree[node<<1|1].color=tree[node].color;
        tree[node].color=-1;        
    }
}

void updata_tree(int node,int x,int y,int num){
    int l=tree[node].l;
    int r=tree[node].r;
    if(x<=l&&r<=y){
        tree[node].color=num;
        return ;
    }
    push_down(node);
    int mid=(l+r)>>1;
    if(x<=mid)  updata_tree(node<<1,x,y,num);
    if(y>mid)   updata_tree(node<<1|1,x,y,num);
}

void query(int node,int x,int y,int flag){
    if(tree[node].color!=-1){
        vis[tree[node].color][flag]=1;
        return ;
    }
    int l=tree[node].l;
    int r=tree[node].r;
    int mid=(l+r)>>1;
    if(l==r)
        return;
    push_down(node);
    if(x<=mid)  query(node<<1,x,y,flag);
    if(y>mid)   query(node<<1|1,x,y,flag);
}

int main(){
    //ios::sync_with_stdio(false);
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    #endif
    int T;
    cin>>T;
    while(T--){
        cin>>n;
        for(int i=1;i<=n;i++){  
            scanf("%d%d%d",&line[i].y1,&line[i].y2,&line[i].x);
            line[i].y1<<=1;
            line[i].y2<<=1;         
        }   
        sort(line+1,line+n+1,cmp);
        memset(vis,0,sizeof(vis));
        init(1,0,MAX);
        for(int i=1;i<=n;i++){
            query(1,line[i].y1,line[i].y2,i);
            updata_tree(1,line[i].y1,line[i].y2,i);
        }       
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(vis[i][j]){
                    for(int k=1;k<=n;k++){
                        if(vis[i][k]&&vis[j][k])
                            ans++;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值