[Bzoj4446] [loj # 2009] [Scoi2015] play small convex Chamber of Secrets

Description

Small square and small projections similar play escape chamber, this chamber there is a \ (n-\) complete binary tree nodes, each node has a bulb. All bulbs can light up to escape the Chamber of Secrets. Each lamp has a weight \ (Ai \) , each side has a weight \ (BI \) . Lighting of a lamp does not take, after each new lighting a lamp \ (V \) spent, it is equal to a lighting bulb \ (the U-\) to the point \ (V \) distance \ (Du, v \) , multiplied by the weight of the point \ (Av \) . During the lighting, any time to ensure that all communication must be lit bulb, which must be lit after lighting subtree in order to light a lamp bulb all other lamps.
Please tell them to spend at least escaped the Chamber of Secrets it is.

Input

Line 1 contains a number of \ (n-\) , the number of nodes in the representative
line 2 contains a \ (n-\) number, the representative value for each node weight \ (AI \) . ( \ (I = 1,2, ..., n-\) )
Line 3 contains \ (n-1 \) number, the representation of the value of each edge \ (BI \) , the first \ (I \) No. edge the first is \ ((i + 1) / 2 \) dot is connected to the second \ (i + 1 \) side of the dot. ( \ (L = I, 2, .... 1-N \) )

Output

1 comprises a number of outputs, representing the least expensive.

Sample Input

3

5 1 2

2 1

Sample Output

5

HINT

For \ (100 \% \) data, \ (. 1 \ Leq N \ Leq 2 \ ^ 10. 5 Times \) , \ (. 1 <Ai, of Bi \ Leq. 5 ^ 10 \)


idea

Clear tree \ (the DP \) .
But pay attention to two issues in the pit ! ! ! ! ! !
The first node is not necessarily a lighting point 1!
"Complete binary tree" means the first \ (i \) points father \ (i / 2 \) , but does not guarantee that all non-leaf nodes have two children!

First assume that the first point is a lighted.
Then tree \ (DP \) status of:
\ (DP [I] [J] \) represents the current \ (I \) has been lit, the lighting start to \ (I \) is the root of the subtree, after its full lit, the last point went to \ (j \) to light up \ (j \) minimum cost.
Transfer is also very obvious, considering what about the child first lit on the line, memory search.
Since in this case, for every \ (i \) , useful \ (dp [i] [j ] \) in the \ (j \) for all ancestors of another child, no more than \ (O (logn) \) months, so the total number of states \ (O (nlogn) \) , does not time out.

Pay first serve, \ (WA \) a.
Root began to change.
For the first point lights, or to put it in the sub-tree is lit, then lit its parent.
Then for each \ (i \) , useful \ (dp [i] [j ] \) in the \ (j \) in addition to all the other children ancestors, as well as all of its ancestors, but still \ ( O (logn) \) level, the total complexity \ (O (nlogn) \) .
Memory Search, and then timed out \ (qwq \)

It is not of the memory (using \ (Map \) constants are too large [Behind his])
re-arranged state - \
(F [I] [J] \) represents \ (DP [I] [Y] \) , wherein \ (Y \) is the \ (I \) of \ (j + 1 \) ancestor.
\ (g [i] [j ] \) represents \ (DP [I] [Z] \) , where \ (Z \) of \ (I \) of \ (j + 1 \) ancestor of another child.
\ (O (nlogn) \) time can these values are calculated, and then back to the root.

Pay first serve, \ (WA \) a.
It found that not every non-leaf node has two children, so he changed to change the details. Finally \ (A \) out!


Code

Very much afraid [details]

#include<cstdio>
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int read(){
    int x=0;
    char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}
 
const int N = 200005;
typedef long long ll;
 
int n,a[N],b[N];
ll f[N][20],g[N][20];
 
ll ans;
void dfs(int x,ll cur){ //换根
    int l=x*2,r=x*2+1;
    if(x!=1){
        ll now;
        if(r<=n) now=min(1ll*a[l]*b[l]+g[l][0]+f[r][1],1ll*a[r]*b[r]+g[r][0]+f[l][1]);
        else if(l==n) now=1ll*a[l]*b[l]+f[l][1];
        else now=f[x][0];
        ans=min(ans,now+cur);
    }
    if(l>n) return;
    if(l==n) dfs(l,cur+1ll*b[x]*a[x/2]);
    else{
        dfs(l,cur+1ll*a[r]*b[r]+f[r][1]); 
        dfs(r,cur+1ll*a[l]*b[l]+f[l][1]);
    }
}
 
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=2;i<=n;i++) b[i]=read();
     
    for(int i=n;i>0;i--){
        if(i*2>n){
            int x=i/2,last=(i&1) ? i-1 : i+1;
            ll s=b[i];
            for(int j=0;x>=0;j++,x/=2){
                f[i][j]=s*a[x];
                g[i][j]=1ll*(s+b[last])*a[last];
                s+=b[x]; last=(x&1) ? x-1 : x+1;
                if(x==0) break;
            }
            continue;
        }
        else if(i*2==n){
            for(int j=0,x=i/2;x>=0;j++,x/=2){
                f[i][j]=1ll*a[n]*b[n]+f[n][j+1];
                g[i][j]=1ll*a[n]*b[n]+g[n][j+1];
                if(x==0) break;
            }
            continue;
        }
        int l=i*2,r=l+1;
        for(int j=0,x=i/2;x>=0;j++,x/=2){
            f[i][j]=min(1ll*a[l]*b[l]+g[l][0]+f[r][j+1],1ll*a[r]*b[r]+g[r][0]+f[l][j+1]);
            g[i][j]=min(1ll*a[l]*b[l]+g[l][0]+g[r][j+1],1ll*a[r]*b[r]+g[r][0]+g[l][j+1]);
            if(x==0) break;
        }
    }
     
    ans=f[1][0];
    dfs(1,0);
    printf("%lld\n",ans);
     
    return 0;
} 

Guess you like

Origin www.cnblogs.com/lindalee/p/11404035.html