您的位置:首页 > 其它

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

2015-08-17 18:56 330 查看
题目大意:

就是现在给出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ 1436 线段树