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
| #include<iostream> #include<cmath> #include<cstring> #include<cstdio> #include<cstdlib> #include<algorithm> using namespace std; const int N=1e5+5; typedef long long LL; int n,m,root,p; struct Edge{ int ver,ne; }e[N*2]; int h[N],idx=0,w[N]; int id[N],nw[N],cnt=0; int dep[N],size[N],top[N],fa[N],h_son[N]; struct ST{ int l,r; LL add,sum; }tr[N*4]; void add(int x,int y){ e[idx].ver=y,e[idx].ne=h[x],h[x]=idx++; } void dfs1(int x,int father,int depth){ dep[x]=depth; fa[x]=father; size[x]=1; for(int i=h[x];i!=-1;i=e[i].ne){ int y=e[i].ver; if(y==father) continue; dfs1(y,x,depth+1); size[x]+=size[y]; if(size[h_son[x]]<size[y]) h_son[x]=y; } } void dfs2(int x,int t){ id[x]=++cnt; nw[cnt]=w[x]; top[x]=t; if(!h_son[x]) return ; dfs2(h_son[x],t); for(int i=h[x];i!=-1;i=e[i].ne){ int y=e[i].ver; if(y==fa[x]||y==h_son[x]) continue; dfs2(y,y); } } void push_up(int u){ tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum; tr[u].sum%=p; } void push_down(int u){ if(tr[u].add){ tr[u<<1].add+=tr[u].add; tr[u<<1].add%=p; tr[u<<1].sum+=tr[u].add*(tr[u<<1].r-tr[u<<1].l+1); tr[u<<1].sum%=p; tr[u<<1|1].add+=tr[u].add; tr[u<<1|1].add%=p; tr[u<<1|1].sum+=tr[u].add*(tr[u<<1|1].r-tr[u<<1|1].l+1); tr[u<<1|1].sum%=p; tr[u].add=0; } } void build(int u,int l,int r){ tr[u].l=l; tr[u].r=r; tr[u].add=0; if(l==r){ tr[u].sum=nw[r]; return ; } int mid=l+r>>1; build(u<<1,l,mid); build(u<<1|1,mid+1,r); push_up(u); } void modify(int u,int l,int r,int k){ if(tr[u].l>=l&&tr[u].r<=r){ tr[u].add+=k; tr[u].add%=p; tr[u].sum+=k*(tr[u].r-tr[u].l+1); tr[u].sum%=p; return ; } push_down(u); int mid=tr[u].l+tr[u].r>>1; if(l<=mid) modify(u<<1,l,r,k); if(r>mid) modify(u<<1|1,l,r,k); push_up(u); } LL ask(int u,int l,int r){ if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum; push_down(u); int mid=tr[u].l+tr[u].r>>1; LL res=0; if(l<=mid) res+=ask(u<<1,l,r),res%=p; if(r>mid) res+=ask(u<<1|1,l,r),res%=p; return res; } void modify_path(int u,int v,int k){ while(top[u]!=top[v]){ if(dep[top[u]]<dep[top[v]]) swap(u,v); modify(1,id[top[u]],id[u],k); u=fa[top[u]]; } if(dep[u]<dep[v]) swap(u,v); modify(1,id[v],id[u],k); } LL ask_path(int u,int v){ LL res=0; while(top[u]!=top[v]){ if(dep[top[u]]<dep[top[v]]) swap(u,v); res+=ask(1,id[top[u]],id[u]); res%=p; u=fa[top[u]]; } if(dep[u]<dep[v]) swap(u,v); res+=ask(1,id[v],id[u]); return res%p; } void modify_tree(int u,int k){ modify(1,id[u],id[u]+size[u]-1,k); } LL ask_tree(int u){ return ask(1,id[u],id[u]+size[u]-1); } int main(){ memset(h,-1,sizeof h); scanf("%d%d%d%d",&n,&m,&root,&p); for(int i=1;i<=n;++i){ scanf("%d",&w[i]); w[i]%=p; } for(int i=1;i<n;++i){ int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs1(root,-1,1); dfs2(root,root); build(1,1,cnt); while(m--){ int op,u,v,k; scanf("%d%d",&op,&u); if(op==1){ scanf("%d%d",&v,&k); modify_path(u,v,k); } else if(op==2){ scanf("%d",&v); printf("%lld\n",ask_path(u,v)); } else if(op==3){ scanf("%d",&k); modify_tree(u,k); } else{ printf("%lld\n",ask_tree(u)); } } return 0; }
|