POJ 1436 Horizontally Visible Segments (线段树水过)

题目大意:

就是现在给出n <= 8000条线段, 都是垂直于x轴的且两两没有交点, 从中选出3条如果这3条两两能看到对方则算一种方案, 文友多少种方案


大致思路:

表示没有想到比较靠谱的计数方法, 用线段树处理出各个线段的可视情况之后如何计数感觉网上说的三重循环枚举的计数方法不可靠

但是还没有想到比较好的方法, 先水过去了...

注意纵坐标都乘上了2的原因是为了处理这一类情况:

前面有线段(2, 0)->(2, 2) 和线段(2, 3)->(2, 5)那么当出现(3, 1)->(3, 4)的时候这个线段应该要能通过那两个线段的缝隙看到前面的


代码如下:

Result  :  Accepted     Memory  :  63040 KB     Time  :  1500 ms

/*
 * Author: Gatevin
 * Created Time:  2015/8/17 17:57:22
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 8000*2

struct Segment_Tree
{
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    int color[maxn << 2];
    bool cansee[maxn/2][maxn/2];
    void pushUp(int rt)
    {
        if(color[rt << 1] == color[rt << 1 | 1] && color[rt << 1] != -1)
            color[rt] = color[rt << 1];
        else color[rt] = -1;
    }
    void pushDown(int rt)
    {
        if(color[rt] != -1)
        {
            color[rt << 1] = color[rt << 1 | 1] = color[rt];
            color[rt] = -1;
        }
    }
    void update(int l, int r, int rt, int L, int R, int value)
    {
        if(l >= L && r <= R)
        {
            color[rt] = value;
            return;
        }
        int mid = (l + r) >> 1;
        pushDown(rt);
        if(mid >= L) update(lson, L, R, value);
        if(mid + 1 <= R) update(rson, L, R, value);
        pushUp(rt);
    }
    void query(int l, int r, int rt, int L, int R, int value)
    {
        if(l >= L && r <= R)
        {
            if(color[rt] != -1)
            {
                if(color[rt])
                    cansee[color[rt]][value] = cansee[value][color[rt]] = 1;
                return;
            }
        }
        int mid = (l + r) >> 1;
        pushDown(rt);
        if(mid >= L) query(lson, L, R, value);
        if(mid + 1 <= R) query(rson, L, R, value);
        pushUp(rt);
    }
};

Segment_Tree ST;

struct Segment
{
    int x, y1, y2;
    Segment(int _x, int _y1, int _y2)
    {
        x = _x, y1 = _y1, y2 = _y2;
    }
    Segment(){}
};

bool cmp(Segment s1, Segment s2)
{
    return s1.x < s2.x;
}

Segment s[8080];

int main()
{
    int T, n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        int y1, y2, x;
        int ma = -1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d %d %d", &y1, &y2, &x);
            if(y1 > y2) swap(y1, y2);
            s[i] = Segment(x, y1*2, y2*2);
            ma = max(ma, y2*2);
        }
        sort(s + 1, s + n + 1, cmp);
        ST.color[1] = 0;
        memset(ST.cansee, 0, sizeof(ST.cansee));
        for(int i = 1; i <= n; i++)
        {
            ST.query(0, ma, 1, s[i].y1, s[i].y2, i);
            ST.update(0, ma, 1, s[i].y1, s[i].y2, i);
            
        }
        int ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = i + 1; j <= n; j++)
                if(ST.cansee[i][j])
                    for(int k = j + 1; k <= n; k++)
                        if(ST.cansee[i][k] && ST.cansee[j][k])
                            ans++;
        printf("%d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值