Horizontally Visible Segments
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3716 | Accepted: 1364 |
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
好久没写线段树了,都不会写了。。
setv为0表示当前这一段没有被挡,-1表示当前这一段被挡情况不唯一,i(i>0)表示被编号为i的线挡住。这题有个特殊的地方,比如这种情况:
最右边的线段其实是对左边三条都可见的,但是如果按一般的线段树做,做出来最右边的线段是看不见最左边的线段的。所以要对所有坐标乘2,相当于两个点中间的线段也成为一个点,剩下的就和一般线段树一样了。
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 8010
#define MAXM 110
#define MOD 999983
typedef long long LL;
using namespace std;
int T,N,setv[MAXN<<3];
bool vis[MAXN][MAXN];
struct Seg{
int x,y1,y2;
bool operator < (const Seg& a) const{
return x<a.x;
}
}seg[MAXN];
void build(int o,int L,int R){
setv[o]=0;
if(L>=R) return;
int mid=(L+R)>>1;
build(o<<1,L,mid);
build(o<<1|1,mid+1,R);
}
void pushdown(int o){
if(setv[o]!=-1){
setv[o<<1]=setv[o<<1|1]=setv[o];
setv[o]=-1;
}
}
void update(int o,int L,int R,int ql,int qr,int id){
if(ql<=L&&qr>=R){
setv[o]=id;
return;
}
pushdown(o);
int mid=(L+R)>>1;
if(ql<=mid) update(o<<1,L,mid,ql,qr,id);
if(qr>mid) update(o<<1|1,mid+1,R,ql,qr,id);
}
void query(int o,int L,int R,int ql,int qr,int id){
if(setv[o]!=-1){
vis[id][setv[o]]=1;
return;
}
int mid=(L+R)>>1;
if(ql<=mid) query(o<<1,L,mid,ql,qr,id);
if(qr>mid) query(o<<1|1,mid+1,R,ql,qr,id);
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&N);
int MAXY=0;
for(int i=1;i<=N;i++){
scanf("%d%d%d",&seg[i].y1,&seg[i].y2,&seg[i].x);
seg[i].y1<<=1;
seg[i].y2<<=1;
MAXY=max(MAXY,seg[i].y2);
}
sort(seg+1,seg+N+1);
build(1,0,MAXY);
memset(vis,0,sizeof(vis));
for(int i=1;i<=N;i++){
query(1,0,MAXY,seg[i].y1,seg[i].y2,i);
update(1,0,MAXY,seg[i].y1,seg[i].y2,i);
}
LL 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++;
}
printf("%lld\n",ans);
}
return 0;
}