poj 1436 Horizontally Visible Segments(线段树成段覆盖问题+简单hash),好题,覆盖问题想法较难
来源:互联网 发布:foxpro数据库下载 编辑:程序博客网 时间:2024/06/06 10:42
1、http://poj.org/problem?id=1436
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: 65536KTotal 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.
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.
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
Central Europe 2001
5、AC代码:
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>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<int> 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<b.x;}int main(){ int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].x); a[i].s<<=1; a[i].e<<=1; adj[i].clear();//注意清空 } sort(a,a+n,cmp); memset(cover,-1,sizeof(cover)); memset(hash,-1,sizeof(hash)); for(int i=0;i<n;i++) { query(a[i].s,a[i].e,i,0,N,1); update(a[i].s,a[i].e,i,0,N,1); } for(int i=0;i<n;i++) { //这样就可以给vector容器中每一行从小到大排序 sort(adj[i].begin(),adj[i].end()); } //printf("*************\n");// for(int i=0;i<n;i++)// {// printf("%d:",i);// for(int j=0;j<adj[i].size();j++)// printf("%d ",adj[i][j]);// printf("\n");// } // printf("******************\n"); int ans=0; for(int i=0;i<n;i++) { int len=adj[i].size(); for(int j=0;j<len;j++) { for(int k=j+1;k<len;k++) { int p=adj[i][j]; int q=adj[i][k]; //printf("%d %d\n",p,q); if(binary_search(adj[p].begin(),adj[p].end(),q)) { ans++; } } } } printf("%d\n",ans); } return 0;}
0 0
- poj 1436 Horizontally Visible Segments(线段树成段覆盖问题+简单hash),好题,覆盖问题想法较难
- poj 1436 Horizontally Visible Segments(线段树、区间覆盖)
- poj 1436 Horizontally Visible Segments(线段树 区间的覆盖关系)
- POJ 题目1436 Horizontally Visible Segments(线段树染色覆盖求相互是否可见)
- poj 1436 Horizontally Visible Segments #线段树#hash#暴力枚举
- POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力+简单hash)
- POJ 1436 Horizontally Visible Segments 简单hash+区间更新
- POJ 1436 Horizontally Visible Segments 线段树
- POJ 1436 Horizontally Visible Segments 线段树
- poj 1436 Horizontally Visible Segments[线段树]
- poj 1436 线段树 Horizontally Visible Segments
- POJ 1436 Horizontally Visible Segments (线段树)
- [poj 1436]Horizontally Visible Segments[线段树]
- poj 1436 Horizontally Visible Segments(线段树)
- poj 1436 Horizontally Visible Segments(线段树)
- POJ1436 Horizontally Visible Segments 线段树区间更新--区间覆盖
- POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
- POJ 1436 - Horizontally Visible Segments
- 算法 字符串模式匹配KMP
- 广播电台凭什么顽强抵抗了互联网
- 面试题整理:JavaSE部分
- 基于Linux (RHEL 5.5) 安装Oracle 10g RAC
- SQLServer 2008 (三)
- poj 1436 Horizontally Visible Segments(线段树成段覆盖问题+简单hash),好题,覆盖问题想法较难
- 动态加载js,css文件(跨iframe)
- C++学习之MessageBox和对话框
- Ubuntu 压缩解压命令
- 计算机网络体系结构
- JAVA 对象引用,以及对象赋值
- 输入和输出重载操作符
- 局域网
- Android生命周期实验