标签:
//优化前
#include <algorithm>
#include <cstdio>
using namespace std;
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
const int maxn = 100000 + 10;
struct Event{
double x;
int type;
Event(){};
Event(double mX,int mType):x(mX),type(mType){}
bool operator < (const Event & a)const{
return x < a.x || (x == a.x && type > a.type);
}
}e[maxn * 2];
void update(int x,int a,int w,double &l,double &r){
if(a == 0){
//无解
if(x <= 0 || x >= w) r = l - 1;
}else if(a > 0){
l = max(l,-(double)x/a);
r = min(r,(double)(w - x)/a);
}else {
l = max(l,(double)(w - x)/a);
r = min(r,-(double)x/a);
}
}
int main(){
int t,w,h,n,x,y,a,b,cnt,ans,cur;
double l,r;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&w,&h,&n);
cnt = 0;
FOR(i,0,n){
scanf("%d%d%d%d",&x,&y,&a,&b);
l = 0,r = 1E9;
update(x,a,w,l,r);
update(y,b,h,l,r);
if(r > l){
e[cnt++] = Event(l,0);
e[cnt++] = Event(r,1);
}
}
sort(e,e + cnt);
cur = 0,ans = 0;
FOR(i,0,cnt){
if(e[i].type) --cur;
else ans = max(++cur,ans);
}
printf("%d\n",ans);
}
return 0;
}
//优化后
#include <algorithm>
#include <cstdio>
using namespace std;
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
const int maxn = 100000 + 10;
struct Event{
int x,type;
Event(){};
Event(int mX,int mType):x(mX),type(mType){}
bool operator < (const Event & a)const{
return x < a.x || (x == a.x && type > a.type);
}
}e[maxn * 2];
void update(int x,int a,int w,int &l,int &r){
if(a == 0){
//无解
if(x <= 0 || x >= w) r = l - 1;
}else if(a > 0){
l = max(l,-x*2520/a);
r = min(r,(w - x)*2520/a);
}else {
l = max(l,(w - x)*2520/a);
r = min(r,-x*2520/a);
}
}
int main(){
int t,w,h,n,x,y,a,b,cnt,ans,cur,l,r;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&w,&h,&n);
cnt = 0;
FOR(i,0,n){
scanf("%d%d%d%d",&x,&y,&a,&b);
l = 0,r = 1E9;
update(x,a,w,l,r);
update(y,b,h,l,r);
if(r > l){
e[cnt++] = Event(l,0);
e[cnt++] = Event(r,1);
}
}
sort(e,e + cnt);
cur = 0,ans = 0;
FOR(i,0,cnt){
if(e[i].type) --cur;
else ans = max(++cur,ans);
}
printf("%d\n",ans);
}
return 0;
}
[2016-03-20][UVALive][3905][Meteor]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/5301510.html