# BZOJ4034 [HAOI2015]T2 题解&代码

note:

/**************************************************************
Problem: 4034
User: Rainbow6174
Language: C++
Result: Accepted
Time:3920 ms
Memory:29872 kb
****************************************************************/

#include<iostream>
#include<vector>
#include<cstdio>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define LL long long
using namespace std;
const int maxn = 100005;
vector<int> edge[maxn];
int n,m,op,x,tot,val[maxn],in[maxn],out[maxn],deep[maxn],son[maxn],fa[maxn],rt[maxn],fin[maxn];
LL sum[8*maxn],lazy[8*maxn],size[8*maxn];
void dfs1(int x,int pre)
{
fa[x] = pre;
son[x] = -1;
size[x] = 1;
deep[x] = deep[pre]+1;
for(int i = 0; i < edge[x].size(); i++)
if(edge[x][i] != pre)
{
dfs1(edge[x][i],x);
size[x] += size[edge[x][i]];
if(son[x]==-1 || size[edge[x][i]]>size[son[x]]) son[x] = edge[x][i];
}
}
void dfs2(int x,int root)
{
rt[x] = root;
in[x] = out[x] = ++tot;
fin[in[x]] = x;
if(son[x] != -1)dfs2(son[x],root);
for(int i = 0; i < edge[x].size(); i++)
if(edge[x][i] != fa[x] && edge[x][i] != son[x])
dfs2(edge[x][i],edge[x][i]);
out[x]=tot;
}
void maintain(int o,int l,int r)
{
if(l!=r)sum[o]=sum[lson]+sum[rson];
}
void pushdown(int o,int l,int r)
{
if(lazy[o])
{
sum[lson]+=size[lson]*lazy[o];
sum[rson]+=size[rson]*lazy[o];
lazy[lson]+=lazy[o];
lazy[rson]+=lazy[o];
}
lazy[o]=0;
}
void build(int o,int l,int r)
{
if(l==r)
{
sum[o]=(LL)val[fin[l]];
size[o]=1;
//cout<<o<<' '<<sum[o]<<endl;
return;
}
int mid = (l+r)/2;
build(lson,l,mid);
build(rson,mid+1,r);
maintain(o,l,r);
size[o]=size[lson]+size[rson];
}
void addtree(int o,int l,int r,int L,int R,int v)
{
pushdown(o,l,r);
if(R<l || L>r)return;
if(l>=L && r<=R)
{
lazy[o]+=(LL)v;
sum[o]+=((LL)size[o])*((LL)v);
//cout<<"test "<<o<<' '<<l<<' '<<r<<' '<<sum[o]<<endl;
return;
}
int mid=(l+r)>>1;
maintain(o,l,r);
}
LL query(int o,int l,int r,int L,int R)
{
pushdown(o,l,r);
if(R<l || L>r)return 0;
if(l>=L && r<=R)return sum[o];
int mid=(l+r)>>1;
return query(lson,l,mid,L,R)+query(rson,mid+1,r,L,R);
}
LL Query(int x)
{
LL ret = 0;
while(rt[x]!=1)
{
ret+=query(1,1,n,in[rt[x]],in[x]);
x=fa[rt[x]];
}
ret+=query(1,1,n,1,in[x]);
return ret;
}
int main(void)
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++)
scanf("%d",&val[i]),sum[i]=i;
int u,v;
for(int i = 1; i < n; i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs1(1,0);dfs2(1,1);
build(1,1,n);
for(int i = 0; i < m; i++)
{
scanf("%d%d",&op,&x);
if(op==1)
{
scanf("%d",&v);
}
if(op==2)
{
scanf("%d",&v);
}