首页 技术 正文
技术 2022年11月19日
0 收藏 798 点赞 2,207 浏览 1644 个字

pro:给定平面上N条直线,保证没有直线和Y轴平行。 求有多少交点的X坐标落在(L,R)开区间之间,注意在x=L或者R处的不算。

sol:求出每条直线与L和R的交点,如果A直线和B直线在(L,R)相交,一定有Xa<Xb而且Ya>Yb(或相反);那么即是求逆序对。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pdd pair<double,double>
#define ll long long
using namespace std;
const int maxn=;
struct point{
double x,y;
point(){}
point(double xx,double yy):x(xx),y(yy){}
}s[maxn];
struct line{
point s,p;
line(){}
line(point xx,point yy):s(xx),p(yy){}
};
point operator *(double t,point a){ return point(t*a.x,t*a.y);}
point operator -(point w,point v){return point(w.x-v.x,w.y-v.y);}
point operator +(point w,point v){return point(w.x+v.x,w.y+v.y);}
double det(point w,point v){ return w.x*v.y-w.y*v.x;}
double dot(point w,point v){ return w.x*v.x+w.y*v.y;}
point inters(line a,line b){
point p=a.s-b.s;
double t=det(b.p,p)/det(a.p,b.p);
return a.s+t*a.p;
}
int sum[maxn],b[maxn],N; double L,R,a[maxn];
point A[maxn],B[maxn]; ll ans;
line F,C;
struct in{
double a,b; int id;
bool friend operator<(in w,in v){
if(w.a==v.a) return w.b<v.b;
return w.a<v.a;
}
}fcy[maxn],ys[maxn];
void add(int x){
while(x<=N) {
sum[x]++;
x+=(-x)&x;
}
}
int query(int x){
int res=; while(x){
res+=sum[x];
x-=(-x)&x;
}
return res;
}
int main()
{
while(~scanf("%d",&N)&&N){
rep(i,,N)
scanf("%lf%lf%lf%lf",&A[i].x,&A[i].y,&B[i].x,&B[i].y);
scanf("%lf%lf",&L,&R);
F=line(point(L,0.0),point(.,1.0));
C=line(point(R,0.0),point(.,1.0));
rep(i,,N) {
point tA=A[i],tB=B[i];
A[i]=inters(line(tA,tB-tA),F);
B[i]=inters(line(tA,tB-tA),C);
}
rep(i,,N) fcy[i].a=A[i].y,fcy[i].b=B[i].y; sort(fcy+,fcy+N+);
rep(i,,N) ys[i].a=fcy[i].b,ys[i].id=i;
sort(ys+,ys+N+);
rep(i,,N) a[i]=ys[i].a;
rep(i,,N) b[ys[i].id]=lower_bound(a+,a+N+,ys[i].a)-a;
ans=;
rep(i,,N) sum[i]=;
for(int i=N;i>=;i--){
ans+=query(b[i]-);
add(b[i]);
}
printf("%lld\n",ans);
}
return ;
}
上一篇: mac随笔
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860