巧妙地预处理 + 扫描线
https://blog.csdn.net/weixin_41380961/article/details/90083696
别人的代码,平台有问题,我没写
https://vjudge.net/problem/UVALive-3905
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define lcm 2520 //lcm(1,2,...,10)=2520 int t,w,h,n; int x,y,a,b; void update(int x,int a,int w,int &l,int &r) { if(a==0&&(x<=0||x>=w))//如果没有答案 { r=l-1; } if(a==0) return ;//如果为垂直或平行 else if(a>0) { l=max(l,-x*lcm/a); r=min(r,(w-x)*lcm/a); } else { l=max(l,(x-w)*lcm/-a); r=min(r,x*lcm/-a); } return ; } struct node { int x,v; node(){}; node(int _x,int _v) { x=_x; v=_v; } int operator < (const node a) const { if(x==a.x) return v<a.v;//因为是开区间,所以要把右端点排在左端点前 return x<a.x; } }A[210000]; int main() { scanf("%d",&t); while(t--) { int tot=0; scanf("%d%d%d",&w,&h,&n); for(int i=1;i<=n;i++) { scanf("%d%d%d%d",&x,&y,&a,&b); int l=0,r=1e9; update(x,a,w,l,r); update(y,b,h,l,r); if(r>l) { A[tot++]=node(l,1); A[tot++]=node(r,-1); } } sort(A,A+tot); int ans=0,New=0; for(int i=0;i<tot;i++) { New+=A[i].v; ans=max(ans,New); } printf("%d ",ans); } return 0; }