P7671 解题报告

· · 题解

前言

重剖题单里的,闲暇的时候刷到的推式子好题,挺好玩的。

做着题前推荐先做下 P9808 [POI2022~2023R1] zbo 和 P4211 [LNOI2014] LCA。

先练习了解下经典式子的拆法,再来试试这题吧。

有个傻逼式子抄错了半天没推出来我不说是谁。

题意分析

首先说下题意中我理解错的地方,能量石在生产之后是不会消耗的,能一直解救动物。

所以这种题写前最好看下形式化题意对下题意理解是否有问题。

其他部分暂且不考虑,先看题目中又臭又长的那串式子:

\sum\limits_{i \in x\rightarrow y}\!{a_i}\times\dfrac{\operatorname{dis}(i,y)(\operatorname{dis}(i,y)+1)}{2}

在这里,我们发现了一个很熟悉的部分,\operatorname{dis}(i,y)。

像这样的树上两点距离,肯定是要无脑的给他拆成 dep_i+dep_y-2\times dep_{\operatorname{LCA}(i,y)}。

然后把这部分拆开看看,再把 \dfrac{1}{2} 提到前面去,就得到了:

\dfrac{1}{2}\sum\limits_{i \in x\rightarrow y}\!{a_i}\times(dep_i+dep_y-2\times dep_{\operatorname{LCA}(i,y)})(dep_i+dep_y-2dep_{\operatorname{LCA}(i,y)}+1)

欸,这式子怎么越化越复杂了,难道要全部拆开吗?

显然不是的,我们发现这里的 \operatorname{dis}(i,y) 有一个与其他推式子题不一样的特性,就是其中的 y 点是已经给定的。

又因为 i 是 x\rightarrow y 路径上的点,所以当 i 是 \operatorname{LCA}(x,y)\rightarrow x 的那一段时,\operatorname{LCA}(i,y) 就是 \operatorname{LCA}(x,y)。

同理的考虑当 i 是 \operatorname{LCA}(x,y)\rightarrow y 的那一段时,\operatorname{LCA}(i,y) 就是 i。

这下式子就有着美妙的性质了,再考虑下按照这两个部分给他化开。

下面式子中用 lca 表示 \operatorname{LCA}(x,y)。

  1. 当 i\in lca\rightarrow x 时,式子即为:
\dfrac{1}{2}\sum\limits_{i\in lca\rightarrow x}\!{a_i}\times(dep_i+dep_y-2dep_{lca})(dep_i+dep_y-2dep_{lca}+1)

观察到 dep_y-2\times dep_{lca} 是个定值,直接用 s_1 来表示他,就得到了:

\dfrac{1}{2}\sum\limits_{i\in lca\rightarrow x}\!{a_i}\times(dep_i+s_1)(dep_i+s_1+1)

再接一步大力展开:

\dfrac{1}{2}\sum\limits_{i\in lca\rightarrow x}\!{a_i}\times((dep_i+s_1)^2+(dep_i+s_1)) \dfrac{1}{2}\sum\limits_{i\in lca\rightarrow x}\!{a_i}\times(dep_i^2+2s_1dep_i+s_1^2+dep_i+s_1)

最后把求和也给展开,就得到了:

\dfrac{1}{2}\left[\sum\limits_{i\in lca\rightarrow x}{a_idep_i^2}+\sum\limits_{i\in lca\rightarrow x}{a_idep_i\left(2s_1+1\right)}+\sum\limits_{i\in lca\rightarrow x}{a_i\left(s_1^2+s_1\right)}\right]
  1. 当 i\in lca\rightarrow y 时,式子即为:
\dfrac{1}{2}\sum\limits_{i\in lca\rightarrow y}\!{a_i}\times(dep_i+dep_y-2dep_i)(dep_i+dep_y-2dep_i+1) \dfrac{1}{2}\sum\limits_{i\in lca\rightarrow y}\!{a_i}\times(dep_y-dep_i)(dep_y-dep_i+1)

发现其中的 dep_y 为不变量,用 s_2 代入展开得:

\dfrac{1}{2}\sum\limits_{i\in lca\rightarrow y}\!{a_i}\times(s_2^2-2dep_is_2+dep_i^2+s_2-dep_i)

最后再展开下求和就得到了:

\dfrac{1}{2}\left[\sum\limits_{i\in lca\rightarrow y}{a_idep_i^2}-\sum\limits_{i\in lca\rightarrow y}{a_idep_i\left(2s_2+1\right)}+\sum\limits_{i\in lca\rightarrow y}{a_i\left(s_2^2+s_2\right)}\right]

至此,最艰难的一步就完成了。

然后再去看题面中其他的部分,因为有版本回溯,所以直接大力上一颗主席树。

对于链加,直接标记永久化即可。

主席树上维护三个值,分别是正常的点权和,乘上 dep 的点权合和乘上 dep^2 的点权和。

接着大力维护即可,详情见代码了,有关变量部分有些许解释。

代码

#include <bits/stdc++.h>
#define ls (t[p].l)
#define rs (t[p].r)
#define mid ((l+r)>>1)
#define int long long
using namespace std;
const int N=100010,INF=0x3f3f3f3f3f3f3f3f,mod=20160501;
struct edge
{int v,nxt;}e[N<<1];
struct tree
{
    int s,ss,sss,tag,l,r,v[2];
    //s正常点权,ss乘上一个dep,sss乘上两个dep
    //l,r代表的是左右儿子,不写ls,rs是因为我上面宏定义会冲突。
    //tag是修改的值的大小,v[0/1]代表左/右儿子是否需要修改
    tree(){s=ss=sss=l=r=tag=v[0]=v[1]=0;}
}t[N<<6];//经典主席树空间
int n,m,cnt,tot,root,lst,idx;
//cnt是dfn序的,tot是链式前向星的,root是主席树的根,lst是上次答案,idx是主席树版本号
int head[N],dfn[N],id[N],top[N];
int fa[N],si[N],son[N],dep[N];//树剖,不解释
int a[N],rt[N],s[N],ss[N];//a是点权,rt是根,s是乘上dep的点权,ss是乘上两个dep的点权
static char buf[1000000],*paa=buf,*pd=buf;
//#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline int read(void){
    int x(0),t(1);char fc(getchar());
    while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
    while(isdigit(fc)) x=(x<<1)+(x<<3)+(fc^48),fc=getchar();
    if(fc=='.'){fc=getchar();while(isdigit(fc)) x=(x<<1)+(x<<3)+(fc^48),fc=getchar();}
    return x*t;
}
inline void print(int x)
{
    if(x<0) putchar('-'),x=-x;
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
inline void add(int u,int v){e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;}
inline void dfs1(int u,int ff)
{
    fa[u]=ff,dep[u]=dep[ff]+1,si[u]=1;
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v==ff) continue;
        dfs1(v,u);si[u]+=si[v];
        if(si[v]>si[son[u]]) son[u]=v;
    }
}
inline void dfs2(int u,int topf)
{
    top[u]=topf,dfn[u]=++cnt,id[cnt]=u;
    if(son[u]) dfs2(son[u],topf);
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}
inline tree operator+(tree a,tree b)
{
    tree c;
    c.s=(a.s+b.s)%mod;
    c.ss=(a.ss+b.ss)%mod;
    c.sss=(a.sss+b.sss)%mod;
    return c;
}
inline int calcs(int l,int r){return s[r]-s[l-1]+mod;}
inline int calcss(int l,int r){return ss[r]-ss[l-1]+mod;}
inline void build(int &p,int l,int r)
{
    p=++cnt;
    if(l==r)
    {
        int x=id[l];
        t[p].s=a[x]%mod;
        t[p].ss=a[x]*dep[x]%mod;
        t[p].sss=a[x]*dep[x]*dep[x]%mod;
        return;
    }
    build(ls,l,mid);build(rs,mid+1,r);
    t[p].s=(t[ls].s+t[rs].s)%mod;
    t[p].ss=(t[ls].ss+t[rs].ss)%mod;
    t[p].sss=(t[ls].sss+t[rs].sss)%mod;
}
inline void pushup(tree &p,int l,int r,int k)
{
    p.s=(p.s+k*(r-l+1)%mod)%mod;
    p.ss=(p.ss+k*calcs(l,r)%mod)%mod;
    p.sss=(p.sss+k*calcss(l,r)%mod)%mod;
}
inline int copy(int p){t[++cnt]=t[p];return cnt;}
inline void modify(int p,int l,int r,int st,int ed,int k)
{
    pushup(t[p],max(l,st),min(r,ed),k);
    if(st<=l&&r<=ed) return void(t[p].tag=(t[p].tag+k)%mod);
    if(st<=mid)
    {
        if(t[p].v[0]) ls=copy(ls),t[ls].v[0]=1,t[ls].v[1]=1,t[p].v[0]=0;
        modify(ls,l,mid,st,ed,k);
    }
    if(mid<ed)
    {
        if(t[p].v[1]) rs=copy(rs),t[rs].v[0]=1,t[rs].v[1]=1,t[p].v[1]=0;
        modify(rs,mid+1,r,st,ed,k);
    }
}
inline tree query(int p,int l,int r,int st,int ed)
{
    if(st<=l&&r<=ed) return t[p];tree res;
    pushup(res,max(l,st),min(r,ed),t[p].tag);
    if(st<=mid) res=res+query(ls,l,mid,st,ed);
    if(mid<ed) res=res+query(rs,mid+1,r,st,ed);
    return res;
}
inline int LCA(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    return x;
}
inline void modify(int x,int y,int k)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        modify(rt[root],1,n,dfn[top[x]],dfn[x],k);
        x=fa[top[x]];
    }
    if(dfn[x]>dfn[y]) swap(x,y);
    modify(rt[root],1,n,dfn[x],dfn[y],k);
}
inline int query(int c,int op,int l,int r)
{
    tree res=query(rt[root],1,n,l,r);
    return (res.sss+(op*res.ss*(c*2+1))%mod+res.s*((c*c)%mod+c)+mod)%mod;
}
inline int query(int x,int y)
{
    int lca=LCA(x,y),s1=(dep[y]-dep[lca]*2+mod)%mod,s2=dep[y],res=0;
    while(top[x]!=top[y])
        if(dep[top[x]]<=dep[top[y]]) res=(res+query(s2,-1,dfn[top[y]],dfn[y]))%mod,y=fa[top[y]];
        else res=(res+query(s1,1,dfn[top[x]],dfn[x]))%mod,x=fa[top[x]];
    if(dep[x]>=dep[y]) res=(res+query(s1,1,dfn[y],dfn[x]))%mod;
    else res=(res+query(s2,-1,dfn[x],dfn[y]))%mod;
    return res*((mod+1)/2)%mod;
}
signed main()
{
    n=read(),m=read();
    for(int i=1,u,v;i<n;i++) u=read(),v=read(),add(u,v),add(v,u);
    for(int i=1;i<=n;i++) a[i]=read();dfs1(1,0),dfs2(1,1);
    for(int i=1;i<=n;i++) s[i]=(s[i-1]+dep[id[i]])%mod,ss[i]=(ss[i-1]+dep[id[i]]*dep[id[i]])%mod;
    build(rt[0],1,n);
    for(int i=1,op,x,y,k;i<=m;i++)
    {
        op=read(),x=read()^lst;
        if(op==1)
        {
            y=read()^lst;k=read();rt[++idx]=++cnt;
            t[rt[idx]]=t[rt[root]];root=idx;
            t[rt[root]].v[0]=t[rt[root]].v[1]=1;
            modify(x,y,k);
        }
        else if(op==2) y=read()^lst,print((lst=query(x,y))),puts("");
        else root=x;
    }
    return 0;
}