问n条平行于x,y的直线交点个数
https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/6/CGL_6_A
struct Line {
int st,ed,pos;
Line() {}
Line(int a,int b,int c) {
st=a,ed=b,pos=c;
}
bool operator<(const Line&B)const {
return pos < B.pos;
}
};
int n;
Point p[N],tmp[N];
vector<Line>vx,vy;
void work() {
vx.clear(),vy.clear();
scanf("%d",&n);
for(int i=0; i<n; i++) {
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1!=x2) {
vx.push_back(Line(min(x1,x2),max(x1,x2),y1));
} else {
vy.push_back(Line(min(y1,y2),max(y1,y2),x1));
}
}
sort(vy.begin(),vy.end());//根据x排序
vector<Line>::iterator it;
int ans =0;
for(it=vx.begin(); it!=vx.end(); it++) {
Line now = *it;
Line s=Line(0,0,now.st);
Line e=Line(0,0,now.ed+1);
int l = lower_bound(vy.begin(),vy.end(),s)-vy.begin();
int r = lower_bound(vy.begin(),vy.end(),e)-vy.begin();
for(int i=l; i<r; i++) {
if(vy[i].st<=now.pos && vy[i].ed>=now.pos)ans++;
}
}
printf("%d
",ans);
}