1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #include<bits/stdc++.h> #define Inf 0x3f3f3f3f using namespace std; typedef long long LL; typedef pair<int,int> P; const int MAXX=100005;
int m,R;
int n; LL mod,a[MAXX]; vector<int> to[MAXX];
LL c1[MAXX],c2[MAXX]; inline int lowbit(int x){ return x&(-x); } inline void add(int l,int r,int x){ x %= mod; int ad1 = (LL)(l-1)*x%mod; int ad2 = (LL)r*x%mod; for(int t=l;t<=n;t+=lowbit(t)){ c1[t] = (c1[t]+x)%mod; c2[t] = (c2[t]+ad1)%mod; } for(int t=r+1;t<=n;t+=lowbit(t)){ c1[t] = (c1[t]-x)%mod; c1[t] = (c1[t]+mod)%mod; c2[t] = (c2[t]-ad2)%mod; c2[t] = (c2[t]+mod)%mod; } } inline int qwq(int i){ int res = 0; for(int t=i;t>0;t-=lowbit(t)){ res = (res+(LL)i*c1[t]%mod)%mod; res = (res-c2[t])%mod; res = (res+mod)%mod; } return res; } inline int query(int l,int r){ int res = (qwq(r)-qwq(l-1))%mod; return (res+mod)%mod; }
int depth[MAXX],num[MAXX],son[MAXX],fa[MAXX]; int top[MAXX],id[MAXX]; void dfs1(int now,int fa){ depth[now]=depth[::fa[now]=fa]+1; num[now]=1; int maxnum=0; for(int jj:to[now]){ if(jj==fa) continue; dfs1(jj,now); num[now]+=num[jj]; if(num[jj]>maxnum){ son[now]=jj; maxnum=num[jj]; } } } int cnt=0; void dfs2(int now,int t){ top[now]=t; id[now]=++cnt; if(a[now]) add(id[now],id[now],a[now]); if(son[now]) dfs2(son[now],t); for(int jj:to[now]){ if(jj==son[now]||jj==fa[now]) continue; dfs2(jj,jj); } } inline void init(int root){ dfs1(root,0); dfs2(root,root); } void addpath(int l,int r,LL jk){ while(top[l]!=top[r]){ if(depth[top[l]]<depth[top[r]]) swap(l,r); add(id[top[l]],id[l],jk); l=fa[top[l]]; } if(depth[l]>depth[r]) swap(l,r); add(id[l],id[r],jk); } LL getsum(int l,int r){ LL ret=0LL; while(top[l]!=top[r]){ if(depth[top[l]]<depth[top[r]]) swap(l,r); ret=(ret+query(id[top[l]],id[l]))%mod; l=fa[top[l]]; } if(depth[l]>depth[r]) swap(l,r); ret=(ret+query(id[l],id[r]))%mod; return ret; } void addtree(int x,LL jk){ add(id[x],id[x]+num[x]-1,jk); } LL getsum(int x){ return query(id[x],id[x]+num[x]-1); }
inline void solve(){ scanf("%d%d%d%lld",&n,&m,&R,&mod); for(int i=1;i<=n;++i) scanf("%lld",&a[i]); for(int i=1;i<n;++i){ int jj,kk;scanf("%d%d",&jj,&kk); to[jj].emplace_back(kk); to[kk].emplace_back(jj); }
init(R);
while(m--){ int ch;scanf("%d",&ch); if(ch==1){ int l,r;LL jk; scanf("%d%d%lld",&l,&r,&jk); addpath(l,r,jk); } else if(ch==2){ int l,r;scanf("%d%d",&l,&r); printf("%lld\n",getsum(l,r)); } else if(ch==3){ int x;LL jk; scanf("%d%lld",&x,&jk); addtree(x,jk); } else{ int x;scanf("%d",&x); printf("%lld\n",getsum(x)); } } }
signed main(){
solve(); return 0; }
|