| 记录编号 |
161199 |
评测结果 |
AAAAAAAAAA |
| 题目名称 |
1963.[HAOI 2015]树上操作 |
最终得分 |
100 |
| 用户昵称 |
TAT |
是否通过 |
通过 |
| 代码语言 |
C++ |
运行时间 |
0.571 s |
| 提交时间 |
2015-05-02 14:40:51 |
内存使用 |
5.50 MiB |
显示代码纯文本
#include<cstdio>
const int maxn=100010;
bool vis[maxn];
int st[maxn],en[maxn];
int n,m,val[maxn],dep[maxn];
long long k[maxn],b[maxn],c[maxn];
int cnt,tot,head[maxn],to[maxn<<1],next[maxn<<1];
void ins(int u,int v){
to[++tot]=v,next[tot]=head[u],head[u]=tot;
to[++tot]=u,next[tot]=head[v],head[v]=tot;
}
void add(long long *c,int i,long long x){
while(i<=n) c[i]+=x,i+=i&-i;
}
long long query(long long *c,int i){
static long long ret;
for(ret=0;i>0;i-=i&-i) ret+=c[i];
return ret;
}
int read(){
static int f,ch,ret;
while((ch=getchar())<40);
if(ch!=45) f=0,ret=ch-48;
else f=1,ret=0;
while((ch=getchar())>47)
ret=ret*10+ch-48;
if(f) ret=-ret;
return ret;
}
void dfs(int from,int x,long long sum){
dep[x]=dep[from]+1,sum+=val[x],c[x]=sum,st[x]=++cnt,vis[x]=1;
for(int i=head[x];i;i=next[i])
if(!vis[to[i]]) dfs(x,to[i],sum);
en[x]=cnt;
}
int main(){
freopen("haoi2015_t2.in","r",stdin);
freopen("haoi2015_t2.out","w",stdout);
int i,u,v,op;
long long tmp;
n=read(),m=read();
for(i=1;i<=n;i++)
val[i]=read();
for(i=1;i<n;i++)
ins(read(),read());
dfs(0,1,0ll);
while(m--){
op=read();
switch(op){
case 1:
u=read(),v=read();
add(b,st[u],v),add(b,en[u]+1,-v);
break;
case 2:
u=read(),v=read();
tmp=1-dep[u],tmp*=v;
add(k,st[u],v),add(k,en[u]+1,-v);
add(b,st[u],tmp),add(b,en[u]+1,-tmp);
break;
case 3:
u=read();
printf("%lld\n",c[u]+query(k,st[u])*dep[u]+query(b,st[u]));
break;
}
}
return 0;
}