-
Notifications
You must be signed in to change notification settings - Fork 3
/
HeavyLightDecomposition.cpp
207 lines (186 loc) · 4.07 KB
/
HeavyLightDecomposition.cpp
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
#include <bits/stdc++.h>
using namespace std;
inline int nxt(){
int x;
scanf("%d", &x);
return x;
}
const int mod = 1000000007;
struct Tree{
int n;
vector<long long> a, b, sum;
Tree(int sz = 0){
n = 1;
while (n < sz)
n *= 2;
a.resize(n + n);
b.resize(n + n);
sum.resize(n + n);
}
void relax(int v, int l, int r){
if (v >= n)
return;
a[v + v] += a[v];
a[v + v + 1] += a[v];
b[v + v] += b[v];
b[v + v + 1] += b[v];
int mid = (l + r) / 2;
sum[v + v] += a[v] * (mid - l) * (mid + l - 1) / 2 + b[v] * (mid - l);
sum[v + v + 1] += a[v] * (r - mid) * (mid + r - 1) / 2 + b[v] * (r - mid);
a[v] = b[v] = 0;
a[v + v] %= mod;
b[v + v] %= mod;
sum[v + v] %= mod;
a[v + v + 1] %= mod;
b[v + v + 1] %= mod;
sum[v + v + 1] %= mod;
}
void _add(int v, int l1, int r1, int l, int r, int x, int y){
if (r1 <= l || r <= l1)
return;
if (l < l1)
l = l1;
if (r > r1)
r = r1;
sum[v] = (sum[v] + 1ll * y * (r - l) + 1ll * x * (r - l) * (r + l - 1) / 2) % mod;
if (l == l1 && r == r1){
a[v] += x;
b[v] += y;
return;
}
int mid = (l1 + r1) / 2;
relax(v, l1, r1);
_add(v + v, l1, mid, l, r, x, y);
_add(v + v + 1, mid, r1, l, r, x, y);
}
void add(int l, int r, int x, int y){
_add(1, 0, n, l, r, x, y);
}
int _get(int v, int l1, int r1, int l, int r){
if (r1 <= l || r <= l1)
return 0;
if (l < l1)
l = l1;
if (r > r1)
r = r1;
if (l == l1 && r == r1){
return sum[v];
}
long long res = a[v] * (r - l) * (r + l - 1) / 2 + b[v] * (r - l);
int mid = (l1 + r1) / 2;
res += _get(v + v, l1, mid, l, r) + _get(v + v + 1, mid, r1, l, r);
return res % mod;
}
int get(int l, int r){
return _get(1, 0, n, l, r);
}
};
#define next ljshflgjsf
const int N = 444444;
int top[N], size[N], level[N], next[N], pr[N];
char used[N];
vector<int> a[N];
Tree tree[N];
void dfs(int v){
used[v] = 1;
size[v] = 1;
top[v] = v;
int cur = -1;
for (auto x : a[v]){
if (!used[x]){
level[x] = level[v] + 1;
pr[x] = v;
dfs(x);
size[v] += size[x];
if (cur == -1 || size[x] > size[cur])
cur = x;
}
}
next[v] = cur;
}
void dfs1(int v){
used[v] = 2;
if (next[v] > -1)
top[next[v]] = top[v];
for (auto x : a[v])
if (used[x] != 2)
dfs1(x);
}
int get(int u, int v){
long long ans = 0;
while (top[u] != top[v]){
if (level[top[u]] > level[top[v]]){
swap(u, v);
}
int w = top[v];
ans += tree[w].get(0, level[v] - level[w] + 1);
v = pr[w];
}
int w = top[u];
if (level[u] > level[v])
swap(u, v);
ans += tree[w].get(level[u] - level[w], level[v] - level[w] + 1);
return ans % mod;
}
void update(int u, int v, int x){
vector<int> to_add;
int cur = 0;
while (top[u] != top[v]){
if (level[top[u]] < level[top[v]]){
to_add.push_back(v);
v = pr[top[v]];
} else {
int w = top[u];
tree[w].add(0, level[u] - level[w] + 1, mod - x, 1ll * (1 - level[w] + level[u] + cur) * x % mod);
cur += (level[u] - level[w] + 1);
u = pr[w];
}
}
int w = top[u];
if (level[u] > level[v])
tree[w].add(level[v] - level[w], level[u] - level[w] + 1, mod - x, 1ll * (1 - level[w] + level[u] + cur) * x % mod);
else
tree[w].add(level[u] - level[w], level[v] - level[w] + 1, x, 1ll * (cur + 1 - level[u] + level[w]) * x % mod);
cur += abs(level[u] - level[v]) + 1;
while (!to_add.empty()){
u = to_add.back();
w = top[u];
tree[w].add(0, level[u] - level[w] + 1, x, 1ll * (1 + cur) * x % mod);
to_add.pop_back();
cur += level[u] - level[w] + 1;
}
}
int main(){
int n = nxt(), q = nxt();
for (int i = 0; i < n - 1; i++){
int u = nxt(), v = nxt();
assert(u != v);
a[u].push_back(v);
a[v].push_back(u);
}
pr[0] = -1;
for (int i = 0; i < n; i++)
next[0] = -1;
dfs(0);
dfs1(0);
for (int i = 0; i < n; i++)
assert(used[i]);
vector<int> len(n);
for (int i = 0; i < n; i++){
len[top[i]] = max(len[top[i]], level[i] - level[top[i]] + 1);
}
for (int i = 0; i < n; i++){
tree[i] = Tree(len[i]);
}
while (q--){
int t = nxt();
if (t == 1){
int u = nxt(), v = nxt(), x = nxt();
update(u, v, x);
} else {
int u = nxt(), v = nxt();
cout << get(u, v) << "\n";
}
}
return 0;
}