题解:P3081 [USACO13MAR] Hill Walk G
纪念我长达 1.25h 的提交。
题意:给定
掉到下面第一根线段提示了我们李超线段树单点查最值,但是它不能求范围不超过
不妨将线段按
并且,所有经过直线
而且,
如果你交上去 WA 但是本地 AC,请相信科学,不要特判数据,开 long double 应该就能过了。
:::success[AC 代码]{open}
#include <bits/stdc++.h>
#define mid ((l+r)>>1)
#define fi first
#define se second
using namespace std;
using ll= long long;
using db= long double;
using pdi= pair<long double,int>;
const int N=5000005,H=10000005,V=2000000005;
const db eps=1e-9;
struct line {
int x1,y1,x2,y2;
line(int x1=0,int y1=-1,int x2=0,int y2=-1): x1(x1),y1(y1),x2(x2),y2(y2) {}
db val(int x) const {
if(x1==x2) return x==x1?max(y1,y2):-1;
db k=(y1-y2)/db(x1-x2);
db b=y1-k*x1;
return k*x+b;
}
} a[N];
int ttot,lc[H],rc[H],bst[H];
db sub(int x,int y,int p) {
return a[x].val(p)-a[y].val(p);
}
bool lbst(int x,int y,int p) {
if(!x) return 0;
if(!y) return 1;
return sub(x,y,p)>eps||(sub(x,y,p)>-eps&&x>y);
}
void upd(int& u,int l,int r,int x) {
if(!u) u=++ttot;
if(lbst(x,bst[u],mid)) swap(bst[u],x);
if(lbst(x,bst[u],l)) upd(lc[u],l,mid,x);
if(lbst(x,bst[u],r)) upd(rc[u],mid+1,r,x);
}
void upd(int& u,int l,int r,int s,int t,int x) {
if(t<l||s>r) return;
if(s<=l&&r<=t) return upd(u,l,r,x),void();
if(!u) u=++ttot;
upd(lc[u],l,mid,s,t,x);
upd(rc[u],mid+1,r,s,t,x);
}
pdi mx(pdi x,pdi y) {
if(x.fi-y.fi>eps) return x;
else return y;
}
pdi query(int u,int l,int r,int s) {
if(s<l||s>r||!u) return make_pair(-1,0);
pdi ret(a[bst[u]].val(s),bst[u]);
if(l==r) return ret;
return mx(ret,mx(query(lc[u],l,mid,s),query(rc[u],mid+1,r,s)));
}
int n,root;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
for(int x1,y1,x2,y2,i=1;i<=n;i++) {
cin>>x1>>y1>>x2>>y2;
a[i]=line(x1,y1,x2,y2);
}
sort(a+2,a+n+1,[](const line& x,const line& y) {
return x.x1<y.x1;
});
int p=2,i=1,ans=0;
do {
ans++;
if(p<=i) p=i+1;
while(p<=n&&a[i].x2>=a[p].x1) {
if(a[i].y2>=a[p].val(a[i].x2))
upd(root,0,V,a[p].x1,a[p].x2-1,p);
p++;
}
i=query(root,0,V,a[i].x2).se;
} while(i);
cout<<ans;
return 0;
}
| ::: |
|---|