poj3162 (dp + segment tree tree seeking the maximum and minimum)

Topic link: https: //vjudge.net/problem/POJ-3162

Question is intended: to a tree, each node of the tree seeking maximum distance, referred to as a [i], and then find the maximum interval [l, r] in a range satisfying max (a [i]) - min ( a [i]) <= M.

Ideas: The first step to hdoj2196 that the same problem tree dp calculated for each node of the longest distance, I have another blog post written https://www.cnblogs.com/FrankChen831X/p/11375572. html. After obtaining the maximum distance a [i], to establish minimum and maximum segment tree maintenance interval. Then two pointers i, j traversed again, each time is obtained [i, j] of the maximum and minimum ans1 ANS2, updating answer, because not every j initialization, the total complexity of O (nlogn).

AC Code:

#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long LL;
const int maxn=1e6+5;
const LL inf=0x3f3f3f3f3f3f3f3f;
int n,ans,cnt,head[maxn],pt[maxn],a[maxn];
LL M,dp[maxn][3],ans1,ans2;

struct node1{
    int v,nex;
    LL w;
}edge[maxn<<1];

struct node2{
    int l,r;
    LL Max,Min;
}tr[maxn<<2];

void adde(int u,int v,LL w){
    edge[++cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].nex=head[u];
    head[u]=cnt;
}

void dfs1(int u,int fa){
    for(int i=head[u];i;i=edge[i].nex){
        int v=edge[i].v;
        LL w=edge[i].w;
        if(v==fa) continue;
        dfs1(v,u);
        if(w+dp[v][0]>dp[u][0]){
            dp[u][1]=dp[u][0];
            dp[u][0]=w+dp[v][0];
            pt[u]=v;
        }
        else if(w+dp[v][0]>dp[u][1])
            dp[u][1]=w+dp[v][0];
    }
}

void dfs2(int u,int fa){
    for(int i=head[u];i;i=edge[i].nex){
        int v=edge[i].v;
        LL w=edge[i].w;
        if(v==fa) continue;
        if(v!=pt[u])
            dp[v][2]=w+max(dp[u][0],dp[u][2]);
        else 
            dp[v][2]=w+max(dp[u][1],dp[u][2]);
        dfs2(v,u);
    }
}

void pushup(int v){
    tr[v].Max=max(tr[v<<1].Max,tr[v<<1|1].Max);
    tr[v].Min=min(tr[v<<1].Min,tr[v<<1|1].Min);
}

void build(int v,int l,int r){
    tr[v].l=l,tr[v].r=r;
    if(l==r){
        tr[v].Max=tr[v].Min=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(v<<1,l,mid);
    build(v<<1|1,mid+1,r);
    pushup(v);
}

void query(int v,int l,int r){
    if(l<=tr[v].l&&r>=tr[v].r){
        ans1=max(ans1,tr[v].Max);
        ans2=min(ans2,tr[v].Min);
        return;
    }
    int mid=(tr[v].l+tr[v].r)>>1;
    if(l<=mid) query(v<<1,l,r);
    if(r>mid) query(v<<1|1,l,r);
}

int main(){
    scanf("%d%lld",&n,&M);
    for(int i=2;i<=n;++i){
        int v;LL w;
        scanf("%d%lld",&v,&w);
        adde(i,v,w);
        adde(v,i,w);
    }
    dfs1(1,0);
    dfs2(1,0);
    for(int i=1;i<=n;++i)
        a[i]=max(dp[i][0],dp[i][2]);
    build(1,1,n);
    int j=1;
    for(int i=1;i<=n;++i){
        while(j<=n){
            ans1=0,ans2=inf;
            query(1,i,j);
            if(ans1-ans2>M) break;
            ++j;
        }
        ans=max(ans,j-i);
    }
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11423208.html