51nod 1389 跳跳树 倍增

题意

小a与小b在树上玩一个游戏,每一轮游戏小a与小b在树上选两个点,然后小a朝着小b所在的方向移动。神奇的是,这棵树上每条边都有一个长度的,每个点上都有一棵非常高的松树。小a可以从高处不耗费任何体力跳往相邻的低处,也可以耗费一点体力,走不超过k的长度的路径走到某个点然后跳到松树顶端(不能停在路中央)。现在他们进行了Q次游戏,每一次小a所在的位置为xi的顶端,小b所在的位置为yi的顶端。小a想知道最少耗费多少体力能到达小b所在的位置。
1<=n,Q<=300000

分析

调死我了。。。

把询问沿着lca拆成两条链分别处理。
预处理bz[i,j]表示i花费2^j往上跳最多能跳到哪里。
然后先从x往上跳,跳到一个位置满足再跳一步就能到达或跨过lca,然后再讨论一下各种情况,再从y开始往上跳即可。
细节超多。。。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long LL;

const int N=300005;

int n,k,cnt,last[N],top1[N],top2[N],bz1[N][20],bz2[N][20],fa[N][20],dep[N],h[N],bin[20],stack[20];
LL dis[N];
struct edge{int to,next,w;}e[N*2];

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void pri(int x)
{
    if (!x) {puts("0");return;}
    int top=0;
    while (x) stack[++top]=x%10,x/=10;
    while (top) putchar(stack[top]+'0'),top--;
    puts("");
}

void addedge(int u,int v,int w)
{
    e[++cnt].to=v;e[cnt].w=w;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].to=u;e[cnt].w=w;e[cnt].next=last[v];last[v]=cnt;
}

void dfs(int x)
{
    dep[x]=dep[fa[x][0]]+1;
    for (int i=1;i<=18;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
    top1[x]=(fa[x][0]&&h[fa[x][0]]<h[x])?top1[fa[x][0]]:x;
    top2[x]=(fa[x][0]&&h[fa[x][0]]>h[x])?top2[fa[x][0]]:x;
    if (top1[x]!=x) bz1[x][0]=bz1[top1[x]][0];
    else
    {
        int y=x;
        for (int i=18;i>=0;i--) if (dis[x]-dis[fa[y][i]]<=k) y=fa[y][i];
        bz1[x][0]=top1[y];
    }
    if (top2[x]!=x) bz2[x][0]=bz2[top2[x]][0];
    else
    {
        int y=x;
        for (int i=18;i>=0;i--) if (dis[x]-dis[fa[y][i]]<=k) y=fa[y][i];
        bz2[x][0]=top2[y];
    }
    for (int i=1;i<=18;i++)
    {
        bz1[x][i]=bz1[bz1[x][i-1]][i-1];
        bz2[x][i]=bz2[bz2[x][i-1]][i-1];
    }
    for (int i=last[x];i;i=e[i].next)
    {
        if (e[i].to==fa[x][0]) continue;
        fa[e[i].to][0]=x;
        dis[e[i].to]=dis[x]+e[i].w;
        dfs(e[i].to);
    }
}

int get_lca(int x,int y)
{
    if (dep[x]<dep[y]) swap(x,y);
    for (int i=18;i>=0;i--) if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
    if (x==y) return x;
    for (int i=18;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}

int main()
{
    bin[0]=1;
    for (int i=1;i<=18;i++) bin[i]=bin[i-1]*2;
    n=read();k=read();
    for (int i=1;i<n;i++)
    {
        int x=read(),y=read(),z=read();
        addedge(x,y,z);
    }
    for (int i=1;i<=n;i++) h[i]=read();
    dfs(1);
    int q=read();
    while (q--)
    {
        int x=read(),y=read(),lca=get_lca(x,y);
        x=(dep[top1[x]]>dep[lca])?top1[x]:lca;y=(dep[top2[y]]>dep[lca])?top2[y]:lca;
        if (lca==x)
        {
            int now=y,ans=0;
            for (int i=18;i>=0;i--) if (dep[bz2[now][i]]>dep[x]) ans+=bin[i],now=bz2[now][i];
            if (top2[x]==top2[y]) ans--;
            pri(ans+1);
        }
        else if (lca==y)
        {
            int now=x,ans=0;
            for (int i=18;i>=0;i--) if (dep[bz1[now][i]]>dep[y]) ans+=bin[i],now=bz1[now][i];
            if (top1[x]==top1[y]) ans--;
            pri(ans+1);
        }
        else
        {
            int now=x,ans=0;
            for (int i=18;i>=0;i--) if (dep[bz1[now][i]]>dep[lca]) ans+=bin[i],now=bz1[now][i];
            int ret=0,sta=y;
            if (dis[now]-dis[lca]<=k) ret=k-dis[now]+dis[lca];
            else sta=lca;
            for (int i=18;i>=0;i--) if (dis[fa[sta][i]]-dis[lca]>ret) sta=fa[sta][i];
            if (sta!=lca) sta=fa[sta][0];
            int ypx=y;
            for (int i=18;i>=0;i--) if (dep[bz2[ypx][i]]>dep[sta]) ans+=bin[i],ypx=bz2[ypx][i];
            if (dis[y]-dis[lca]<=ret) ans--;
            pri(ans+2);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值