POJ 1436 Horizontally Visible Segments

题意:
给你一些垂直于x轴的直线,给出这条直线的两个y坐标还有一个x坐标。
求有几组可以在水平方向上两两互相看见的三条线段。

样例:

横坐标为1的、横坐标为2的(3,4)、横坐标为4的可以互相两两在水平面上看见

思路:
由于是遮挡可以看成是覆盖,所以我们可以定义一个顺序进行覆盖,
可以从左到右,也可以从右到左,那我们从左到右进行染色。
对x坐标进行升序排列,然后从0-n-1依次进行染色。
不过在这里,先进行查询,然后再染色,这样就可以知道,我这条线段能看到哪种颜色(也就是哪条线段)。
记录可以用一个二维数组来记,同样也可以用map来记,但是这里看到网上说8000*8000 bool这道题竟然不会超,
所以。。嘿嘿!
不过需要注意一点,由于这里面都是线段的区间,所以需要把对应坐标扩大二倍,举个例子来说:
1 3 1
3 4 2
1 2 2
1 4 3
如果是仅仅对坐标进行染色,那么就会出现横坐标为3的看不见1的那条线段。

Code:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<bitset>
#include<vector>
#include<list>
#include<set>
#include<map>

//#define TEST

#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef TEST
    #define See(a) cout << #a << " = " << a << endl;
    #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
    #define debug(a, s, e) rep(_i, s, e) {cout << a[_i] << ' ';}cout << endl;
    #define debug2(a, s, e, ss, ee) rep(i_, s, e){debug(a[i_], ss, ee);}
#else
    #define See(a)
    #define See2(a, b)
    #define debug(a, s, e)
    #define debug2(a, s, e, ss, ee)
#endif

const int MAX = 2e9;
const int MIN = -2e9;
const double eps = 1e-8;
const double PI = acos(-1.0);

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int N = (8000 + 5) << 1;

int f[N << 2], alt[N << 2];
bool v[N >> 1][N >> 1];

struct Node
{
    int x, y1, y2;
    Node(int x, int y1, int y2) : x(x), y1(y1), y2(y2) {}
    Node(){}
}e[N];

bool cmp(Node a, Node b)
{
    return a.x < b.x;
}

void pushDown(int rt)
{
    if(f[rt] != -1)
    {
        f[rt << 1] = f[rt << 1 | 1] = f[rt];
        f[rt] = -1;
    }
}

void update(int L, int R, int x, int l, int r, int rt)
{
    if(l >= L && r <= R)
    {
        f[rt] = x;
        return ;
    }
    pushDown(rt);
    int m = (l + r) >> 1;
    if(L <= m) update(L, R, x, lson);
    if(R > m) update(L, R, x, rson);
}

void query(int L, int R, int x, int l, int r, int rt)
{
    if(f[rt] != -1 )
    {
        v[f[rt]][x] = true;
        return ;
    }
    if(l == r) return ;
    pushDown(rt);
    int m = (l + r) >> 1;
    if(L <= m) query(L, R, x, lson);
    if(R > m) query(L, R, x, rson);
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        Mt(f, -1);
        Mt(alt, -1);
        Mt(v, false);
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i)
        {
            int y1, y2, x;
            scanf("%d%d%d", &y1, &y2, &x);
            e[i] = Node(x, y1 << 1, y2 << 1);
        }
        sort(e, e + n, cmp);
        for(int i = 0; i < n; ++i)
        {
            query(e[i].y1, e[i].y2, i, 0, 16000, 1);
            update(e[i].y1, e[i].y2, i, 0, 16000, 1);
        }
        int ans = 0;
        for(int i = 0; i < n; ++i)
        {
            for(int k = i + 1; k < n; ++k)
            {
                if(v[i][k])
                {
                    for(int u = k + 1; u < n; ++u)
                    {
                        if(v[i][u] && v[k][u])
                        {
                            ++ans;
                        }
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值