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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include<bits/stdc++.h> using namespace std; #define ll long long
const int N = 1e5+5;
int n,m,r; ll p;
int h[N],nxt[N*2],to[N*2]; int cnt = 1;
void add_edge(int u,int v){ nxt[cnt]=h[u]; to[cnt]=v; h[u]=cnt++; }
ll val[N];
int fa[N],dep[N],sz[N],hson[N]; void dfs1(int u){ hson[u]=-1; sz[u]=1; for(int e=h[u];e;e=nxt[e]){ int v = to[e]; if(v==fa[u])continue; dep[v]=dep[u]+1; fa[v]=u; dfs1(v); sz[u]+=sz[v]; if(hson[u]==-1 || sz[hson[u]]<sz[v]){ hson[u]=v; } } }
int top[N],dfn[N],rnk[N],cnt2=1; void dfs2(int u,int t){ top[u]=t; rnk[cnt2]=u; dfn[u]=cnt2++; if(hson[u]==-1)return; dfs2(hson[u],t); for(int e=h[u];e;e=nxt[e]){ int v = to[e]; if(v==fa[u] || v==hson[u])continue; dfs2(v,v); } }
int ql,qr;ll qk; ll t[N<<2],lz[N<<2];
void build(int o,int l,int r){ if(l==r){ t[o] = val[rnk[l]]; return; } int ls = (o<<1),rs=((o<<1)|1); int mid = (l+r)>>1; build(ls,l,mid); build(rs,mid+1,r); t[o] = (t[ls]+t[rs])%p; } void pushdown(int o,int l,int r){ int ls = (o<<1),rs=((o<<1)|1); int mid = (l+r)>>1; t[ls] = (t[ls]+lz[o]*(mid-l+1))%p; t[rs] = (t[rs]+lz[o]*(r-mid))%p; lz[ls]=(lz[ls]+lz[o])%p; lz[rs]=(lz[rs]+lz[o])%p; lz[o]=0; } void add(int o,int l,int r){ if(ql<=l && r<=qr){ t[o] = (t[o]+qk*(r-l+1))%p; lz[o] = (lz[o]+qk)%p; return; } pushdown(o,l,r); int ls = (o<<1),rs=((o<<1)|1); int mid = (l+r)>>1; if(mid>=ql)add(ls,l,mid); if(mid<qr)add(rs,mid+1,r); t[o] = (t[ls]+t[rs])%p; } ll query(int o,int l,int r){ if(ql<=l && r<=qr){ return t[o]; } pushdown(o,l,r); int ls = (o<<1),rs=((o<<1)|1); int mid = (l+r)>>1; ll ans = 0; if(mid>=ql)ans += query(ls,l,mid); if(mid<qr)ans += query(rs,mid+1,r); return ans %p; }
void add_chain(int x,int y,ll z){ while(top[x]!=top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); ql = dfn[top[x]],qr=dfn[x],qk=z; add(1,1,n); x = fa[top[x]]; } if(dep[x]>dep[y])swap(x,y); ql = dfn[x], qr = dfn[y] ,qk=z; add(1,1,n); }
ll query_chain(int x,int y){ ll ans = 0; while(top[x]!=top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); ql = dfn[top[x]],qr=dfn[x]; ans += query(1,1,n); x = fa[top[x]]; } if(dep[x]>dep[y])swap(x,y); ql = dfn[x], qr = dfn[y]; ans += query(1,1,n); return ans % p; }
void add_tree(int x,ll z){ ql = dfn[x],qr=dfn[x]+sz[x]-1,qk=z; add(1,1,n); }
ll query_tree(int x){ ql = dfn[x],qr=dfn[x]+sz[x]-1; return query(1,1,n); }
int main(){ scanf("%d%d%d%lld",&n,&m,&r,&p); for(int i=1;i<=n;i++){ scanf("%lld",val+i); } for(int i=1;i<n;i++){ int u1,v1; scanf("%d%d",&u1,&v1); add_edge(u1,v1); add_edge(v1,u1); }
dfs1(r); dfs2(r,r);
build(1,1,n);
while(m--){ int o;scanf("%d",&o); if(o==1){ int x,y;ll z; scanf("%d%d%lld",&x,&y,&z); add_chain(x,y,z); }else if(o==2){ int x,y; scanf("%d%d",&x,&y); printf("%lld\n",query_chain(x,y)); }else if(o==3){ int x;ll z; scanf("%d%lld",&x,&z); add_tree(x,z); }else{ int x; scanf("%d",&x); printf("%lld\n",query_tree(x)); } } return 0; }
|