POJ-1436 Horizontally Visible Segments

Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K

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

————————————————————集训27.1的分割线————————————————————

思路:之前没有写查询操作,只写了一个更新操作,无情地TLE了。更新是这样进行的——首先是一旦区间有颜色,建立边,然后下放标记。如果在有效区间内,则更换懒惰标记。但是这个时候并不能return。因为有可能区间的颜色非常散,最坏是一个叶结点有一个颜色。而父结点因为下放懒惰标记失去了颜色。每次必须更新到叶结点为止。

所以懒惰标记的作用丧失了。

应该把查询和更新操作分开进行。

查询:一旦有颜色,说明该区间全部都是该颜色,不论是不是有效区间,因为和有效区间一定有交集,所以直接建立边就可以return了。如果最坏情况,还是要走到叶结点为止。

更新:一旦到了有效区间,直接成段替换,return。否则下放懒惰标记。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mid int m = (l+r)>>1
const int N = 8005, maxn = 8005*2;
struct Node
{
	int y1, y2, x;
}edge[N];
int col[maxn<<2];
bool G[N][N];

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

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

void query(int L, int R, int id, int l, int r, int rt)
{
    if(col[rt] != -1) {
        if(!G[id][col[rt]]) {
            G[col[rt]][id] = G[id][col[rt]] = 1;
        }
        return ;
    }
    if(l == r) return ;
    mid;
    if(L <= m) query(L, R, id, lson);
    if(m < R) query(L, R, id, rson);
}

void update(int L, int R, int id, int l, int r, int rt)
{
    if(L <= l && r <= R) {
        col[rt] = id;
        return ;
    }
    Down(rt);
    mid;
    if(L <= m) update(L, R, id, lson);
    if(m < R) update(L, R, id, rson);
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		int n;
		scanf("%d", &n);
		int y1, y2;
		for(int i = 0; i < n; i++) {
			scanf("%d%d%d", &y1, &y2, &edge[i].x);
			edge[i].y1 = y1<<1; edge[i].y2 = y2<<1;
			for(int j = 0; j < n; j++) G[i][j] = false;
		}
		sort(edge, edge+n, cmp);
		memset(col, -1, sizeof(col));
		for(int i = 0; i < n; i++) {
			query(edge[i].y1, edge[i].y2, i, 0, maxn, 1);
			update(edge[i].y1, edge[i].y2, i, 0, maxn, 1);
		}
		int ans = 0;
		for(int i = 0; i < n; i++) {
			for(int j = i+1; j < n; j++) if(G[i][j]) {
				for(int k = j+1; k < n; k++) {
					if(G[j][k] && G[i][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、付费专栏及课程。

余额充值