# bzoj4034: [HAOI2015]树上操作

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

int n,m;

struct node
{
int x,y,next;
}a[210000];int len,last[110000];
void ins(int x,int y)
{
len++;
a[len].x=x;a[len].y=y;
a[len].next=last[x];last[x]=len;
}

int z,dep[110000],l[110000],r[110000];
void dfs(int x,int f)
{
l[x]=++z;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y!=f)
dep[y]=dep[x]+1, dfs(y,x);
}
r[x]=z;
}

//---------init-----------------

LL s[2][110000];
int lowbit(int x){return x&-x;}
void change(int w,int x,LL k)
{
while(x<=n)
{
s[w][x]+=k;
x+=lowbit(x);
}
}
LL getsum(int w,int x)
{
LL ret=0;
while(x>=1)
{
ret+=s[w][x];
x-=lowbit(x);
}
return ret;
}

//-----------bit--------------

LL point[110000];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&point[i]);

len=0;memset(last,0,sizeof(last));
int x,y;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
z=0;dep[1]=1;dfs(1,0);

//init

for(int i=1;i<=n;i++)
change(0,l[i],point[i]), change(0,r[i]+1,-point[i]);
int op;LL d;
for(int i=1;i<=m;i++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%lld",&x,&d);
change(0,l[x],d);
change(0,r[x]+1,-d);
}
else if(op==2)
{
scanf("%d%lld",&x,&d);

change(0,l[x],-d*(dep[x]-1));
change(0,r[x]+1,d*(dep[x]-1));

change(1,l[x],d);
change(1,r[x]+1,-d);
}
else
{
scanf("%d",&x);
printf("%lld\n", getsum(0,l[x]) + dep[x]*getsum(1,l[x]) );
}
}
return 0;
}

pain and happy in the cruel world.
posted @ 2018-03-01 20:36  AKCqhzdy  阅读(185)  评论(0编辑  收藏  举报