poj1436(线段树区间染色)
来源:互联网 发布:c语言扑克牌程序 编辑:程序博客网 时间:2024/05/21 10:48
Horizontally Visible Segments
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
==题意:有n条垂直于x轴的线段,给出他们y范围。当两条线段之间有一条垂直于他们并且可以不经过其他线段,则可定义两条线段互相可见,求出有多少三条线段两两互相可见==
==思路:可以把每个线段看成区间,按y轴建树,然后根据线段的x位置从小到大对区间进行染色,在染色之前先对其区间内的颜色进行标记,这些颜色与当前要染的颜色一定互相可见。最后三重for循环求出两两互相可见的三条线段。但要注意有些区间如1-2,3-4,其染色之后2-3这个会被忽略,因为2-3区间没有点,而线段树储存的颜色是在点上。所以要对y扩大两倍,相当于在原先的两点间在插入一点。==
#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <algorithm>#include <functional>#define inf 0x7fffffffusing namespace std;typedef long long ll;const int MAXN=1e9+10;const int MAX=16100+10;int n;bool vis[MAX>>1][MAX>>1];struct Line{ int y1,y2,x;}line[MAX>>1];struct NODE{ int l,r,color;}tree[MAX*4];int cmp(Line a,Line b){return a.x<b.x;}void init(int node,int l,int r){ tree[node].l=l; tree[node].r=r; tree[node].color=0; if(l==r) return ; int mid=(l+r)>>1; init(node<<1,l,mid); init(node<<1|1,mid+1,r); }void push_down(int node){ if(tree[node].color!=-1){ tree[node<<1].color=tree[node<<1|1].color=tree[node].color; tree[node].color=-1; }}void updata_tree(int node,int x,int y,int num){ int l=tree[node].l; int r=tree[node].r; if(x<=l&&r<=y){ tree[node].color=num; return ; } push_down(node); int mid=(l+r)>>1; if(x<=mid) updata_tree(node<<1,x,y,num); if(y>mid) updata_tree(node<<1|1,x,y,num);}void query(int node,int x,int y,int flag){ if(tree[node].color!=-1){ vis[tree[node].color][flag]=1; return ; } int l=tree[node].l; int r=tree[node].r; int mid=(l+r)>>1; if(l==r) return; push_down(node); if(x<=mid) query(node<<1,x,y,flag); if(y>mid) query(node<<1|1,x,y,flag);}int main(){ //ios::sync_with_stdio(false); #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); #endif int T; cin>>T; while(T--){ cin>>n; for(int i=1;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+1,line+n+1,cmp); memset(vis,0,sizeof(vis)); init(1,0,MAX); for(int i=1;i<=n;i++){ query(1,line[i].y1,line[i].y2,i); updata_tree(1,line[i].y1,line[i].y2,i); } int ans=0; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(vis[i][j]){ for(int k=1;k<=n;k++){ if(vis[i][k]&&vis[j][k]) ans++; } } } } cout<<ans<<endl; } return 0;}
- poj1436(线段树区间染色)
- POJ1436 Horizontally Visible Segments 线段树区间更新--区间覆盖
- 【POJ1436】Horizontally Visible Segments-线段树区间更新
- poj2528(线段树区间染色)
- poj1436 线段树+暴力
- 线段树+拆点poj1436
- hdu1698(线段树染色区间求和)
- ZOJ 1610 线段树区间染色
- poj 2777 线段树(区间染色)
- BZOJ 8843 染色 [树链剖分+区间线段树]
- poj 2777(线段树+区间染色)
- poj2528&&zoj1610 线段树区间染色问题
- poj2528(线段树区间染色+离散化)
- 【线段树区间更新 && 染色】ZOJ
- HDU 3911 线段树区间染色 区间查询
- HDU 3397 线段树区间染色 区间查询
- poj1436 Horizontally Visible Segments 线段树
- poj1436(线段树lazy标记)
- 安装Anaconda IDE
- 用python简单处理图片(4):图像中的像素访问
- 软件测试职业培训有感
- 素数环问题 prime ACM HDU 1016
- Mybatis 分页的实现
- poj1436(线段树区间染色)
- 1001 487-3279
- Ruby基础知识(linxu上的安装步骤)
- 学生信息系统学习心得一、配置
- struts2与SpringMVC的区别
- 集群1
- 剑指offer面试题目:两个链表的第一个公共节点
- 软件管理
- xgboost简介