线段树区间更新——POJ 1436

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

Horizontally Visible Segments

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

Submit 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;
}
时间: 2024-08-10 15:09:33

线段树区间更新——POJ 1436的相关文章

线段树区间更新——POJ 2777

对应POJ题目:点击打开链接 Count Color Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, w

POJ 2528 Mayor&#39;s posters (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

线段树 + 区间更新 + 模板 ---- poj 3468

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59798   Accepted: 18237 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

线段树区间更新+向量知识——POJ 2991

对应POJ题目:点击打开链接 Crane Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description ACM has bought a new crane (crane -- je?áb) . The crane consists of n segments of various lengths, connected by flexible joints

poj 3468 A Simple Problem with Integers (线段树区间更新入门)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 70442   Accepted: 21723 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 2777 Count Color (线段树区间更新)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37647   Accepted: 11315 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

poj 2528 Mayor&#39;s posters 线段树区间更新

Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at al