Preface
为什么没有人做?
Description
给定序列 和两棵 个点的树 。
设 表示树 上路径 上的点集。
执行 次操作 :
- 先对每个 执行 。
- 然后求 的值。
Limitations
Solution
首先对两棵树进行树剖。
然后发现修改是关于 的,但查询是关于 的,线段树维护不了。
考虑设排列 满足 ,那么可以将问题转化成:
problem
给定序列 和排列 ,你需要执行 次操作,分两种:
1 l r k:对每个 执行 。2 l r:求 。
这是个很典的问题,对 和 都分个块,修改时改排列块,查询时查序列块,处理好修改对查询的贡献即可(具体见代码)。
然后做完了,时间复杂度 ,但是常数小,可以碾过树分块。
code
qoj 上是最优解。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;
template<class T>
bool chmax(T &a, const T &b){
if(a < b){ a = b; return true; }
return false;
}
template<class T>
bool chmin(T &a, const T &b){
if(a > b){ a = b; return true; }
return false;
}
struct HLD {
int n, clock;
vector<vector<int>> g;
vector<int> siz, par, dep, son, dfn, top;
inline HLD() {}
inline HLD(int n) : n(n), clock(0) {
g.resize(n);
siz.resize(n);
par.resize(n);
dep.resize(n);
son.resize(n, -1);
dfn.resize(n);
top.resize(n);
}
inline void add_edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
void dfs(int u, int fa) {
siz[u] = 1, par[u] = fa;
for (auto v : g[u]) {
if (v == fa) continue;
dep[v] = dep[u] + 1;
dfs(v, u);
siz[u] += siz[v];
if (son[u] == -1 || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int topf) {
dfn[u] = clock++, top[u] = topf;
if (~son[u]) dfs2(son[u], topf);
for (auto v : g[u]) {
if (v == par[u] || v == son[u]) continue;
dfs2(v, v);
}
}
void decomp(int u, int v, vector<pair<int, int>> &vec) {
vec.clear();
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
vec.emplace_back(dfn[top[u]], dfn[u]);
u = par[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
vec.emplace_back(dfn[v], dfn[u]);
}
inline void init() {
dfs(0, 0);
dfs2(0, 0);
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
vector<ui64> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
HLD t1(n), t2(n);
for (int i = 0, u, v; i < n - 1; i++) {
cin >> u >> v, u--, v--;
t1.add_edge(u, v);
}
for (int i = 0, u, v; i < n - 1; i++) {
cin >> u >> v, u--, v--;
t2.add_edge(u, v);
}
t1.init(), t2.init();
const int B = 450;
const int blocks = (n + B - 1) / B;
vector<int> fr(n), to(n), L(blocks), R(blocks);
for (int i = 0; i < blocks; i++) {
L[i] = i * B;
R[i] = min(L[i] + B, n) - 1;
}
for (int i = 0; i < n; i++) to[t1.dfn[i]] = t2.dfn[i];
for (int i = 0; i < n; i++) fr[to[i]] = i;
vector<vector<ui64>> cnt(blocks, vector<ui64>(blocks));
vector<ui64> val(n), sum(blocks), tag(blocks);
auto brute_update = [&](int l, int r, ui64 w) {
for (int i = l; i <= r; i++) sum[to[i] / B] += w, val[i] += w;
};
auto brute_query = [&](int l, int r) {
ui64 ans = 0;
for (int i = l; i <= r; i++) ans += val[fr[i]] + tag[fr[i] / B];
return ans;
};
auto block_update = [&](int l, int r, ui64 w) {
if (l > r) return;
for (int i = 0; i < blocks; i++) sum[i] += (cnt[r][i] - cnt[l - 1][i]) * w;
for (int i = l; i <= r; i++) tag[i] += w;
};
auto block_query = [&](int l, int r) {
ui64 ans = 0;
for (int i = l; i <= r; i++) ans += sum[i];
return ans;
};
auto update = [&](int l, int r, ui64 w) {
const int bl = l / B, br = r / B;
if (bl == br) return brute_update(l, r, w);
brute_update(l, R[bl], w);
brute_update(L[br], r, w);
block_update(bl + 1, br - 1, w);
};
auto query = [&](int l, int r) {
const int bl = l / B, br = r / B;
if (bl == br) return brute_query(l, r);
return brute_query(l, R[bl]) + brute_query(L[br], r) + block_query(bl + 1, br - 1);
};
for (int i = 0; i < n; i++) cnt[i / B][to[i] / B]++;
for (int i = 1; i < blocks; i++) {
for (int j = 0; j < blocks; j++) cnt[i][j] += cnt[i - 1][j];
}
for (int i = 0; i < n; i++) update(t1.dfn[i], t1.dfn[i], a[i]);
vector<pair<int, int>> path;
for (int i = 0, u, v; i < q; i++) {
ui64 k; cin >> u >> v >> k;
u--, v--;
t1.decomp(u, v, path);
for (auto [l, r] : path) update(l, r, k);
t2.decomp(u, v, path);
ui64 ans = 0;
for (auto [l, r] : path) ans += query(l, r);
cout << ans << '\n';
}
return 0;
}