[原创]POJ 1436 Horizontally Visible Segments [线段树-区间更新]【数据结构】
[原创]POJ 1436 Horizontally Visible Segments [线段树-区间更新]【数据结构】
2016-12-09 17:08:59 Tabris_ 阅读数:233
博客爬取于2020-06-14 22:42:30
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/53541831
题目链接:http://poj.org/problem?id=1436
-------------------------------------------------------------------------.
Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5262 Accepted: 1932
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
Central Europe 2001
---------------------------------------------------------------------------.
题目大意:
就是在二维平面上有n个平行于Y轴的线段,问你两两能用水平线(平行于X轴)直接连接在一起的三元组有多少个。
大概就这意思吧
解题思路:
遇到知道题目的做法一定是考虑怎么判断两个线段是不是能够直接连接在一起的,其实只要维护一个线段树从左向右或者从右向左扫一遍就行了,对于每个线段每个都染色,维护最左或最右边的颜色就行了,只要先查询,后更新就能判断出当前的线段和之前出现过的几个线段能够直接相连接的了。
但是要注意的是,在维护线段树的时候不能直接用(y1,y2)来更新。因为会在(1,4,1)(1,2,2)(3,4,2)(1,4,3)这样的情况下出现第四条线段查询的时候查询不到第一条,所以维护的室友要用(y12,y22)来维护,这样的话。就能空出来一个5,这样就能够查询到了。
期间两个边的链接关系用一个bool型的二维数组就可以了,这样不会MLE了
最后统计结果的时候因为时间给的很宽只要三层for中间剪下枝就能怼过去了。
※数据不保证一定是从左到右的 一定要排序啊.
附本题代码
--------------------------------------------------------------------------------------------.
1 | //#include <bits/stdc++.h> |