题解:P4751 【模板】动态 DP(加强版)

· · 题解

前置知识:P4719,动态开点线段树。

注意到出题人想卡树剖,但是我们可以卡常。

不知道有什么树形态可以开树剖(听说是每个点有根号个儿子,根号长的链的树)。

第一步:随机根

任意取一个大数(推荐 10^7 以上),模上 n 之后作为根,可以有效地避免树形态对程序造成常数上的影响。

第二步:少用 STL 模板

将 vector 存图改为链式前向星存图。

去掉 using namespace std;,并加上 #define max(x,y) ((x)>(y)?(x):(y))。

第三步:快读快写

推荐用 cin 和 cout 加上关闭输入输出同步流,改 endl 为 \n。

实测用 getchar 和 putchar 反而比较慢。

第四步:优化线段树

由动态开点线段树的思想,我们可以 对每一条链单独维护一颗线段树。

具体地,每一颗线段树的根用 链头的编号 来确定。每次开点时,取一个不重复的大于 n 的编号即可。可以证明,动态开点的点数不超过 5 n。

这样每次修改时,改的树深度一般不会顶满 \log n。查询的时候,直接取用树根上的值即可。

故修改是常数小的 O(\log n),查询 O(1),且代码合并的部分比常规的好写。

第五步:矩阵循环展开

将矩阵乘法的循环展开。这样可以提高缓存命中率,增加语句并发执行可能性。

改到这里,代码便可以以 2.90s 的时间通过第 10 个测试点。

代码

#include<bits/stdc++.h>

#define max(x,y) ((x)>(y)?(x):(y))

const int N=1000010,inf=0x3f3f3f3f;

struct mat
{
    int a[2][2];

    inline mat operator * (const mat& oth) const
    {
        mat res={{}};
        res.a[0][0]=max(a[0][0]+oth.a[0][0],a[0][1]+oth.a[1][0]);
        res.a[0][1]=max(a[0][0]+oth.a[0][1],a[0][1]+oth.a[1][1]);
        res.a[1][0]=max(a[1][0]+oth.a[0][0],a[1][1]+oth.a[1][0]);
        res.a[1][1]=max(a[1][0]+oth.a[0][1],a[1][1]+oth.a[1][1]);
        return res;
    }
}v[N],M;

int n,q,head[N],l=2,dfn[N],id,sz[N],maxson[N],top[N],bot_son[N],rnk[N];

int f[N][2],g[N][2],w[N],dth[N],fa[N];

struct side
{
    int last,to;
}e[N<<1];

inline void add_side(int x,int y)
{
    e[l].last=head[x];
    e[l].to=y;
    head[x]=l;
    ++l;
}

void dfs1(int cu,int pa)
{
    dth[cu]=dth[pa]+1;
    fa[cu]=pa;
    sz[cu]=1;
    int maxsz=0;
    for(int i=head[cu];i;i=e[i].last)
    {
        if(e[i].to==pa) continue;
        dfs1(e[i].to,cu);
        sz[cu]+=sz[e[i].to];
        if(sz[e[i].to]>maxsz)
        {
            maxsz=sz[e[i].to];
            maxson[cu]=e[i].to;
        }
    }
}

int dfs2(int cu,int pa,int tp)
{
    dfn[cu]=++id;
    rnk[id]=cu;
    top[cu]=tp;
    f[cu][0]=0,f[cu][1]=w[cu];
    v[cu].a[0][0]=0;
    v[cu].a[0][1]=0;
    v[cu].a[1][0]=w[cu];
    v[cu].a[1][1]=-inf;
    if(maxson[cu])
    {
        bot_son[cu]=dfs2(maxson[cu],cu,tp);
        f[cu][0]+=max(f[maxson[cu]][0],f[maxson[cu]][1]);
        f[cu][1]+=f[maxson[cu]][0];
    }
    else
    {
        bot_son[cu]=cu;
    }
    for(int i=head[cu];i;i=e[i].last)
    {
        if(e[i].to==pa||e[i].to==maxson[cu]) continue;
        dfs2(e[i].to,cu,e[i].to);
        f[cu][0]+=max(f[e[i].to][0],f[e[i].to][1]);
        v[cu].a[0][0]+=max(f[e[i].to][0],f[e[i].to][1]);
        f[cu][1]+=f[e[i].to][0];
        v[cu].a[1][0]+=f[e[i].to][0];
    }
    v[cu].a[0][1]=v[cu].a[0][0];
    g[cu][0]=v[cu].a[0][0];
    g[cu][1]=v[cu].a[1][0];
    return bot_son[cu];
}

struct Segment_tree
{
    int ls[N*6],rs[N*6],c;

    mat Val[N*6];   

    inline void update(int x)
    {
        Val[x]=Val[ls[x]]*Val[rs[x]];
    }

    void build(int l,int r,int& x,const int st)//st传dfn
    {
        if(x==0) x=++c;
        if(l==r)
        {
            Val[x]=v[rnk[st+l-1]];
            return ;
        }
        build(l,((l+r)>>1),ls[x],st);
        build(((l+r)>>1)+1,r,rs[x],st);
        update(x);
    }

    void change(int A,int l,int r,int& x)
    {
        if(l==r) 
        {
            Val[x]=M;
            return ;
        }
        if(A<=((l+r)>>1)) change(A,l,((l+r)>>1),ls[x]);
        else change(A,((l+r)>>1)+1,r,rs[x]);
        update(x);
    }

    inline mat query(int x)
    {
        return Val[x];
    }

    Segment_tree()
    {
        c=N+1;
    }
}S;

int last_ans,root;

int main()
{
    std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);
    std::cin>>n>>q;
    root=30624700%n+1;
    for(int i=1;i<=n;++i)
    {
        std::cin>>w[i];
    }
    for(int i=1,x,y;i<=n-1;++i)
    {
        std::cin>>x>>y;
        add_side(x,y);
        add_side(y,x);
    }
    dfs1(root,0);
    dfs2(root,0,root);
    for(int i=1;i<=n;++i)
    {
        if(top[i]==i)
        {
            S.build(1,dfn[bot_son[i]]-dfn[i]+1,i,dfn[i]);
        }
    }
    int cu,v;
    while(q--)
    {
        std::cin>>cu>>v;
        cu^=last_ans;
        g[cu][1]+=v-w[cu];
        w[cu]=v;
        while(cu)
        {
            mat last=S.query(top[cu]);
            M=(mat){g[cu][0],g[cu][0],g[cu][1],-inf};
            S.change(dfn[cu]-dfn[top[cu]]+1,1,dfn[bot_son[cu]]-dfn[top[cu]]+1,top[cu]);
            mat now=S.query(top[cu]);
            if(fa[top[cu]]==0) break;
            g[fa[top[cu]]][0]+=max(now.a[0][1],max(now.a[0][0],now.a[1][0]))-max(last.a[0][1],max(last.a[0][0],last.a[1][0]));
            g[fa[top[cu]]][1]+=max(now.a[0][0],now.a[0][1])-max(last.a[0][0],last.a[0][1]);
            cu=fa[top[cu]];
        }
        mat Ans=S.query(root);
        last_ans=max(Ans.a[0][0],Ans.a[1][0]);
        std::cout<<last_ans<<'\n';
    }
    return 0;
}