Horizontally Visible Segments
Time Limit: 5000MS | | Memory Limit: 65536K | Total Submissions: 5717 | | Accepted: 2083 |
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 1
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3 Sample Output 1 |
/*
题意:
有很多垂直的线段,两个线段之间存在连线且和其他的线段没有交点为可见,
给出一组数据,求三条线段两两可见的组数。
题解:
将输入的的线段按x从小到大排列,然后先查询再更新,
就是每次插入线段之前,先查询即将插入的那条线段和以前插入的线段是否可见,
每条线段都有一种颜色,所以用一个vis[i][j]表示i与j可见
最后直接暴力枚举就可以过了
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=16100;
bool vis[MAXN>>1][MAXN>>1];
struct node{
int l,r;
int color;
}tree[MAXN<<2];
struct Node{
int x,y1,y2;
}p[MAXN>>1];
bool cmp(Node a,Node b){
return a.x<b.x;
}
void build(int l,int r,int node){
tree[node].l=l;
tree[node].r=r;
tree[node].color=0;
if(l==r)
return;
int mid=(l+r)>>1;
build(l,mid,node<<1);
build(mid+1,r,node<<1|1);
}
void pushdown(int node){
if(!tree[node].color) return;
tree[node<<1].color=tree[node<<1|1].color=tree[node].color;
tree[node].color=0;///取消标记
}
void query(int l,int r,int node,int val){
if(tree[node].color!=0){
vis[tree[node].color][val]=1;
return;
}
if(tree[node].l==tree[node].r)
return;
pushdown(node);
int mid=(tree[node].l+tree[node].r)>>1;
if(l<=mid)
query(l,r,node<<1,val);
if(r>mid)
query(l,r,node<<1|1,val);
}
void update(int l,int r,int node,int val){
if(tree[node].l>=l&&tree[node].r<=r){
tree[node].color=val;
return;
}
pushdown(node);
int mid=(tree[node].l+tree[node].r)>>1;
if(l<=mid)
update(l,r,node<<1,val);
if(r>mid)
update(l,r,node<<1|1,val);
}
int main(){
int t,n,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int maxn = 0;
for(i=1;i<=n;i++){
scanf("%d%d%d",&p[i].y1,&p[i].y2,&p[i].x);
p[i].y1=2*p[i].y1;
p[i].y2=2*p[i].y2;
maxn = max(maxn,p[i].y1);
maxn = max(maxn,p[i].y2);
}
build(0,maxn,1);
sort(p+1,p+n+1,cmp);
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
query(p[i].y1,p[i].y2,1,i);
update(p[i].y1,p[i].y2,1,i);
}
int ans=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(vis[i][j])
for(k=1;k<=n;k++){
if(vis[i][k]&&vis[j][k])
ans++;
}
printf("%d\n",ans);
}
return 0;
}
|