树链剖分

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
#define dbg(x) cout<<#x<<"="<<x<<'\n'
#define mmt memset
#define sf sizeof
#define nl NULL
int n,m,dep[100010],top[100010],dfn[100010],edid[100010],siz[100010];
int fa[100010],hson[100010],tot1;
ll w[100010];
struct node{int l,r;ll sum,lazy;}a[100010*8];
int head[100010],to[200010],nxt[200010],tot=1;
void add(int x,int y)
{
nxt[++tot]=head[x];
head[x]=tot;to[tot]=y;
}
void dfs1(int x,int depp,int faa)
{
fa[x]=faa;dep[x]=depp;
siz[x]=1;
for(int i=head[x];i;i=nxt[i])
{
if(to[i]==faa)continue;
dfs1(to[i],depp+1,x);
siz[x]=siz[x]+siz[to[i]];
if(siz[to[i]]>siz[hson[x]])hson[x]=to[i];
}
}
void dfs2(int x,int topp)
{
top[x]=topp;
dfn[x]=++tot1;
edid[tot1]=x;
if(!hson[x])return;
dfs2(hson[x],topp);
for(int i=head[x];i;i=nxt[i])
{
if(to[i]==fa[x]||to[i]==hson[x])continue;
dfs2(to[i],to[i]);
}
}
void pushdown(int x)
{
a[x<<1].lazy+=a[x].lazy;
a[x<<1|1].lazy+=a[x].lazy;
a[x<<1].sum=a[x<<1].sum+a[x].lazy*(a[x<<1].r-a[x<<1].l+1);
a[x<<1|1].sum=a[x<<1|1].sum+a[x].lazy*(a[x<<1|1].r-a[x<<1|1].l+1);
a[x].lazy=0;
}
void maketree(int x,int l,int r)
{
a[x].l=l;a[x].r=r;a[x].lazy=0;
if(l==r)
{
a[x].sum=w[edid[l]];
return;
}
R int mid=(l+r)>>1;
maketree(x<<1,l,mid);maketree(x<<1|1,mid+1,r);
a[x].sum=a[x<<1].sum+a[x<<1|1].sum;
}
ll query(int x,int l,int r)
{
if(l>r)swap(l,r);
if(l<=a[x].l&&a[x].r<=r)return a[x].sum;
if(a[x].lazy)pushdown(x);
R int mid=(a[x].l+a[x].r)>>1;
if(r<=mid)return query(x<<1,l,r);
else if(l>mid)return query(x<<1|1,l,r);
else return query(x<<1,l,mid)+query(x<<1|1,mid+1,r);
}
ll find(int l,int r)
{
R ll res=0;
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
res+=query(1,dfn[top[l]],dfn[l]);
l=fa[top[l]];
}
if(dfn[l]<dfn[r])swap(l,r);
res+=query(1,dfn[r],dfn[l]);
return res;
}
void add(int x,int l,int r,ll v)
{
if(l>r)swap(l,r);
if(l<=a[x].l&&r>=a[x].r)
{
a[x].lazy+=v;
a[x].sum+=v*(a[x].r-a[x].l+1);
return;
}
pushdown(x);
R int mid=(a[x].l+a[x].r)>>1;
if(r<=mid)add(x<<1,l,r,v);
else if(l>mid)add(x<<1|1,l,r,v);
else add(x<<1,l,mid,v),add(x<<1|1,mid+1,r,v);
a[x].sum=a[x<<1].sum+a[x<<1|1].sum;
}
int main()
{
scanf("%d%d",&n,&m);
fo(i,1,n)scanf("%lld",&w[i]);
fo(i,2,n)
{
R int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs1(1,1,0);dfs2(1,1);
maketree(1,1,n);
while(m--)
{
R int odd;ll x,y;
scanf("%d",&odd);
if(odd==1)scanf("%lld%lld",&x,&y),add(1,dfn[x],dfn[x],y);
else if(odd==2)scanf("%lld%lld",&x,&y),add(1,dfn[x],dfn[x]+siz[x]-1,y);
else scanf("%lld",&x),printf("%lld\n",find(1,x));
}
return 0;
}
显示 Gitment 评论