博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1436 Horizontally Visible Segments(线段树成段覆盖问题+简单hash),好题,覆盖问题想法较难
阅读量:4035 次
发布时间:2019-05-24

本文共 3000 字,大约阅读时间需要 10 分钟。

1、

2、题目大意:

给出n段垂直的线段,如果两条线段可以用一条水平的线连接起来,且中间不能跟别的垂直的线相交,那么这两条线段称作是可见的,如果有三条线段两两是可见的,那么称作是线段三角形,求给出的这n条垂直的线段可以组成多少个线段三角形?

3、思路分析

要求多少个线段三角形,就是求有多少个三条线段两两相交的,首先我们先求出两条线段相交的情况,也就是求一下跟第一条线段相交的线段有那几条,定义一个vector容器,比如跟第一条相交的有2,3,4;跟第二条线段相交的有3

那么vector里存的就是这样的:

1   2,3,4

2   3

那么我们知道了他们之间的关系,就可以三重for循环遍历一遍求出来即可

例如我们现在已经知道1-2,1-3,我们要证明1,2,3是线段三角形,只需再证明2-3也是可见的就行了,此时去2后面查找是否有3,如果有那么这个就是线段三角形,计数器+1,

对于区间覆盖来说,用一个cover[rt]数组表示,例如cover[rt]=1,表示rt对应的区间被第一条边覆盖了

4、题目:

Horizontally Visible Segments
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3463   Accepted: 1275

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

150 4 40 3 13 4 20 2 20 2 3

Sample Output

1

Source

5、AC代码:

#include
#include
#include
#include
using namespace std;#define N 16005#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int cover[N*4];int hash[N*4];vector
adj[N];struct node{ int s; int e; int x;}a[N];void pushdown(int rt){ if(cover[rt]!=-1) { cover[rt<<1]=cover[rt<<1|1]=cover[rt]; cover[rt]=-1; }}void query(int L,int R,int id,int l,int r,int rt){ if(cover[rt]!=-1) { if(hash[cover[rt]]!=id) { adj[cover[rt]].push_back(id); hash[cover[rt]]=id; } return ; } if(l==r) return ; pushdown(rt); int m=(l+r)>>1; if(L<=m) query(L,R,id,lson); if(R>m) query(L,R,id,rson);}void update(int L,int R,int id,int l,int r,int rt){ if(L<=l && R>=r) { cover[rt]=id; return ; } pushdown(rt); int m=(l+r)>>1; if(L<=m) update(L,R,id,lson); if(R>m) update(L,R,id,rson);}int cmp(node a,node b){ return a.x

 

转载地址:http://swcdi.baihongyu.com/

你可能感兴趣的文章
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>