bzoj 4539 [Hnoi2016]树 主席树 倍增lca

来源:互联网 发布:新版手机知乎怎么提问 编辑:程序博客网 时间:2024/05/02 06:45

把插入的树看成一个点,这个点到父亲的距离是插入树的根到父亲树的根的距离。

需要建一个对于模板树的dfs序的主席树,用于找一个子树中第k大点。

求两个点的距离分两个点在一棵内层树中,一个点的内层树对应的点是另一个点内层树对应点的父亲,两个点内层树的lca是另一棵内层树考虑。

#include <bits/stdc++.h>using namespace std;#define ll long long#define N 110000#define M 11000000#define PA pair<int,ll> int n,m,Q,tot,num,cnt;int head[N],nex[N<<1],to[N<<1];int st[N],ed[N],pos[N],bel[N],root[N];int fap[N],mep[N];ll fis[N];void add(int x,int y){    tot++;    nex[tot]=head[x];head[x]=tot;    to[tot]=y;}struct Dis_tree{    int fa[N][21],deep[N],num;    ll dis[N][21];    void init()    {        for(int i=1;i<=20;i++)            for(int j=1;j<=num;j++)            {                fa[j][i]=fa[fa[j][i-1]][i-1];                dis[j][i]=dis[fa[j][i-1]][i-1]+dis[j][i-1];            }    }    PA lca(int x,int y)    {        ll v=0;        if(deep[x]<deep[y])swap(x,y);        for(int i=20;i>=0;i--)            if(deep[fa[x][i]]>=deep[y])                v+=dis[x][i],x=fa[x][i];        if(x==y)return make_pair(x,v);        for(int i=20;i>=0;i--)            if(fa[x][i]!=fa[y][i])            {                v+=dis[x][i],v+=dis[y][i];                x=fa[x][i],y=fa[y][i];            }        v+=dis[x][0];v+=dis[y][0];        return make_pair(fa[x][0],v);    }    PA find(int x,int y)    {        ll v=0;        for(int i=20;i>=0;i--)            if(deep[fa[x][i]]>deep[y])                v+=dis[x][i],x=fa[x][i];        return make_pair(x,v);    }}a1,a2;struct Seg_tree{    int ch[M][2],num[M],cnt;    void insert(int l,int r,int &now,int pre,int pos)    {        num[now=++cnt]=num[pre]+1;        if(l==r)return;        ch[now][0]=ch[pre][0];        ch[now][1]=ch[pre][1];        int mid=(l+r)>>1;        if(mid>=pos)insert(l,mid,ch[now][0],ch[pre][0],pos);        else insert(mid+1,r,ch[now][1],ch[pre][1],pos);    }    int query(int l,int r,int now,int pre,int K)    {        if(l==r)return l;        int mid=(l+r)>>1;        int sum=num[ch[now][0]]-num[ch[pre][0]];        if(sum>=K)return query(l,mid,ch[now][0],ch[pre][0],K);        else return query(mid+1,r,ch[now][1],ch[pre][1],K-sum);         }}tr;void dfs(int x,int y){    pos[x]=st[x]=++cnt;    a1.deep[x]=a1.deep[y]+1;    bel[cnt]=x;    a1.fa[x][0]=y;a1.dis[x][0]=1;    for(int i=head[x];i;i=nex[i])        if(to[i]!=y)dfs(to[i],x);    ed[x]=cnt;}int main(){    //freopen("tt.in","r",stdin);    scanf("%d%d%d",&n,&m,&Q);    for(int i=1,x,y;i<n;i++)    {        scanf("%d%d",&x,&y);        add(x,y);add(y,x);    }    a1.num=n;dfs(1,0);    a1.init();    for(int i=1;i<=n;i++)        tr.insert(1,n,root[i],root[i-1],bel[i]);    fis[cnt=1]=n;mep[1]=1;    a2.deep[1]=1;    ll K;    for(int x,y,fa;m--;)    {        scanf("%d%lld",&x,&K);        a2.fa[cnt+1][0]=fa=lower_bound(fis+1,fis+cnt+1,K)-fis;        mep[++cnt]=x;y=mep[fa];        a2.deep[cnt]=a2.deep[fa]+1;        fap[cnt]=tr.query(1,n,root[ed[y]],root[st[y]-1],K-fis[fa-1]);        a2.dis[cnt][0]=a1.lca(fap[cnt],y).second+1;        fis[cnt]=fis[cnt-1]+ed[mep[cnt]]-st[mep[cnt]]+1;    }    a2.num=cnt;a2.init();    ll K1,K2;    for(int x,y,xp,yp,x1,y1;Q--;)    {        ll ans=0;        scanf("%lld%lld",&K1,&K2);        x=lower_bound(fis+1,fis+cnt+1,K1)-fis;        y=lower_bound(fis+1,fis+cnt+1,K2)-fis;        if(a2.deep[x]<a2.deep[y])            {swap(x,y);swap(K1,K2);}        xp=mep[x];yp=mep[y];        x1=tr.query(1,n,root[ed[xp]],root[st[xp]-1],K1-fis[x-1]);        y1=tr.query(1,n,root[ed[yp]],root[st[yp]-1],K2-fis[y-1]);        if(x==y)        {            printf("%lld\n",a1.lca(x1,y1).second);            continue;        }        PA t=a2.lca(x,y);        if(t.first==y)        {            ans=a1.lca(x1,xp).second;            PA t1=a2.find(x,y);            PA t2=a1.lca(fap[t1.first],y1);            ans+=t1.second+t2.second+1;            printf("%lld\n",ans);            continue;        }        ans=a1.lca(x1,xp).second+a1.lca(y1,yp).second;        PA t1=a2.find(x,t.first);        PA t2=a2.find(y,t.first);        ans+=t1.second+t2.second+2;        ans+=a1.lca(fap[t1.first],fap[t2.first]).second;        printf("%lld\n",ans);    }    return 0;}
0 0