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 176 177 178 179 180 181 182 183 184 185 186 187
| #include<bits/stdc++.h> #define maxn 100007 #define ol putchar('\n') #define le(x) x<<1 #define re(x) x<<1|1 #define ll long long using namespace std; int n,m,head[maxn],cent,dep[maxn],son[maxn],fa[maxn],vis[maxn]; int top[maxn],a[maxn],id[maxn],w[maxn],sz[maxn],cnt,ij,root; ll tr[maxn<<3],tag[maxn<<3]; struct node{ int next,to; }edge[maxn<<3];
template<typename type_of_scan> inline void scan(type_of_scan &x){ type_of_scan f=1;x=0;char s=getchar(); while(s<'0'||s>'9') f=s=='-'?-1:1,s=getchar(); while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+s-'0',s=getchar(); x*=f; } template<typename type_of_print> inline void print(type_of_print x){ if(x<0) putchar('-'),x=-x; if(x>9) print(x/10); putchar(x%10+'0'); }
inline void add(int u,int v){ edge[++cent]=(node){head[u],v};head[u]=cent; }
void push_up(int p){ tr[p]=tr[le(p)]+tr[re(p)]; }
void push_down(int l,int r,int p,ll k){ int mid=l+r>>1; tr[le(p)]+=1ll*(mid-l+1)*k, tr[re(p)]+=1ll*(r-mid)*k, tag[le(p)]+=k,tag[re(p)]+=k; }
void build(int l,int r,int p){ if(l==r){ tr[p]=w[l]; return ; } int mid=l+r>>1; build(l,mid,le(p)); build(mid+1,r,re(p)); push_up(p); }
void r_add(int nl,int nr,int l,int r,int p,int k){ if(nl<=l&&nr>=r){ tr[p]+=1ll*(r-l+1)*k; tag[p]+=1ll*k; return ; } push_down(l,r,p,tag[p]),tag[p]=0; int mid=l+r>>1; if(nl<=mid) r_add(nl,nr,l,mid,le(p),k); if(nr>mid) r_add(nl,nr,mid+1,r,re(p),k); push_up(p); }
ll r_query(int nl,int nr,int l,int r,int p){ ll ans=0; if(nl<=l&&nr>=r) return tr[p]; push_down(l,r,p,tag[p]),tag[p]=0; int mid=l+r>>1; if(nl<=mid) ans+=r_query(nl,nr,l,mid,le(p)); if(nr>mid) ans+=r_query(nl,nr,mid+1,r,re(p)); return ans; }
void dfs1(int x){ sz[x]=1;int max_part=-1;vis[x]++; for(int i=head[x];i;i=edge[i].next){ int y=edge[i].to; if(y==fa[x]) continue; fa[y]=x;dep[y]=dep[x]+1; dfs1(y);sz[x]+=sz[y]; if(max_part<sz[y]) son[x]=y,max_part=sz[y]; } }
void dfs2(int x,int t){ id[x]=++cnt;w[cnt]=a[x];top[x]=t; if(!son[x]) return ; dfs2(son[x],t); for(int i=head[x];i;i=edge[i].next){ int y=edge[i].to; if(y==son[x]||fa[x]==y) continue; dfs2(y,y); } }
int Lca(int x,int y){ while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); x=fa[top[x]]; } return dep[x]>dep[y]?y:x; }
int find(int x,int y){ while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); if(fa[top[x]]==y) return top[x]; x=fa[top[x]]; } if(dep[x]<dep[y]) swap(x,y); return son[y]; }
void tree_add(int x,int k){ if(root==x) r_add(1,n,1,n,1,k); else{ int lca=Lca(x,root); if(lca!=x) r_add(id[x],id[x]+sz[x]-1,1,n,1,k); else{ int dson=find(x,root); r_add(1,n,1,n,1,k); r_add(id[dson],id[dson]+sz[dson]-1,1,n,1,-k); } } return ; }
ll tree_query(int x){ if(root==x) return r_query(1,n,1,n,1); else{ int lca=Lca(x,root); if(lca!=x) return r_query(id[x],id[x]+sz[x]-1,1,n,1); else{ int dson=find(x,root); return r_query(1,n,1,n,1)-r_query(id[dson],id[dson]+sz[dson]-1,1,n,1); } } }
void road_add(int x,int y,ll k){ while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); r_add(id[top[x]],id[x],1,n,1,k); x=fa[top[x]]; } if(dep[x]>dep[y]) swap(x,y); r_add(id[x],id[y],1,n,1,k); return ; }
ll road_query(int x,int y){ ll ans=0; while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); ans+=r_query(id[top[x]],id[x],1,n,1); x=fa[top[x]]; } if(dep[x]>dep[y]) swap(x,y); ans+=r_query(id[x],id[y],1,n,1); return ans; }
int main(){
scan(n); for(int i=1;i<=n;i++) scan(a[i]); for(int i=2,v;i<=n;i++) scan(v),add(i,v),add(v,i); dfs1(1),dfs2(1,1),build(1,n,1);root=1; scan(m); for(int i=1;i<=m;i++){ int type,x,y,z; scan(type),scan(x); if(type==1) root=x; else if(type==2) scan(y),scan(z),road_add(x,y,z); else if(type==3) scan(z),tree_add(x,z); else if(type==4) scan(y),printf("%lld\n",road_query(x,y)); else if(type==5) printf("%lld\n",tree_query(x)); } return 0; }
|