poj1436 Horizontally Visible Segments
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
题意是如果两条线段之间能被一条平行于x轴的线段相连且这条线段和其他线段没有交点,那么这两条线段可见,如果三条线段每两条线段可见,那么他们能组成特定三角形,那么问三角形有多少个。这题先把所有线段储存起来,按x大小升序排列,然后相当于依次读入不同颜色的线段,每次操作,先判断这条线段所在的纵坐标范围内颜色种类,这些颜色种类对应的线段和当前这条线段是可见的,接着把这条线段插入区间,更新总区间的颜色。这里有一点要注意,为了避免单位元线段被“忽略”,把所有的纵坐标都乘2.如3 0 4 1 0 2 2 3 4 2这组数据不乘2的话2-3会被忽略。刚开始所有颜色都为0,如果线段是纯色,那么为大于0的数,若为-1,则是杂色,要在子区间找。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
struct node{
int l,r,cnt;
}b[8*8005];
struct edge{
int y2,y3,x;
}s[8005];
bool cmp(edge a,edge b){
return a.x<b.x;
}
bool mark[8015][8015];
int k;
void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;b[i].cnt=0;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
}
void update(int l,int r,int value,int i)
{
int mid;
if(b[i].l==l && b[i].r==r){
b[i].cnt=value;return;
}
if(b[i].cnt!=-1){
b[i*2].cnt=b[i*2+1].cnt=b[i].cnt;b[i].cnt=-1;
}
mid=(b[i].l+b[i].r)/2;
if(r<=mid)update(l,r,value,i*2);
else if(l>mid)update(l,r,value,i*2+1);
else {
update(l,mid,value,i*2);
update(mid+1,r,value,i*2+1);
}
}
void question(int l,int r,int id,int i)
{
int mid;
if(b[i].cnt>0){
mark[b[i].cnt][id]=true;return;
}
if(b[i].cnt==0 || (b[i].l==b[i].r))return;
mid=(b[i].l+b[i].r)/2;
if(r<=mid)question(l,r,id,i*2);
else if(l>mid)question(l,r,id,i*2+1);
else {
question(l,mid,id,i*2);
question(mid+1,r,id,i*2+1);
}
}
int main()
{
int n,m,i,j,T,x,y2,y3,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d%d%d",&y2,&y3,&x);
s[i].y2=2*y2;s[i].y3=2*y3;s[i].x=x;
}
sort(s+1,s+n+1,cmp);
memset(mark,false,sizeof(mark));
build(0,16000,1);
for(i=1;i<=n;i++){
question(s[i].y2,s[i].y3,i,1);
update(s[i].y2,s[i].y3,i,1);
}
ans=0;
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++){
if(mark[i][j])
{
for(k=j+1;k<=n;k++){
if(mark[j][k] && mark[i][k]){
ans++;
//printf("%d %d %d\n",i,j,k);
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}