Saint_yu @ 2024-01-02 14:03:51
#include <bits/stdc++.h>
#define MAXN 100005
#define int long long
using namespace std;
int mod,n,m,res,r,tot,cnt;
int val[MAXN],og_val[MAXN],head[MAXN],depth[MAXN],fa[MAXN];
int num[MAXN],top[MAXN],son[MAXN],size[MAXN];
struct node {
int lazy,ls,rs,sum;
} tree[MAXN*4];
struct Edge{
int to,nxt;
}edge[MAXN*2];
void add_edge(int u,int v){
edge[++tot].to=v;
edge[tot].nxt=head[u];
head[u]=tot;
}
void pushdown(int x) {
tree[x<<1].lazy+=tree[x].lazy;
tree[x<<1|1].lazy+=tree[x].lazy;
tree[x<<1].sum+=tree[x].lazy*(tree[x<<1].rs-tree[x<<1].ls+1);
tree[x<<1|1].sum+=tree[x].lazy*(tree[x<<1|1].rs-tree[x<<1|1].ls+1);
tree[x<<1].sum%=mod;
tree[x<<1|1].sum%=mod;
tree[x].lazy=0;
}
void build_tree(int x,int l,int r) {
if(l==r) {
tree[x].sum=val[l];
if(tree[x].sum>mod)tree[x].sum%=mod;
return;
}
int mid=(tree[x].ls+tree[x].rs)>>1;
build_tree(x>>1,l,mid);
build_tree(x>>1|1,mid+1,r);
tree[x].sum=(tree[x<<1].sum+tree[x<<1|1].sum)%mod;
}
void query(int x,int l,int r,int L,int R) {
if(L<=l&&r<=R) {
res+=tree[x].sum;
res%=mod;
return;
}
int mid=(tree[x].ls+tree[x].rs)>>1;
if(tree[x].lazy)pushdown(x);
if(L<=mid)query(x<<1,l,mid,L,R);
if(R>mid)query(x<<1|1,mid+1,r,L,R);
}
void updata(int x,int l,int r,int L,int R,int delta) {
if(L<=l&&r<=R) {
tree[x].lazy+=delta;
tree[x].sum+=delta*(r-l+1);
return;
}
int mid=(tree[x].ls+tree[x].rs)>>1;
if(tree[x].lazy)pushdown(x);
if(L<=mid)updata(x<<1,l,mid,L,R,delta);
if(R>mid)updata(x<<1|1,mid+1,r,L,R,delta);
tree[x].sum=(tree[x<<1].sum+tree[x<<1|1].sum)%mod;
}
int query_tree(int x,int y) {
int ans=0;
while(top[x]!=top[y]) {
if(depth[top[x]]<depth[top[y]])swap(x,y);
res=0;
query(1,1,n,num[top[x]],num[x]);
ans+=res;
ans%=mod;
x=fa[top[x]];
}
if(depth[x]>depth[y])swap(x,y);
res=0;
query(1,1,n,num[x],num[y]);
ans+=res;
return ans%mod;
}
void updata_tree(int x,int y,int delta) {
delta%=mod;
while(top[x]!=top[y]) {
if(depth[top[x]]<depth[top[y]])swap(x,y);
updata(1,1,n,num[top[x]],num[x],delta);
x=fa[top[x]];
}
if(depth[x]>depth[y])swap(x,y);
updata(1,1,n,num[x],num[y],delta);
}
int query_son(int x) {
res=0;
query(1,1,n,num[x],num[x]+size[x]-1);
return res;
}
void updata_son(int x,int delta) {
updata(1,1,n,num[x],num[x]+size[x]-1,delta);
}
void dfs1(int x,int f,int deep){
depth[x]=deep;
fa[x]=f;
size[x]=1;
int maxson=-1;
for(register int i=head[x];i;i=edge[i].nxt) {
int v=edge[i].to;
if(v==f)continue;
dfs1(v,x,deep+1);
size[x]+=size[v];
if(size[v]>maxson)son[x]=v,maxson=size[v];
}
}
void dfs2(int x,int topf) {
num[x]=++cnt;
val[cnt]=og_val[x];
top[x]=topf;
if(!son[x])return;
dfs2(son[x],topf);
for(register int i=head[x];i;i=edge[i].nxt) {
int v=edge[i].to;
if(v==fa[x]||v==son[x])continue;
dfs2(v,v);
}
}
signed main() {
scanf("%lld%lld%lld%lld",&n,&m,&r,&mod);
for(int i=1;i<=n;i++)scanf("%lld",&og_val[i]);
for(int i=1;i<=n-1;i++){
int u,v;
scanf("%lld%lld",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs1(r,0,1);
dfs2(r,r);
build_tree(1,1,n);
while(m--){
int op,x,y,z;
cin>>op;
if(op==1){
cin>>x>>y>>z;
updata_tree(x,y,z);
}
else if(op==2){
cin>>x>>y;
cout<<query_tree(x,y)<<endl;
}
else if(op==3){
cin>>x>>z;
updata_son(x,z);
}
else if(op==4){
cin>>x;
cout<<query_son(x)<<endl;
}
}
return 0;
}
by ran_qwq @ 2024-01-02 14:36:03
@3wykx 建树没有给ls,rs赋值,并且往下递归x>>1,x>>1|1是什么鬼
by strcmp @ 2024-01-02 15:04:48
首先你维护了
by Saint_yu @ 2024-01-02 20:21:36
@ran_qwq @wql_cai OK