题解:P5327 [ZJOI2019] 语言

· · 题解

P5327 [ZJOI2019] 语言 - 洛谷

看了一下,发现没有树链剖分、线段树上二分、树链(树上路径)求并的 \mathcal{O}(n\log^2 n) 的做法。

先说一下大体思路。

简化点对 (u,v) 的性质 u<v,只限制 u\ne v,最后将答案除以 2 即可。

对于点 u,能与 u 形成合法点对 (u,v) 的 v,是经过 u 的路径的并的点(除 u 以外)。

由于这里的树上路径求并只关心点的个数,所以有经典做法:把所有路径的端点按时间戳排序,所有路径两端的深度之和减去相邻端点的 lca 的深度之和再减去所有端点的 lca 的父亲深度(再减去所有端点的 lca 的深度减 1)。

为什么?

可以从根据时间戳从小往大加点进行考虑。考虑这些点形成的树上连通块的大小。为了方便,钦定最开始有 1 号点。

假设当前加入 u 号点,之前加入的点构成点集 S,S 的连通子树是 T。显然,为了使 u 和 T 连通,只需要找到 T 中离 u 最近的点连起来就好了,这“最近的点”正是上一个加入的点 v 与 u 的 lca。所以 u 的贡献是 dep(u)−dep(lca(u, v))。别忘了钦定的 1 号点,最后还要减去所有点的 lca 的 dep-1 。

那么现在每个点维护一个路径的并(具体维护什么后面会说)。

对于一条路径 s\leadsto t,把路径上所有点都并上路径 s\leadsto t。答案为最后所有点的并的大小之和,再除以 2。

然后考虑具体的实现。

先树链剖分,开一棵线段树,每个节点开一个 vector,维护要取并的路径。对于路径 s\leadsto t,在线段树上把它加入其经过的点的 vector 里。

然后在这棵线段树上做线段树分治,全局维护一个节点集合 nd、集合内所有点的最近公共祖先 d 以及并的大小 res。

插入就是看前驱后继,给 res 处理一下相邻点 lca 的计算,然后将 d 更新。

撤回可以在每个节点记录 d,res,将 nd 内的点删除。

到叶子节点的 res-1 就是这个点所能到达的点的个数。

时间复杂度 \mathcal{O}(n\log^2 n)。

#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
//#include<ext/pb_ds/priority_queue.hpp>
#define umap unordered_map
#define mset multiset
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(),(x).end()
#define tmax(a,b) (a)=max((a),(b))
#define tmin(a,b) (a)=min((a),(b))
#define dbg(x) cout<<"line "<<__LINE__<<" : "<<#x<<"="<<x<<"\n"
#define cout(x) cout<<fixed<<setprecision(x)

using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;typedef pair<int,int> PII;
typedef vector<int> vi;typedef vector<PII> vp;
typedef array<int,3> a3;typedef vector<a3> va3;

const int N=1e5+5,Logn=17+5;
int n,m,f[Logn][N],dep[N],top[N],siz[N],fa[N],son[N],dfn[N],rnk[N],times,res,lca,ores[N<<2],olca[N<<2];
LL ans;
vi g[N];
vp tr[N<<2];
mset<PII>nd;

int cmpv(int u,int v){
    return dfn[u]<dfn[v]?u:v;
}
void dfs(int u,int pa){
    dep[u]=dep[pa]+1,siz[u]=1,fa[u]=pa;
    for(auto v:g[u]){
        if(v==pa) continue;
        dfs(v,u);
        siz[u]+=siz[v];
        if(siz[son[u]]<siz[v]){
            son[u]=v;
        }
    }
}
void dfs(int u,int pa,int t){
    dfn[u]=++times,rnk[times]=u,top[u]=t;
    f[0][dfn[u]]=pa;
    if(son[u]) dfs(son[u],u,t);
    for(auto v:g[u]){
        if(v==pa||v==son[u]) continue;
        dfs(v,u,v);
    }
}
int LCA(int u,int v){
    if(!u) return v;
    if(!v) return u;
    if(u==v) return u;
    u=dfn[u],v=dfn[v];
    if(u>v) swap(u,v);
    u++;
    int s=__lg(v-u+1);
    return cmpv(f[s][u],f[s][v-(1<<s)+1]);
}
#define lc (rt<<1)
#define rc (rt<<1|1)
void update(int rt,int l,int r,int L,int R,PII val){
    if(L<=l&&r<=R){
        tr[rt].pb(val);
        return;
    }
    int mid=l+r>>1;
    if(R<=mid){
        update(lc,l,mid,L,R,val);
    }else if(L>mid){
        update(rc,mid+1,r,L,R,val);
    }else{
        update(lc,l,mid,L,mid,val);
        update(rc,mid+1,r,mid+1,R,val);
    }
} 
void update(int u,int v,PII val){
    while(top[u]!=top[v]){
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        update(1,1,n,dfn[top[u]],dfn[u],val);
        u=fa[top[u]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    update(1,1,n,dfn[u],dfn[v],val);
}
void getin(int rt,int l,int r){
    ores[rt]=res,olca[rt]=lca;
//  cout<<l<<" "<<r<<":"<<res<<" "<<lca<<" "<<nd.size()<<"\n";
    res+=dep[lca]-1;
    for(auto tmp:tr[rt]){
//      cout<<"+++"<<tmp.fi<<" "<<tmp.se<<"\n";
        for(auto u:{tmp.fi,tmp.se}){
            res+=dep[u],lca=LCA(lca,u);
            nd.insert({dfn[u],u});
            auto it=nd.find({dfn[u],u});
            int x=0,y=0;
            if(it!=nd.begin()){
                it--;
                x=(*it).se;
                it++;
            }
            it++;
            if(it!=nd.end()){
                y=(*it).se;
            }
            if(x&&y){
                res+=dep[LCA(x,y)];
            }
            if(x){
                res-=dep[LCA(x,u)];
            }
            if(y){
                res-=dep[LCA(y,u)];
            }
        }
//      cout<<res<<" "<<lca<<"\n";
    }
    res-=dep[lca]-1;
//  cout<<"+end"<<res<<" "<<lca<<"\n";
}
void getout(int rt,int l,int r){
    res=ores[rt],lca=olca[rt];
    for(auto tmp:tr[rt]){
//      cout<<"---"<<tmp.fi<<" "<<tmp.se<<"\n";
        for(auto u:{tmp.fi,tmp.se}){
            nd.erase(nd.find({dfn[u],u}));
        }
    }
//  cout<<"-end"<<res<<" "<<lca<<"\n";
}
void work(int rt,int l,int r){
    getin(rt,l,r);
    if(l==r){
//      cout<<"&&&&&&&&&&&&&&&left"<<rnk[l]<<" "<<res<<"\n";
        ans+=max(res-1,0);
        getout(rt,l,r);
        return;
    }
    int mid=l+r>>1;
    work(lc,l,mid);
    work(rc,mid+1,r);
    getout(rt,l,r);
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1,u,v;i<n;i++){
        cin>>u>>v;
        g[u].pb(v),g[v].pb(u);
    }
    dfs(1,0),dfs(1,0,1);
    dfn[0]=++times,dep[0]=1;
    for(int i=1;i<=__lg(n);i++){
        for(int j=1;j<=n-(1<<i)+1;j++){
            f[i][j]=cmpv(f[i-1][j],f[i-1][j+(1<<i-1)]);
        }
    }
    for(int s,t;m--;){
        cin>>s>>t;
        update(s,t,{s,t});
    }
    work(1,1,n);
    cout<<ans/2;
    return 0;
}
/*

*/