poj 1436

      题意:给出平面上一条垂线的坐标y1,y2,x,y1和y2分别代表垂线两个端点的纵坐标值,x代表横纵坐标。以三条线段为一组,要求输出有多少组里任意两条线段都是相互可见的。可见的定义为:有一条水平线段,能够直接的连接两条垂线,中间不经过其它垂线,则称这两条垂线相互可见。

    注意:两条垂线仅端点相交,也算是可见的。

    分析:实质就是线段树的区间更新问题,是poj2777区间染色问题的延伸。先将所有垂线按x升序排序,然后把每一个垂线当做一种颜色,更新一个垂线的区间时,判断这个区间内有多少种不同颜色,则这个垂线与多少其它垂线可见,用一个二维矩阵记录下这些消息,接着更新覆盖,再对下一个垂线重复操作。

    用矩阵记录下所有两两相互可见的垂线后,暴力求解本题答案就行了。

   代码如下:

 

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lrt rt << 1
#define rrt rt << 1|1
#define root 1,m,1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const LL INF = (((LL)1)<<62)+1;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const double M=1e-8;
const int N=8000+10;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int n,m;
bool vis[N][N];
LL st[N<<3];
struct Line
{
    int x,y1,y2;
};
Line line[N];
bool cmp(Line a,Line b)
{
    return a.x==b.x?a.y1<b.y1:a.x<b.x;
}
void pushdown(int rt)
{
    if (st[rt])
    {
        st[lrt]=st[rt];
        st[rrt]=st[rt];
        st[rt]=0;
    }
}
void query(int c,int l,int r,int rt)
{
    //if (a>r || b<l) return ;
    if (st[rt]>0) {
        int t=st[rt];
        vis[c][t]=vis[t][c]= true;
        return ;
    }
    if (l==r) return ;
    pushdown(rt);
    int mid=MID(l,r);
    query(c,lson);
    query(c,rson);
}
void updata(int a,int b,int c,int l,int r,int rt)
{
    if (a>r || b<l) return ;
    if (a<=l && r<=b) {
        query(c,l,r,rt);
        st[rt]=c;                      // cout<<"->"<<rt<<" "<<l<<" "<<r<<endl;
        return ;
    }
    pushdown(rt);
    int mid=MID(l,r);
    if (a<=mid) updata(a,b,c,lson);
    if (b>mid) updata(a,b,c,rson);
}
void slove()
{
    int i,j,e,cnt=0;
    for (i=1;i<=n;i++) {
        for (j=i+1;j<=n;j++) if (vis[i][j]) {
            for (e=j+1;e<=n;e++) if (vis[i][e] && vis[j][e]) cnt++;
        }
    }
    printf("%d\n",cnt);
}
int main()
{
    int i,j;
    int T;
    cin>>T;
    m=N*2;
    while(T--)
    {
        MS(vis,false);
        MS(st,0);
        scanf("%d",&n);
        for (i=0;i<n;i++) {
            scanf("%d%d%d",&line[i].y1,&line[i].y2,&line[i].x);
            line[i].y1*=2;
            line[i].y2*=2;
        }
        sort(line,line+n,cmp);
        for (i=1;i<=n;i++) {
            updata(line[i-1].y1,line[i-1].y2,i,root);
        }
        slove();
    }
}


/*
4
4
1 2 1
2 3 2
3 4 3
1 4 4

*/




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值