- 进行树链剖分,按照dfs2的遍历顺序依次标号
- 对的最短路径进行修改时,通过树链,将区间拆为一个个小区间
- 由于每个链内序号为一个区间,符合线段树
- 对子树进行修改时,子树区间即为
- 对于所有点进行修改,整合时要乘区间长度
import java.io.*;
import java.math.BigInteger;
import java.util.StringTokenizer;
//implements Runnable
public class Main{
static long md=(long)1e9+7;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int N=100010;
static int n=0;
static int m=0;
static long ans=0;
static
class Edge{
int fr,to,nxt;
public Edge(int u,int v){
fr=u;to=v;
}
}
static Edge[] e;
static int[] head;
static int cnt=0;
static void addEdge(int fr,int to){
cnt++;
e[cnt]=new Edge(fr,to);
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
static int[] dep,siz,son,fa,top,id;
static long[] v,A,add,tr;
// static int[] ls,rs;
static void init(){
e=new Edge[2*n+1];
head=new int[n+1];
cnt=0;id=new int[n+1];
dep=new int[n+1];siz=new int[n+1];son=new int[n+1];fa=new int[n+1];top=new int[n+1];
v=new long[n+1];A=new long[n+1];add=new long[n<<2];tr=new long[n<<2];
// ls=new int[n+1];rs=new int[n+1];
}
static void dfs1(int x,int f){
dep[x]=dep[f]+1;fa[x]=f;siz[x]=1;
for(int i=head[x];i>0;i=e[i].nxt){
int y=e[i].to;
if(y==f)continue;
dfs1(y,x);
siz[x]+=siz[y];
if(son[x]==0||siz[son[x]]<siz[y]){
son[x]=y;
}
}
}
static int o=0;
static void dfs2(int x,int tp){
o++;id[x]=o;A[o]=v[x];
top[x]=tp;
if(son[x]!=0)dfs2(son[x],tp);
for(int i=head[x];i>0;i=e[i].nxt){
int y=e[i].to;
if(y==fa[x]||y==son[x])continue;
dfs2(y,y);
}
}
static void pushup(int x){
tr[x]=tr[x<<1]+tr[x<<1|1];
}
static void addtag(int x,int l,int r,long d){
add[x]+=d;
tr[x]+=(r-l+1)*d;
}
static void pushdown(int x,int l,int r){
if(add[x]!=0){
int mid=(l+r)>>1;
addtag(x<<1,l,mid,add[x]);
addtag(x<<1|1,mid+1,r,add[x]);
add[x]=0;
}
}
static void build(int x,int l,int r){
if(l==r){
tr[x]=A[l];
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
static void update(int x,int l,int r,int ql,int qr,long d){
if(ql<=l&&r<=qr){
addtag(x,l,r,d);
return;
}
pushdown(x,l,r);
int mid=(l+r)>>1;
if(ql<=mid)update(x<<1,l,mid,ql,qr,d);
if(qr>mid)update(x<<1|1,mid+1,r,ql,qr,d);
pushup(x);
}
static long query(int x,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
return tr[x];
}
pushdown(x,l,r);
long res=0;
int mid=(l+r)>>1;
if(ql<=mid)res+=query(x<<1,l,mid,ql,qr);
if(qr>mid)res+=query(x<<1|1,mid+1,r,ql,qr);
return res;
}
static void update_road(int x,int y,long d){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
int t=x;x=y;y=t;
}
update(1,1,n,id[top[x]],id[x],d);
x=fa[top[x]];
}
if(dep[x]<dep[y]){
int t=x;x=y;y=t;
}
update(1,1,n,id[y],id[x],d);
}
static long query_road(int x,int y){
long res=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
int t=x;x=y;y=t;
}
res+=query(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(dep[x]<dep[y]){
int t=x;x=y;y=t;
}
res+=query(1,1,n,id[y],id[x]);
return res;
}
static void update_sonTree(int x,long d){
update(1,1,n,id[x],id[x]+siz[x]-1,d);
}
static long query_sonTree(int x){
return query(1,1,n,id[x],id[x]+siz[x]-1);
}
static void solve() throws Exception{
//快读快输
AReader input=new AReader();
// Scanner input=new Scanner(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String al="abcdefghijklmnopqrstuvwxyz";
char[] ac=al.toCharArray();
n=input.nextInt();
init();
for(int i=1;i<=n;++i)v[i]=input.nextInt();
for(int i=1;i<n;++i){
int u=input.nextInt();
int v=input.nextInt();
addEdge(u,v);addEdge(v,u);
}
dfs1(1,0);
dfs2(1,1);
build(1,1,n);
m=input.nextInt();
while(m>0){
m--;
int op=input.nextInt();
if(op==1){
int u=input.nextInt();
int v=input.nextInt();
int w=input.nextInt();
update_road(u,v,w);
}else if(op==2){
int u=input.nextInt();
long w=input.nextInt();
update_sonTree(u,w);
}else if(op==3){
int u=input.nextInt();
int v=input.nextInt();
long ans=query_road(u,v);
out.write(ans+"\n");
}else{
int u=input.nextInt();
long ans=query_sonTree(u);
out.write(ans+"\n");
}
}
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
solve();
}
// public static final void main(String[] args) throws Exception {
// new Thread(null, new Main(), "线程名字", 1 << 27).start();
// }
// @Override
// public void run() {
// try {
// //原本main函数的内容
// solve();
//
// } catch (Exception e) {
// }
// }
static//快读模板,不用管
class AReader{
BufferedReader bf;
StringTokenizer st;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
}
}