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
| #include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 400009;
typedef long long LL;
const LL P = 1000000007ll;
vector<int> son[maxn];
LL a[maxn], c[maxn];
int fa[maxn], dfn[maxn], efn[maxn], clo = 1, n, m;
inline int getint() {
int r = 0; bool b = true; char c = getchar();
while ('0' > c || c > '9') { if (c == '-') b = false; c = getchar(); }
while ('0' <= c && c <= '9') { r = r * 10 + (c - '0'); c = getchar(); }
if (b) return r;
return -r;
}
LL ksm(LL a, LL b, LL c) {
LL r = 1ll, t = a;
while (b) {
if (b & 1ll) r = (r * t) % c;
t = (t * t) % c;
b >>= 1ll;
}
return r;
}
LL niyuan(LL x) { return ksm(x, P-2ll, P); }
namespace splay {
int c[maxn][2], fa[maxn], sta[maxn], siz[maxn], tail;
LL tag[maxn], sum[maxn], val[maxn], dep[maxn], tag2[maxn];
void pushup(int x) {
sum[x] = (sum[c[x][0]] + sum[c[x][1]] + val[x]) % P;
siz[x] = siz[c[x][0]] + siz[c[x][1]] + 1;
}
void mul(int x, LL v) {
tag[x] = (tag[x] * v) % P;
val[x] = (val[x] * v) % P;
sum[x] = (sum[x] * v) % P;
}
void mul2(int x, LL v) {
tag2[x] = tag2[x] * v % P;
dep[x] = dep[x] * v % P;
}
void pushdown(int x) {
if (c[x][0]) { mul(c[x][0], tag[x]); mul2(c[x][0], tag2[x]); }
if (c[x][1]) { mul(c[x][1], tag[x]); mul2(c[x][1], tag2[x]); }
tag[x] = tag2[x] = 1;
}
void rotate(int x) {
int y = fa[x], z = fa[y], a = c[y][1] == x, b = a ^ 1;
if (z) c[z][c[z][1]==y] = x;
fa[x] = z; fa[y] = x; if (c[x][b]) fa[c[x][b]] = y;
c[y][a] = c[x][b]; c[x][b] = y;
pushup(y); pushup(x);
}
void splay(int x, int pos) {
int y;
for (y = x; y; y = fa[y]) sta[++tail] = y;
while (tail) pushdown(sta[tail--]);
while (fa[x] != pos) {
y = fa[x];
if (fa[y] != pos) {
if (c[y][0] == x ^ c[fa[y]][0] == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
int pre(int x) {
splay(x, 0);
x = c[x][0];
while (c[x][1]) x = c[x][1];
return x;
}
int suc(int x) {
splay(x, 0);
x = c[x][1];
while (c[x][0]) x = c[x][0];
return x;
}
int Ord(int x) {
splay(x, 0);
return siz[c[x][0]];
}
bool is_anc(int x, int y) {
return Ord(dfn[x]) <= Ord(dfn[y]) && Ord(efn[y]) <= Ord(efn[x]);
}
int split(int i) {
int x = pre(dfn[i]), y = suc(efn[i]);
splay(x, 0); splay(y, x);
pushdown(x); pushdown(y);
int ret = c[y][0]; c[y][0] = 0; fa[ret] = 0;
pushup(y); pushup(x);
return ret;
}
void modify(int x, LL v) {
int pos = dfn[x];
splay(pos, 0);
val[pos] = (val[pos] * v) % P;
pushup(pos);
}
LL query(int x) {
x = dfn[x];
splay(x, 0);
return dep[x];
}
void modify_tree(int x, LL v) {
splay(x, 0);
mul(x, v); mul2(x, v);
}
void modify_tree2(int x, LL v) {
int l = pre(dfn[x]), r = suc(efn[x]);
splay(l, 0); splay(r, l);
if (c[r][0]) { mul(c[r][0], v); mul2(c[r][0], v); }
pushup(r); pushup(l);
}
void insert_tree(int x, int y) {
int l = dfn[y], r = suc(l);
splay(l, 0); splay(r, l);
pushdown(l); pushdown(r);
c[r][0] = x; fa[x] = r;
pushup(r); pushup(l);
}
void qinding(int x, LL v) {
x = dfn[x];
splay(x, 0);
val[x] = v;
pushup(x);
}
}
void dfs(int now, int father) {
dfn[now] = ++clo;
fa[now] = father;
c[now] = 1;
for (auto &v : son[now]) if (v != father) { dfs(v, now); ++c[now]; }
efn[now] = ++clo;
}
void dfs2(int now, LL dis) {
dis = (dis * niyuan(c[now])) % P;
splay::val[dfn[now]] = (dis * a[now]) % P;
splay::dep[dfn[now]] = dis;
for (auto &v : son[now]) if (v != fa[now]) dfs2(v, dis);
}
int main() {
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
int x, y, z;
n = getint(); m = getint();
for (int i = 1; i <= n; ++i) a[i] = getint();
for (int i = 1; i < n; ++i) {
x = getint(); y = getint();
son[x].push_back(y); son[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 1ll);
splay::tag[clo + 1] = splay::tag2[clo + 1] = 1;
splay::pushup(clo + 1);
for (int i = clo; i >= 1; --i) {
splay::c[i][1] = i + 1;
splay::pushup(i);
splay::fa[i + 1] = i;
splay::tag[i] = splay::tag2[i] = 1;
}
for (int i = 1; i <= m; ++i) {
z = getint(); x = getint(); y = getint();
if (z == 0) {
splay::qinding(x, splay::query(x) * LL(y) % P);
a[x] = y;
}
else {
if (!splay::is_anc(x, y)) {
z = splay::split(x);
splay::modify_tree(z, niyuan(splay::query(fa[x])));
splay::modify_tree2(fa[x], c[fa[x]] * niyuan(c[fa[x]] - 1) % P);
--c[fa[x]];
splay::modify_tree2(y, c[y] * niyuan(c[y] + 1) % P);
splay::modify_tree(z, splay::query(y));
splay::insert_tree(z, y);
++c[y]; fa[x] = y;
}
}
splay::splay(1, 0);
printf("%lld\n", splay::sum[1]);
}
}
|