1. 程式人生 > >[BZOJ2402]陶陶的難題II(樹鏈剖分+線段樹維護凸包+分數規劃)

[BZOJ2402]陶陶的難題II(樹鏈剖分+線段樹維護凸包+分數規劃)

五行 add urn build 一行 輸入格式 for res 限制

陶陶的難題II

時間限制:40s 空間限制:128MB

題目描述

技術分享圖片


輸入格式

第一行包含一個正整數N,表示樹中結點的個數。
第二行包含N個正實數,第i個數表示xi (1<=xi<=10^5)。
第三行包含N個正實數,第i個數表示yi (1<=yi<=10^5)。
第四行包含N個正實數,第i個數表示pi (1<=pi<=10^5)。
第五行包含N個正實數,第i個數表示qi (1<=qi<=10^5)。
下面有N-1行,每行包含兩個正整數a,b(1<=a,b<=N),表示樹中的邊。
第N+5行包含一個正整數M,表示詢問的個數。


最後M行,每行包含正整數a,b(1<=a,b<=N),表示一次詢問。


輸出格式

共M行,每行一個實數,第i行的數表示第i次詢問的答案。
只要你的輸出和我們的輸出相差不超過0.001即為正確。


樣例輸入

5 
3.0 1.0 2.0 5.0 4.0 
5.0 2.0 4.0 3.0 1.0 
1.0 3.0 2.0 4.0 5.0 
3.0 4.0 2.0 1.0 4.0 
1 2 
1 3 
2 4 
2 5 
4 
2 3 
4 5 
2 4 
3 5 

樣例輸出

2.5000 
1.5000 
1.5000 
2.5000 

提示

100%的數據滿足N,M≤ 30,000。

1<=Xi,Yi,Pi,Qi<=10^8


題目來源

沒有寫明來源

首先根據答案是個分數以及“你的輸出和我們的輸出相差不超過0.001即為正確”可以想到分數規劃。然後化下式子可以發現i和j是可以分開考慮的,加上題目求的式子是一個分數可以自然想到凸包。但是我們怎麽維護這個凸包呢?由於沒有修改,我們可以通過線段樹套std::vector維護,合並時由於兩邊橫坐標都是單增的所以用類似歸並排序的方法即可。最後,一般凸包問題都是用三分,但是仔細思考會發現其實二分也可以做。這樣總的復雜度就是四個log的(分數規劃,樹鏈剖分,線段樹,二分查找各一個)。由於復雜度並不滿所以可以跑過(當然能用$O(n \log^4 n)$卡掉$O(n^2)$的題並不多見)

  1 #include<cstdio>
  2
#include<vector> 3 #include<algorithm> 4 #define ls (x<<1) 5 #define rs ((x<<1)|1) 6 #define lson ls,L,mid 7 #define rson rs,mid+1,R 8 #define rep(i,l,r) for (int i=l; i<=r; i++) 9 #define For(i,x) for (int i=h[x],k; i; i=nxt[i]) 10 typedef long double ld; 11 using namespace std; 12 13 const int N=50100; 14 int n,m,u,v,cnt,nd,dfn[N],d[N],top[N],sz[N],son[N],f[N],h[N],to[N<<1],nxt[N<<1]; 15 ld vx[N],vy[N],vp[N],vq[N]; 16 17 void add(int u,int v){ to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt; } 18 19 struct Seg{ 20 ld px[N],py[N]; 21 vector<int>V[N<<2]; 22 23 void insert(vector<int>&V,int p){ 24 while (V.size()>1 && (px[V[V.size()-1]]-px[p])*(py[V[V.size()-2]]-py[p])-(py[V[V.size()-1]]-py[p])*(px[V[V.size()-2]]-px[p])<-1e-10) V.pop_back(); 25 V.push_back(p); 26 } 27 28 void build(int x,int L,int R){ 29 if (L==R){ V[x].push_back(L); return; } 30 int mid=(L+R)>>1; 31 build(lson); build(rson); 32 int pl=0,pr=0,tl=V[ls].size(),tr=V[rs].size(); 33 while (pl<tl || pr<tr) 34 if (pr==tr || (pl<tl && px[V[ls][pl]]<px[V[rs][pr]])) insert(V[x],V[ls][pl++]); 35 else insert(V[x],V[rs][pr++]); 36 } 37 38 ld que(int x,int L,int R,int l,int r,ld k){ 39 if (L==l && R==r){ 40 int ll=1,rr=V[x].size()-1,mid,ans=0; 41 while (ll<=rr){ 42 mid=(ll+rr)>>1; 43 if (py[V[x][mid]]-k*px[V[x][mid]]-1e-10>py[V[x][mid-1]]-k*px[V[x][mid-1]]) ans=mid,ll=mid+1; else rr=mid-1; 44 } 45 return py[V[x][ans]]-k*px[V[x][ans]]; 46 } 47 int mid=(L+R)>>1; 48 if (r<=mid) return que(lson,l,r,k); 49 else if (l>mid) return que(rson,l,r,k); 50 else return max(que(lson,l,mid,k),que(rson,mid+1,r,k)); 51 } 52 }A,B; 53 54 ld solve(int x,int y,ld ans){ 55 ld res1=-1e16,res2=-1e16; 56 while (top[x]!=top[y]){ 57 if (d[top[x]]<d[top[y]]) swap(x,y); 58 res1=max(res1,A.que(1,1,n,dfn[top[x]],dfn[x],ans)); 59 res2=max(res2,B.que(1,1,n,dfn[top[x]],dfn[x],ans)); 60 x=f[top[x]]; 61 } 62 if (d[x]>d[y]) swap(x,y); 63 res1=max(res1,A.que(1,1,n,dfn[x],dfn[y],ans)); 64 res2=max(res2,B.que(1,1,n,dfn[x],dfn[y],ans)); 65 return res1+res2; 66 } 67 68 void dfs(int x,int fa){ 69 sz[x]=1; son[x]=0; 70 For(i,x) if ((k=to[i])!=fa){ 71 f[k]=x; d[k]=d[x]+1; dfs(k,x); sz[x]+=sz[k]; 72 if (sz[k]>sz[son[x]]) son[x]=k; 73 } 74 } 75 76 void dfs1(int x,int tp){ 77 dfn[x]=++nd; top[x]=tp; A.px[nd]=vx[x]; A.py[nd]=vy[x]; B.px[nd]=vp[x]; B.py[nd]=vq[x]; 78 if (son[x]) dfs1(son[x],tp); 79 For(i,x) if ((k=to[i])!=f[x] && k!=son[x]) dfs1(k,k); 80 } 81 82 int main(){ 83 freopen("bzoj2402.in","r",stdin); 84 freopen("bzoj2402.out","w",stdout); 85 scanf("%d",&n); 86 rep(i,1,n) scanf("%Lf",&vx[i]); 87 rep(i,1,n) scanf("%Lf",&vy[i]); 88 rep(i,1,n) scanf("%Lf",&vp[i]); 89 rep(i,1,n) scanf("%Lf",&vq[i]); 90 rep(i,1,n-1) scanf("%d%d",&u,&v),add(u,v),add(v,u); 91 dfs(1,0); dfs1(1,1); 92 A.build(1,1,n); B.build(1,1,n); 93 for (scanf("%d",&m); m--; ){ 94 scanf("%d%d",&u,&v); 95 ld L=0,R=1e8; int c=50; 96 while (c--){ 97 ld mid=(L+R)/2; 98 if (solve(u,v,mid)>1e-10) L=mid; else R=mid; 99 } 100 printf("%.5Lf\n",L); 101 } 102 return 0; 103 }

[BZOJ2402]陶陶的難題II(樹鏈剖分+線段樹維護凸包+分數規劃)