poj 1436 成段更新(区间覆盖)

Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2578 Accepted: 965

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
传送门: http://blog.csdn.net/zxy_snow/article/details/6829766

http://www.cnblogs.com/wuyiqi/archive/2012/02/02/2336350.html

http://blog.csdn.net/qingniaofy/article/details/7772293

注意:如 样例 中 ,0 2 2,3 4 2这两条线段,可以看到 2 3之间是没有被覆盖的,但是在线段树中我们看不到这条线段,因为 变成 浮点数了,不能处理,那么我们可以将 坐标 x2,这样就变成 4 6,中间就多出一个点 5 了,就可以判断了。。

下面这个图是我徒手画的,很丑吧,嘻嘻。。表示了偶数点代表点,奇数代表线段,遇到有线段类的题目(用线段树做)经常要考虑乘以2,表示浮点的线段。。。poj  3225这题类似


Accepted1420K141MSC++1991B
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
using namespace std;
const int maxn = 20000;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
struct v_seg{
    int s,t;
    int x;
}ss[maxn];
int cover[maxn<<2];
int Hash[maxn];
vector<int> V[maxn];
void pushdown(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 id,int l,int r,int rt){
    if(L<=l&&r<=R){
        cover[rt]=id;
        return ;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,id,lson);
    if(R>m) update(L,R,id,rson);
}
void query(int L,int R,int id,int l,int r,int rt){
    if(cover[rt]!=-1){
        if(Hash[cover[rt]]!=id){
            V[cover[rt]].push_back(id);
            Hash[cover[rt]]=id;
        }
        return ;
    }
    if(l==r) return ;
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) query(L,R,id,lson);
    if(R>m)  query(L,R,id,rson);
}
int cmp(v_seg a,v_seg b){
    return a.x<b.x;
}
int main(){
    int t,i,j,k,n,T,h;
    scanf("%d",&T);
    while(T--){
        memset(cover,-1,sizeof(cover));
        memset(Hash,-1,sizeof(Hash));
        scanf("%d",&n);
        for(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,cmp);
        for(i=0;i<n;i++){
            query(ss[i].s,ss[i].t,i,0,16000,1);
            update(ss[i].s,ss[i].t,i,0,16000,1);
        }
        FOR(i, 0, n)
{
sort(V[i].begin(), V[i].end());
}
        int ans=0;
        FOR(i, 0, n)
{
int len = V[i].size();
FOR(k, 0, len)
{
FOR(j, k+1, len) // 经测试,根据POJ的数据,最内层这个计算量不到50w
{
int a = V[i][k];
int b = V[i][j];
if( binary_search(V[a].begin(), V[a].end(), b) )
ans++;
}
}
}
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值