码迷,mamicode.com
首页 > 其他好文 > 详细

线段树区间更新——POJ 1436

时间:2014-11-02 09:22:09      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   color   os   ar   for   sp   

对应POJ题目:点击打开链接


Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

 Status

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

Source


题意:在一个平面内,有一些竖直的线段,若两条竖直线段之间可以连一条水平线,这条水平线不与其他竖直线段相交,称这两条竖直线段为相互可见的。若存在三条竖直线段,两两“相互可见”,则构成“线段三角形”。给出一些竖直的线段,问一共有多少“线段三角形”。


思路:求两两“相互可见”的所有可能,暴力求解,复杂度n^3;线段树结点维护区间有多少条可见线段


比较低效的方法╮(╯_╰)╭,有空改善。。。


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define ms(x,y) memset(x,y,sizeof(x))
#define N 8001<<1
const int MAXN=1000+10;
const int INF=1<<30;
using namespace std;
int color[N<<2];
bool vis[N>>1][N>>1];

struct Line
{
	int y1,y2,x;
}line[N>>1];

void down(int rt)
{
	if(color[rt])
	{
		color[rt<<1]=color[rt];
		color[rt<<1|1]=color[rt];
		color[rt]=0;
	}
}

void updata(int rt, int left, int right, int l, int r, int c)
{
	if(left==l && right==r){
		color[rt]=c;
		return;
	}
	down(rt);
	int mid=(left+right)>>1;
	if(mid>=r) updata(rt<<1, left, mid, l, r, c);
	else if(mid<l) updata(rt<<1|1, mid+1, right, l, r, c);
	else{
		updata(rt<<1, left, mid, l, mid, c);
		updata(rt<<1|1, mid+1, right, mid+1, r, c);
	}
}

void query(int rt, int left, int right, int l, int r, int id)
{
	if(color[rt]){
		vis[id][color[rt]]=vis[color[rt]][id]=1;
		return;
	}
	if(left==right) return;
	down(rt);
	int mid=(left+right)>>1;
	if(mid>=r) query(rt<<1, left, mid, l, r, id);
	else if(mid<l) query(rt<<1|1, mid+1, right, l, r, id);
	else{
		query(rt<<1, left, mid, l, mid, id);
		query(rt<<1|1, mid+1, right, mid+1, r, id);
	}
}

bool cmp(Line l1, Line l2)
{
	return l1.x<l2.x;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int T;
	scanf("%d", &T);
	while(T--)
	{
		ms(vis,0);
		ms(color,0);
		int n;
		scanf("%d", &n);
		for(int i=0; i<n; i++){
			scanf("%d%d%d", &line[i].y1, &line[i].y2, &line[i].x);
			line[i].y1<<=1;
			line[i].y2<<=1;
		}
		sort(line, line+n, cmp);
		int cnt=0;
		for(int i=0; i<n; i++){
			++cnt;
			query(1, 0, N, line[i].y1, line[i].y2, cnt);
			updata(1, 0, N, line[i].y1, line[i].y2, cnt);
		}
		int ans=0;
		for(int i=1; i<=n; i++){
			for(int j=i+1; j<=n; j++){
				if(!vis[i][j]) continue;
				for(int k=j+1; k<=n; k++)
					if(vis[j][k] && vis[i][k]) ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}




线段树区间更新——POJ 1436

标签:des   style   http   io   color   os   ar   for   sp   

原文地址:http://blog.csdn.net/u013351484/article/details/40688251

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!