首页 > 代码库 > POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力)
POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力)
Horizontally Visible Segments
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 4130 | Accepted: 1511 |
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
1 5 0 4 4 0 3 1 3 4 2 0 2 2 0 2 3
Sample Output
1
——————————————————分割线————————————————
题目大意:
如果两条线段相交,则叫做水平可见,给出n条这样的线段,求有多少组的3条线段两两可见
思路:
对每条线段的横坐标从小到大排序,然后一边插入,一边统计跟当前要插入的线段是否相交,存放到vector中,才能保证不重复不遗漏。插入的时候给每条线段一个颜色,来区分不同的线段,相当于区间染色
求出哪些线段可以两两相交之后,暴力枚举3条线段两两可见
如果线段x能见到y,z,再判断线段y能不能见到z,如果能,则符合3条线段两两可见
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #define vi vector<int> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=16001; using namespace std; vi v[maxn+5]; int cover[maxn<<2]; int hash[maxn+5]; struct v_seg { int s,t,x; bool operator<(const v_seg&A) const { return x<A.x; } }ss[maxn+5]; void push_down(int rt) { if(cover[rt]!=-1){ cover[rt<<1]=cover[rt<<1|1]=cover[rt]; cover[rt]=-1; } } void update(int L,int R,int c,int l,int r,int rt) { if(L<=l&&r<=R){ cover[rt]=c; return; } push_down(rt); int m=(l+r)>>1; if(L<=m) update(L,R,c,lson); if(m<R) update(L,R,c,rson); } void query(int L,int R,int c,int l,int r,int rt) { if(cover[rt]!=-1){ if(hash[cover[rt]]!=c){ v[cover[rt]].push_back(c); hash[cover[rt]]=c; } return ; } if(l==r) return ; int m=(l+r)>>1; if(L<=m) query(L,R,c,lson); if(m<R) query(L,R,c,rson); } int main() { int T; cin>>T; while(T--){ int n; scanf("%d",&n); for(int i=0;i<n;++i){ scanf("%d %d %d",&ss[i].s,&ss[i].t,&ss[i].x); ss[i].s<<=1,ss[i].t<<=1; v[i].clear(); } sort(ss,ss+n); memset(hash,-1,sizeof(hash)); memset(cover,-1,sizeof(cover)); for(int i=0;i<n;++i){ query(ss[i].s,ss[i].t,i,0,maxn,1); update(ss[i].s,ss[i].t,i,0,maxn,1); } int ans=0; for(int i=0;i<n;++i){ for(int j=0;j<v[i].size();++j){ int x=v[i][j]; for(int k=0;k<v[i].size();++k){ for(int t=0;t<v[x].size();++t){ if(v[i][k]==v[x][t]){ ans++; break; } } } } } printf("%d\n",ans); } return 0; }
POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。