题目链接:CF 或者 洛谷
本题难点在于换根 LCA 与换根以后的子树范围寻找,重点讲解
先说操作一,假如原根为 \(1\) 变为了 \(x\),又变为了 \(y\),那么其实 \(y\) 和 \(x\) 都可以看做由 \(1\) 变化而来的,即 \(1 \rightarrow x\) 与 \(1 \rightarrow y\),原因很简单,我们可以把 \(1 \rightarrow x\) 恢复成 \(1\),再变为 \(y\),这样换根的形态是没有发生任何变化的。所以这个操作我们可以直接换根。
第二个和第三个操作都可以总结为两步,找以当前为根的 \(LCA\) 与 子树在 \(1\) 为根中的实际范围。
先说第一个如何找 \(LCA\),其实分讨下容易发现,分 \(x\) 与 \(y\) 与 \(root\) 原来是否具有子树关系:
-
都是 \(root\) 为根的子树上的点,显然 \(lca\) 即为 \(root\)。
-
一个是子树上的,一个非子树上的。如图所示还是 \(root\)。
- 都不在子树里。
这个我们这样考虑,\(t_1=lca(root,x)\),\(t_2=lca(root,y)\),深度更深的那个,其实就是 lca,原因,这种情况下换根,\(x\) 和 \(y\) 的形态并未发生变化,而 \(t1\) 和 \(t2\) 其实就为以 \(root\) 为根以后的 新子树节点,这个新子树节点包括了 \(x\) 或者 \(y\)。根据第二种换根我们可以知道 \(t1\) 或者 \(t2\) 都有可以是 \(lca\) 的祖先节点,而深度最深的那个显然为真正的 \(lca\)。
这三个情况可以统一成,我们再求出 \(t_3=lca(x,y)\),那么 \(t_1\)、\(t_2\)、\(t_3\) 中最深的点即为换根后的 \(lca\),这也是动态 \(lca\) 的基本套路。
接下来解决如何找到当前 \(lca\) 子树范围在原序上的范围。
前两种情况显然 \(lca=root\),换根后的子树范围即为 \([1,n]\) 包括了整棵树。
考虑第三种情况,分讨下,\(lca\) 和 \(root\) 的关系,显然就两种,\(root\) 是否在 \(lca\) 为根的子树内,一个在它子树当中,一个不在它子树当中。
- 不在子 \(lca\) 为根的子树内,直接修改原子树即可。
子树形态是并未发生变化的。
- 在 \(lca\) 为根的子树内,这个比较复杂,如图所示。
不得不说,太类似换根 dp 的套路,这玩意我们容斥来做,整棵树去掉 \(lca\) 下面那个关键点为根的子树贡献即可。
如图所示,全局加然后去掉红色部分贡献,即为正确的了,至于怎么找 \(root \rightarrow lca\) 这条路径上的最后一个点,直接倍增找就行了。
解法一
涉及到了子树加,子树求和,我们使用 \(dfs序+线段树\) 即可,当然区间加区间求和,也可以用差分树状数组,维护两个数组的 bit 即可,这里就用线段树了。当然由于是区间做加法,我们也可以用标记永久化线段树,不过这里就用普通线段树加懒标记即可。
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
// #define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 1e5 + 10;
constexpr int T = 20;
int s[N], e[N], dfn[N], cnt;
int deep[N], fa[N][T + 1];
int a[N], root = 1;
vector<int> child[N];
int n, q;
struct
{
struct Node
{
ll sum, add, len;
} node[N << 2];
#define len(x) node[x].len
#define add(x) node[x].add
#define sum(x) node[x].sum
void Add(const int curr, const ll val)
{
add(curr) += val;
sum(curr) += len(curr) * val;
}
void pushDown(const int curr)
{
if (add(curr))
{
Add(ls(curr),add(curr)), Add(rs(curr),add(curr));
add(curr) = 0;
}
}
void pushUp(const int curr)
{
sum(curr) = sum(ls(curr)) + sum(rs(curr));
}
void build(const int curr, const int l = 1, const int r = n)
{
len(curr) = r - l + 1;
const int mid = l + r >> 1;
if (l == r)
{
sum(curr) = a[dfn[l]];
return;
}
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
pushUp(curr);
}
void Add(const int curr, const int l, const int r, const int val, const int s = 1, const int e = n)
{
if (l <= s and e <= r)
{
Add(curr, val);
return;
}
const int mid = s + e >> 1;
pushDown(curr);
if (l <= mid)Add(ls(curr), l, r, val, s, mid);
if (r > mid)Add(rs(curr), l, r, val, mid + 1, e);
pushUp(curr);
}
ll Query(const int curr, const int l, const int r, const int s = 1, const int e = n)
{
if (l <= s and e <= r)return sum(curr);
pushDown(curr);
const int mid = s + e >> 1;
ll ans = 0;
if (l <= mid)ans += Query(ls(curr), l, r, s, mid);
if (r > mid)ans += Query(rs(curr), l, r, mid + 1, e);
return ans;
}
} seg;
inline void dfs(const int curr, const int parent)
{
deep[curr] = deep[fa[curr][0] = parent] + 1;
forn(i, 1, T)fa[curr][i] = fa[fa[curr][i - 1]][i - 1];
dfn[++cnt] = curr;
s[curr] = cnt;
for (const auto nxt : child[curr])if (nxt != parent)dfs(nxt, curr);
e[curr] = cnt;
}
inline int lca(int x, int y)
{
if (deep[x] < deep[y])swap(x, y);
forv(i, T, 0)if (deep[fa[x][i]] >= deep[y])x = fa[x][i];
if (x == y)return x;
forv(i, T, 0)if (fa[x][i] != fa[y][i])x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
inline int LCA(const int x, const int y)
{
const int t1 = lca(root, x), t2 = lca(root, y), t3 = lca(x, y);
const int maxDeep = max({deep[t1], deep[t2], deep[t3]});
if (maxDeep == deep[t1])return t1;
if (maxDeep == deep[t2])return t2;
return t3;
}
inline int top(int x, int k)
{
while (k)
{
const int step = log2(k);
x = fa[x][step];
k -= 1 << step;
}
return x;
}
inline void Add(const int curr, const int val)
{
if (curr == root)seg.Add(1, 1, n, val);
else if (s[curr] <= s[root] and e[root] <= e[curr])
{
const int del = top(root, deep[root] - deep[curr] - 1);
seg.Add(1, 1, n, val), seg.Add(1, s[del], e[del], -val);
}
else seg.Add(1, s[curr], e[curr], val);
}
inline ll Query(const int curr)
{
if (curr == root)return seg.Query(1, 1, n);
if (s[curr] <= s[root] and e[root] <= e[curr])
{
const int del = top(root, deep[root] - deep[curr] - 1);
return seg.Query(1, 1, n) - seg.Query(1, s[del], e[del]);
}
return seg.Query(1, s[curr], e[curr]);
}
inline void solve()
{
cin >> n >> q;
forn(i, 1, n)cin >> a[i];
forn(i, 1, n-1)
{
int u, v;
cin >> u >> v;
child[u].push_back(v), child[v].push_back(u);
}
dfs(1, 0);
seg.build(1);
while (q--)
{
int op;
cin >> op;
if (op == 1)cin >> root;
else if (op == 2)
{
int u, v, val;
cin >> u >> v >> val;
Add(LCA(u, v), val);
}
else
{
int curr;
cin >> curr;
cout << Query(curr) << endl;
}
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
解法二
参照朋友的 博客 的换根树剖。其实主要是讲讲 \(top\) 咋求,就是一条路径上的倒数第二个点。
-
如果 \(root\) 和 \(lca\) 已经在同一条重链上了,显然直接返回 \(lca\) 的重儿子 \(son\)。
-
不在同一条链让 \(root\) 跳到 \(lca\) 所在重链下面的一条重链的 \(top\) 上。
-
基于第二天,答案要么为 \(lca\) 的重儿子,要么即为下面一条重链的 \(top\),主要看 \(fa[root]=lca\),相同显然即为 \(root\) (跳的结果),否则为 \(son\)。
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
// #define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 1e5 + 10;
constexpr int T = 20;
int deep[N], son[N], siz[N], fa[N];
vector<int> child[N];
int n, q;
inline void dfs1(const int curr, const int parent)
{
deep[curr] = deep[fa[curr] = parent] + 1;
siz[curr] = 1;
for (const auto nxt : child[curr])
{
if (nxt == parent)continue;
dfs1(nxt, curr);
siz[curr] += siz[nxt];
if (siz[nxt] > siz[son[curr]])son[curr] = nxt;
}
}
int idx[N], s[N], e[N], top[N], cnt;
inline void dfs2(const int curr, const int root)
{
idx[++cnt] = curr, s[curr] = cnt, e[curr] = s[curr] + siz[curr] - 1;
top[curr] = root;
if (son[curr])dfs2(son[curr], root);
for (const auto nxt : child[curr])if (nxt != fa[curr] and nxt != son[curr])dfs2(nxt, nxt);
}
int a[N], root = 1;
struct
{
struct Node
{
ll sum, add, len;
} node[N << 2];
#define len(x) node[x].len
#define add(x) node[x].add
#define sum(x) node[x].sum
void Add(const int curr, const ll val)
{
add(curr) += val;
sum(curr) += len(curr) * val;
}
void pushDown(const int curr)
{
if (add(curr))
{
Add(ls(curr),add(curr)), Add(rs(curr),add(curr));
add(curr) = 0;
}
}
void pushUp(const int curr)
{
sum(curr) = sum(ls(curr)) + sum(rs(curr));
}
void build(const int curr, const int l = 1, const int r = n)
{
len(curr) = r - l + 1;
const int mid = l + r >> 1;
if (l == r)
{
sum(curr) = a[idx[l]];
return;
}
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
pushUp(curr);
}
void Add(const int curr, const int l, const int r, const int val, const int s = 1, const int e = n)
{
if (l <= s and e <= r)
{
Add(curr, val);
return;
}
const int mid = s + e >> 1;
pushDown(curr);
if (l <= mid)Add(ls(curr), l, r, val, s, mid);
if (r > mid)Add(rs(curr), l, r, val, mid + 1, e);
pushUp(curr);
}
ll Query(const int curr, const int l, const int r, const int s = 1, const int e = n)
{
if (l <= s and e <= r)return sum(curr);
pushDown(curr);
const int mid = s + e >> 1;
ll ans = 0;
if (l <= mid)ans += Query(ls(curr), l, r, s, mid);
if (r > mid)ans += Query(rs(curr), l, r, mid + 1, e);
return ans;
}
} seg;
inline int lca(int x, int y)
{
while (top[x] != top[y])
{
if (deep[top[x]] < deep[top[y]])swap(x, y);
x = fa[top[x]];
}
if (deep[x] > deep[y])swap(x, y);
return x;
}
inline int LCA(const int x, const int y)
{
const int t1 = lca(root, x), t2 = lca(root, y), t3 = lca(x, y);
const int maxDeep = max({deep[t1], deep[t2], deep[t3]});
if (maxDeep == deep[t1])return t1;
if (maxDeep == deep[t2])return t2;
return t3;
}
//从root到curr的路径上最后一个点
inline int TreeTop(const int curr, int x = root)
{
if (top[curr] == top[x])return son[curr];
while (top[fa[top[x]]] != top[curr])x = fa[top[x]];
x = top[x];
if (fa[x] != curr)x = son[curr];
return x;
}
inline bool sameTree(const int curr)
{
return s[curr] <= s[root] and e[root] <= e[curr];
}
inline void Add(const int curr, const int val)
{
if (curr == root)seg.Add(1, 1, n, val);
else if (sameTree(curr))
{
const int del = TreeTop(curr);
seg.Add(1, 1, n, val), seg.Add(1, s[del], e[del], -val);
}
else seg.Add(1, s[curr], e[curr], val);
}
inline ll Query(const int curr)
{
if (curr == root)return seg.Query(1, 1, n);
if (sameTree(curr))
{
const int del = TreeTop(curr);
return seg.Query(1, 1, n) - seg.Query(1, s[del], e[del]);
}
return seg.Query(1, s[curr], e[curr]);
}
inline void solve()
{
cin >> n >> q;
forn(i, 1, n)cin >> a[i];
forn(i, 1, n-1)
{
int u, v;
cin >> u >> v;
child[u].push_back(v), child[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 1);
seg.build(1);
while (q--)
{
int op;
cin >> op;
if (op == 1)cin >> root;
else if (op == 2)
{
int u, v, val;
cin >> u >> v >> val;
Add(LCA(u, v), val);
}
else
{
int curr;
cin >> curr;
cout << Query(curr) << endl;
}
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
如果要用 \(LCT\) 做子树操作是较为困难的,当然也能做,具体的每个点同时维护虚子树信息,但这些信息不能单纯地使用懒标记维护,需要维护一个标记永久化的虚子树全局加标记,在虚实变化时通过这个标记来更新真实的虚实子树和总信息,细节较多,后续写了再补代码。当然 \(ETT\) 和 \(Top Tree\) 应该也是完全能做的。
标签:curr,int,题解,Jamie,return,CF916E,lca,const,root From: https://www.cnblogs.com/Athanasy/p/18029516