题目大意:给定一棵树,每个点有两个坐标(x1,y1)和(x2,y2),多次询问某条链上选择两个点i和j(可以相同),求(y1i+y2j)/(x1i+x2j)的最大值
我竟没看出来这是01分数规划。。。真是老了。。。
二分答案ans,问题转化成验证(y1i+y2j)/(x1i+x2j)是否>=ans
将式子变形可得(y1i-ans*x1i)+(y2j-ans*x2j)>=0
加号两边独立,分别计算即可
问题转化为求链上y-ans*x最大的点
令P=y-ans*x 则y=ans*x+P
我们发现这是一个斜率优化的形式 因此我们使用树链剖分 用线段树维护凸包即可
每次查询 树链剖分一个log 线段树一个log 每次凸包上二分一个log 加上最外层的二分一共4个log
能过真是奇迹。。。。
#include <cmath> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 30300 #define EPS 1e-7 #define INF 1e10 using namespace std; struct Point{ double x,y; Point() {} Point(double _,double __): x(_),y(__) {} friend Point operator + (const Point &p1,const Point &p2) { return Point(p1.x+p2.x,p1.y+p2.y); } friend Point operator - (const Point &p1,const Point &p2) { return Point(p1.x-p2.x,p1.y-p2.y); } friend double operator * (const Point &p1,const Point &p2) { return p1.x*p2.y-p1.y*p2.x; } friend bool operator < (const Point &p1,const Point &p2) { return p1.x<p2.x || p1.x==p2.x&&p1.y<p2.y ; } friend double Get_Slope(const Point &p1,const Point &p2) { if( fabs(p1.x-p2.x)<EPS ) return p1.y<p2.y?INF:-INF; return (p1.y-p2.y)/(p1.x-p2.x); } }points1[M],points2[M]; struct abcd{ int to,next; }table[M<<1]; int head[M],tot; int n,m; int fa[M],son[M],dpt[M],size[M]; int top[M],pos[M],a[M],cnt; void Add(int x,int y) { table[++tot].to=y; table[tot].next=head[x]; head[x]=tot; } void DFS1(int x) { int i; dpt[x]=dpt[fa[x]]+1; size[x]=1; for(i=head[x];i;i=table[i].next) { if(table[i].to==fa[x]) continue; fa[table[i].to]=x; DFS1(table[i].to); size[x]+=size[table[i].to]; if(size[table[i].to]>size[son[x]]) son[x]=table[i].to; } } void DFS2(int x) { int i; a[pos[x]=++cnt]=x; if(son[fa[x]]==x) top[x]=top[fa[x]]; else top[x]=x; if(son[x]) DFS2(son[x]); for(i=head[x];i;i=table[i].next) { if(table[i].to==fa[x]||table[i].to==son[x]) continue; DFS2(table[i].to); } } void Merge(const vector<Point> &h1,const vector<Point> &h2,vector<Point> &h) { vector<Point>::const_iterator i,j; i=h1.begin();j=h2.begin(); int top=0; while( i!=h1.end() || j!=h2.end() ) { Point p=i==h1.end()?*j++:j==h2.end()?*i++:*i<*j?*i++:*j++; while( top>=2 && (h[top-1]-h[top-2])*(p-h[top-1])>-EPS ) h.pop_back(),top--; h.push_back(p),top++; } } double Bisection(const vector<Point> &h,double k) { int l=0,r=h.size(); while(l+1<r) { int mid=l+r>>1; if( Get_Slope(h[mid-1],h[mid])>k-EPS ) l=mid; else r=mid; } return h[l].y-k*h[l].x; } struct Segtree{ Segtree *ls,*rs; vector<Point> h1,h2; void* operator new (size_t) { static Segtree mempool[M<<1],*C=mempool; return C++; } void Build_Tree(int x,int y) { int mid=x+y>>1; if(x==y) { h1.push_back(points1[a[mid]]); h2.push_back(points2[a[mid]]); return ; } (ls=new Segtree)->Build_Tree(x,mid); (rs=new Segtree)->Build_Tree(mid+1,y); Merge(ls->h1,rs->h1,h1); Merge(ls->h2,rs->h2,h2); } double Query(int x,int y,int l,int r,double k,bool flag) { int mid=x+y>>1; if(x==l&&y==r) return !flag?Bisection(h1,k):Bisection(h2,k); if(r<=mid) return ls->Query(x,mid,l,r,k,flag); if(l>mid) return rs->Query(mid+1,y,l,r,k,flag); return max( ls->Query(x,mid,l,mid,k,flag) , rs->Query(mid+1,y,mid+1,r,k,flag) ); } }*tree=new Segtree[M]; double Query(int x,int y,double k,bool flag) { int fx=top[x],fy=top[y]; double re=-INF; while(fx!=fy) { if(dpt[fx]<dpt[fy]) swap(x,y),swap(fx,fy); re=max(re,tree->Query(1,n,pos[fx],pos[x],k,flag)); x=fa[fx];fx=top[x]; } if(dpt[x]<dpt[y]) swap(x,y); re=max(re,tree->Query(1,n,pos[y],pos[x],k,flag)); return re; } double Bisection(int x,int y) { double l=0,r=1e8; while(r-l>1e-5) { double mid=(l+r)/2.0; if( Query(x,y,mid,false) + Query(x,y,mid,true) > 0 ) l=mid; else r=mid; } return (l+r)/2; } int main() { int i,x,y; cin>>n; for(i=1;i<=n;i++) scanf("%lf",&points1[i].x); for(i=1;i<=n;i++) scanf("%lf",&points1[i].y); for(i=1;i<=n;i++) scanf("%lf",&points2[i].x); for(i=1;i<=n;i++) scanf("%lf",&points2[i].y); for(i=1;i<n;i++) { scanf("%d%d",&x,&y); Add(x,y);Add(y,x); } DFS1(1); DFS2(1); tree->Build_Tree(1,n); cin>>m; for(i=1;i<=m;i++) { scanf("%d%d",&x,&y); printf("%.5lf\n",Bisection(x,y)); } }
时间: 2024-11-11 02:26:28