hdu5405 Sometimes Naive[树链剖分]

Problem Description

http://acm.hdu.edu.cn/showproblem.php?pid=5Rhason

Cheung had a naive problem, and asked Teacher Mai for help. But Teacher Mai thought this problem was too simple, sometimes naive. So she ask you for help.

She has a tree with n vertices, numbered from 1 to n. The weight of i-th node is wi.

You need to support two kinds of operations: modification and query.

For a modification operation u,w, you need to change the weight of u-th node into w.

For a query operation u,v, you should output ∑n i=1∑n j=1f(i,j). If there is a vertex on the path from u to v and the path from i to j in the tree, f(i,j)=wiwj, otherwise f(i,j)=0. The number can be large, so print the number modulo 109+7

Input

There are multiple test cases.

For each test case, the first line contains two numbers n,m(1≤n,m≤105).

There are n numbers in the next line, the i-th means wi(0≤wi≤109).

Next n−1 lines contain two numbers each, ui and vi, that means that there is an edge between ui and vi.

The following are m lines. Each line indicates an operation, and the format is “1 u w”(modification) or “2 u v”(query)(0≤w≤109)

Output

For each test case, print the answer for each query operation.

Sample Input

6 5
1 2 3 4 5 6
1 2
1 3
2 4
2 5
4 6
2 3 5
1 5 6
2 2 3
1 1 7
2 2 4

Sample Output

341
348
612

题意

给你一棵n个节点的树,有点权。
要求支持两种操作:
   操作1:更改某个节点的权值。
   操作2:给定u,v, 求 Σw[i][j] i , j 为任意两点且i到j的路径与u到v的路径相交。]

题解

用线段树维护权值和,还有出开重儿子以外的儿子 所在的子树的和的平方 的和.
然后就可以瞎搞,求出答案.
然后被我写得很丑
修改操作的话,只要修改自己到跟节点上,每条重链的最底部的点.

然后一波特判,过掉的.

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
inline int read()
{
char s;int k=0,base=1;
while((s=getchar())!='-'&&s!=EOF&&!(isdigit(s)));
if(s==EOF)exit(0);if(s=='-')base=-1,s=getchar();
while(isdigit(s)){k=k*10+(s^'0');s=getchar();}
return k*base;
}
void write(int x)
{
if(x<0){putchar('-');write(-x);}
else{if(x/10)write(x/10);putchar(x%10+'0');}
}
const int maxn=1e5+1000;const int mod=1e9+7;
int n,m,X,Y,a[maxn],po[maxn],ne[maxn<<1],to[maxn<<1],id1,bj;
int fa[maxn],son[maxn],size[maxn],deep[maxn],id[maxn],top[maxn],dfs_time,Map[maxn];
int t[maxn<<2],s[maxn<<2];
int b[maxn];
long long sum[maxn];//sum: 子树和 b:轻边子树平方和

void add(int x,int y)
{
to[++id1]=y;ne[id1]=po[x];po[x]=id1;
}
inline void dfs1(int x)
{
son[x]=0;size[x]=1;deep[x]=deep[fa[x]]+1;sum[x]=a[x];
for (int i=po[x];i;i=ne[i])
if (to[i]!=fa[x])
{
fa[to[i]]=x;dfs1(to[i]);size[x]+=size[to[i]];
if (size[to[i]]>size[son[x]]) son[x]=to[i];
sum[x]+=sum[to[i]];if (sum[x]>=mod) sum[x]-=mod;
}
}
inline void dfs(int x,int last)
{
top[x]=last;id[x]=++dfs_time;Map[dfs_time]=x;b[x]=0;
if (son[x]==0) return; dfs(son[x],last);
for (int i=po[x];i;i=ne[i])
if (to[i]!=fa[x]&&to[i]!=son[x])
{
dfs(to[i],to[i]);
b[x]+=sum[to[i]]*sum[to[i]]%mod;
if (b[x]>=mod) b[x]-=mod;
}
}

inline void bt(int x,int y,int d)
{
if (x==y) {t[d]=a[Map[x]];s[d]=b[Map[x]];return;}
int mid=(x+y)>>1;bt(x,mid,d<<1);bt(mid+1,y,d<<1|1);
t[d]=t[d<<1]+t[d<<1|1];t[d]= t[d]>=mod?t[d]-=mod:t[d];
s[d]=s[d<<1]+s[d<<1|1];s[d]= s[d]>=mod?s[d]-=mod:s[d];
}
inline void xg(int x,int y,int d,int l,int r)
{
if (x==l&&x==r) {t[d]=y;return;}
int mid=(l+r)>>1;if (x<=mid) xg(x,y,d<<1,l,mid); else xg(x,y,d<<1|1,mid+1,r);
t[d]=t[d<<1]+t[d<<1|1];t[d]= t[d]>=mod?t[d]-=mod:t[d];
}
inline void xg1(int x,int y,int d,int l,int r)
{
if (x==l&&r==x) {s[d]=y;return;}
int mid=(l+r)>>1;if (x<=mid) xg1(x,y,d<<1,l,mid); else xg1(x,y,d<<1|1,mid+1,r);
s[d]=s[d<<1]+s[d<<1|1];s[d]= s[d]>=mod?s[d]-=mod:s[d];
}

inline int qh(int x,int y,int d,int l,int r)
{
if (x<=l&&r<=y) return t[d];
int mid=(l+r)>>1;
if (y<=mid) return qh(x,y,d<<1,l,mid); else
if (x>mid) return qh(x,y,d<<1|1,mid+1,r);
long long ss=qh(x,mid,d<<1,l,mid)+qh(mid+1,y,d<<1|1,mid+1,r);
if (ss>=mod) ss-=mod;
return ss;
}
inline int qh1(int x,int y,int d,int l,int r)
{
if (x<=l&&r<=y) {return s[d];}
int mid=(l+r)>>1;
if (y<=mid) return qh1(x,y,d<<1,l,mid); else
if (x>mid) return qh1(x,y,d<<1|1,mid+1,r);
long long ss=qh1(x,mid,d<<1,l,mid)+qh1(mid+1,y,d<<1|1,mid+1,r);if (ss>=mod) ss-=mod;
return ss;
}
inline int lca(int x,int y)
{
int tx=top[x],ty=top[y];
while (tx!=ty)
{
if (deep[tx]<deep[ty]) swap(tx,ty),swap(x,y);
x=fa[tx];tx=top[x];
}
if (deep[x]<deep[y]) return x;return y;
}
int flag[2];int flag1[2];
inline void Mod(long long &a) {if (a>=mod) a%=mod;if (a<0) a=a%mod+mod;}
inline long long qsum(int x,int y,int D)
{
//if (x==y) return 0;
int XX=x,YY=y;
int tx=top[x];
long long sum=0,s1=0,s2=0;
while (tx!=top[y])
{
Mod(sum+=qh1(id[tx],id[x],1,1,n));
s2=0;
s2=qh(id[tx],id[tx]+size[tx]-1,1,1,n);s2=s2*s2%mod;
sum-=s2;Mod(sum);
if (fa[tx]==y) {flag[D]=false;flag1[D]=tx;return sum;}
x=fa[tx];tx=top[x];
if (son[x]!=0) s1=qh(id[son[x]],id[son[x]]+size[son[x]]-1,1,1,n);
s1=s1*s1%mod;
sum+=s1;Mod(sum);
}
sum+=qh1(id[y],id[x],1,1,n);
Mod(sum);
flag[D]=true;
if (XX!=YY) flag1[D]=son[y];
if (XX==YY) flag[D]=false;
return sum;
}
long long s1,S[1000],ls,s233,ss;
long long ans;
int main()
{
while (scanf("%d",&n)!=EOF)
{
dfs_time=0;
memset(po,0,sizeof(po));
id1=0;
m=read();for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<n;i++) X=read(),Y=read(),add(X,Y),add(Y,X);
flag[1]=flag[0]=0;
deep[0]=0;
dfs1(1);dfs(1,1);bt(1,n,1);
for (int i=1;i<=m;i++)
{
bj=read();X=read();Y=read();
if (bj==1)//修改
{
if (Y>=mod) Y-=mod;
int XX=X;ls=0;
int tx=top[X];
while (XX)
{
ls++;//存之前的子树和
S[ls]=qh(id[tx],id[tx]+size[tx]-1,1,1,n);
if (fa[tx]==0) break;
XX=fa[tx];tx=top[XX];
}
xg(id[X],Y,1,1,n);
tx=top[X];
int j=0;
while (X)
{
j++;
if (fa[tx]==0) break;//跳到根了
ss=qh(id[tx],id[tx]+size[tx]-1,1,1,n);
ss=ss*ss%mod;
ss+=qh1(id[fa[tx]],id[fa[tx]],1,1,n);
if (ss>=mod) ss-=mod;
ss-=S[j]*S[j]%mod;
ss=(ss%mod+mod)%mod;
xg1(id[fa[tx]],ss,1,1,n);//修改
X=fa[tx];tx=top[X];
}
} else
{
int LCA=lca(X,Y);
s1=qh(1,n,1,1,n);
s233=qh(id[LCA],id[LCA]+size[LCA]-1,1,1,n);
s233=s1-s233;Mod(s233);
s233=s233*s233%mod;s1=s1*s1%mod;
if (X==Y)//一个点的情况
{
ans=qh1(id[X],id[X],1,1,n);
if (son[X]!=0)
{
ss=qh(id[son[X]],id[son[X]]+size[son[X]]-1,1,1,n);
ss=(long long)ss*(long long)ss%mod;
ans+=ss;if (ans>=mod) ans-=mod;
}
ans=s1-s233-ans;
ans=(ans%mod)+mod;ans%=mod;
printf("%lld\n",ans);
continue;
}
ans=qsum(X,LCA,0)+qsum(Y,LCA,1);if (ans>=mod) ans-=mod;
if (son[X]!=0&&X!=LCA)
{ss=qh(id[son[X]],id[son[X]]+size[son[X]]-1,1,1,n);ss=ss*ss%mod;ans+=ss;if (ans>=mod) ans-=mod;}

if (son[Y]!=0&&Y!=LCA)
{
ss=qh(id[son[Y]],id[son[Y]]+size[son[Y]]-1,1,1,n);
ss=ss*ss%mod;ans+=ss;if (ans>=mod) ans-=mod;
}
if (!(flag[0]||flag[1]))
{
ss=qh(id[son[LCA]],id[son[LCA]]+size[son[LCA]]-1,1,1,n);
ss=ss*ss%mod;ans+=ss;if (ans>=mod) ans-=mod;
if (X!=LCA&&Y!=LCA) {ss=qh1(id[LCA],id[LCA],1,1,n);ans+=ss;if (ans>=mod) ans-=mod;}
}

if (X==LCA||Y==LCA)
{
if ((X==LCA&&flag1[1]==son[LCA])||(Y==LCA&&flag1[0]==son[LCA]))
{
ss=qh1(id[LCA],id[LCA],1,1,n);
ans-=ss;
if (ans<0) ans+=mod; else if (ans>=mod) ans-=mod;
}
}
ans=s1-ans-s233;
ans=(ans%mod+mod)%mod;
printf("%lld\n",ans);
}
}
}
return 0;
}