本文主要是介绍树上操作 HYSBZ - 4034,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://www.lydsy.com/JudgeOnline/problem.php?id=4034
在树链剖分后形成的dfs序 和普通的dfs序性质一样 一颗子树在dfs序列中仍然是连续的 因为树剖之后虽然先走重链 但还是要走完当前子树后才会递归返回 然后进入其他子树
#include <bits/stdc++.h>
using namespace std;
#define ll long longstruct node1
{int v;int next;
};struct node2
{int l;int r;ll val;ll laz;
};node1 edge[200010];
node2 tree[400010];
int pre[100010],first[100010],fa[100010],deep[100010],sum[100010],son[100010],top[100010],mp1[100010],mp2[100010];
int n,q,num;void addedge(int u,int v)
{edge[num].v=v;edge[num].next=first[u];first[u]=num++;
}void dfsI(int cur)
{int i,v;sum[cur]=1,son[cur]=-1;for(i=first[cur];i!=-1;i=edge[i].next){v=edge[i].v;if(v!=fa[cur]){fa[v]=cur,deep[v]=deep[cur]+1;dfsI(v);sum[cur]+=sum[v];if(son[cur]==-1||sum[son[cur]]<sum[v]){son[cur]=v;}}}
}void dfsII(int cur,int tp)
{int i,v;num++;top[cur]=tp,mp1[cur]=num,mp2[num]=cur;if(son[cur]==-1) return;dfsII(son[cur],tp);for(i=first[cur];i!=-1;i=edge[i].next){v=edge[i].v;if(v!=fa[cur]&&v!=son[cur]){dfsII(v,v);}}
}void pushup(int cur)
{tree[cur].val=tree[2*cur].val+tree[2*cur+1].val;return;
}void pushdown(int cur)
{if(tree[cur].laz!=0){tree[2*cur].val+=(tree[2*cur].r-tree[2*cur].l+1)*tree[cur].laz;tree[2*cur].laz+=tree[cur].laz;tree[2*cur+1].val+=(tree[2*cur+1].r-tree[2*cur+1].l+1)*tree[cur].laz;tree[2*cur+1].laz+=tree[cur].laz;tree[cur].laz=0;}return;
}void build(int l,int r,int cur)
{int m;tree[cur].l=l;tree[cur].r=r;tree[cur].laz=0;if(l==r){tree[cur].val=pre[mp2[l]];return;}m=(l+r)/2;build(l,m,2*cur);build(m+1,r,2*cur+1);pushup(cur);
}void updateI(int tar,ll val,int cur)
{if(tree[cur].l==tree[cur].r){tree[cur].val+=val;return;}pushdown(cur);if(tar<=tree[2*cur].r) updateI(tar,val,2*cur);else updateI(tar,val,2*cur+1);pushup(cur);
}void updateII(int pl,int pr,ll val,int cur)
{if(pl<=tree[cur].l&&tree[cur].r<=pr){tree[cur].val+=(tree[cur].r-tree[cur].l+1)*val;tree[cur].laz+=val;return;}pushdown(cur);if(pl<=tree[2*cur].r) updateII(pl,pr,val,2*cur);if(pr>=tree[2*cur+1].l) updateII(pl,pr,val,2*cur+1);pushup(cur);return;
}ll query(int pl,int pr,int cur)
{ll res;if(pl<=tree[cur].l&&tree[cur].r<=pr){return tree[cur].val;}pushdown(cur);res=0;if(pl<=tree[2*cur].r) res+=query(pl,pr,2*cur);if(pr>=tree[2*cur+1].l) res+=query(pl,pr,2*cur+1);return res;
}ll solve(int u,int v)
{ll res;res=0;while(top[u]!=top[v]){if(deep[top[u]]<deep[top[v]]) swap(u,v);res+=query(mp1[top[u]],mp1[u],1);u=fa[top[u]];}if(deep[u]<deep[v]) swap(u,v);res+=query(mp1[v],mp1[u],1);return res;
}int main()
{ll w;int i,u,v,op;while(scanf("%d%d",&n,&q)!=EOF){for(i=1;i<=n;i++){scanf("%lld",&pre[i]);}memset(first,-1,sizeof(first));num=0;for(i=1;i<=n-1;i++){scanf("%d%d",&u,&v);addedge(u,v);addedge(v,u);}fa[1]=-1,deep[1]=1;dfsI(1);num=0;dfsII(1,1);build(1,n,1);while(q--){scanf("%d",&op);if(op==1){scanf("%d%lld",&u,&w);updateI(mp1[u],w,1);}else if(op==2){scanf("%d%lld",&u,&w);updateII(mp1[u],mp1[u]+sum[u]-1,w,1);}else{scanf("%d",&u);printf("%lld\n",solve(1,u));}}}return 0;
}
这篇关于树上操作 HYSBZ - 4034的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!