P3979 遥远的国度 树剖换根
传送门: https://www.luogu.com.cn/problem/P3979
题意
有 一 棵 点 数 为 n 的 树 , 每 个 点 都 有 一 个 权 值 , ( 初 始 根 不 一 定 为 1 ) , 接 下 来 m 个 操 作 : 有一棵点数为n的树,每个点都有一个权值,(初始根不一定为1),接下来m个操作: 有一棵点数为n的树,每个点都有一个权值,(初始根不一定为1),接下来m个操作:
- 1 i d , 将 根 修 改 为 i d 1\;id,将根修改为id 1id,将根修改为id
- 2 x y v a l , 将 x 到 y 的 简 单 路 径 上 所 有 点 的 权 值 修 改 为 v a l 2\;x\;y\;val,将x到y的简单路径上所有点的权值修改为val 2xyval,将x到y的简单路径上所有点的权值修改为val
- 3 x , 在 根 为 r o o t 下 , 询 问 以 x 为 根 的 子 树 的 最 小 值 。 3\;x,在根为root下,询问以x为根的子树的最小值。 3x,在根为root下,询问以x为根的子树的最小值。
思路
虽
然
树
根
不
一
定
为
1
,
但
是
我
们
轻
重
链
剖
分
,
线
段
树
维
护
序
列
,
都
按
照
以
1
为
根
来
做
。
虽然树根不一定为1,但是我们轻重链剖分,线段树维护序列,都按照以1为根来做。
虽然树根不一定为1,但是我们轻重链剖分,线段树维护序列,都按照以1为根来做。
操
作
二
我
们
还
是
一
样
,
线
段
树
修
改
x
到
y
的
简
单
路
径
上
的
权
值
。
m
o
d
i
f
y
_
c
h
a
i
n
(
x
,
y
,
v
a
l
)
。
操作二我们还是一样,线段树修改x到y的简单路径上的权值。modify \_ chain(x,y,val)。
操作二我们还是一样,线段树修改x到y的简单路径上的权值。modify_chain(x,y,val)。
关 键 是 操 作 三 , 如 果 树 根 一 直 都 是 1 的 话 , 就 是 q u e r y ( 1 , d f n [ x ] , d f n [ x ] + s i z [ x ] − 1 ) 关键是操作三,如果树根一直都是1的话,就是query(1,dfn[x],dfn[x]+siz[x]-1) 关键是操作三,如果树根一直都是1的话,就是query(1,dfn[x],dfn[x]+siz[x]−1)
但是树根一直在变!!! 所 以 这 是 一 道 树 剖 换 根 ! \red{所以这是一道树剖换根!} 所以这是一道树剖换根!
如 果 我 们 每 次 在 换 根 的 时 候 重 新 剖 分 , 那 复 杂 度 会 爆 炸 ! 所 以 不 可 能 一 直 剖 分 。 如果我们每次在换根的时候重新剖分,那复杂度会爆炸!所以不可能一直剖分。 如果我们每次在换根的时候重新剖分,那复杂度会爆炸!所以不可能一直剖分。
先 来 观 察 树 根 和 x 的 位 置 关 系 。 先来观察树根和x的位置关系。 先来观察树根和x的位置关系。
-
r o o t = = x 答 案 就 是 [ 1 , n ] 区 间 最 小 的 值 , 即 t [ 1 ] . m n root==x\;答案就是[1,n]区间最小的值,即t[1].mn root==x答案就是[1,n]区间最小的值,即t[1].mn
-
L C A ( r o o t , x ) = = x & & d e p [ r o o t ] > d e p [ x ] LCA(root,x)==x \;\&\&\; dep[root]>dep[x] LCA(root,x)==x&&dep[root]>dep[x]
这 种 情 况 要 求 x 的 子 树 中 最 小 值 , 只 需 要 去 除 L C A 的 儿 子 L C A _ s o n 的 子 树 区 域 , 剩 下 的 取 最 小 即 可 。 这种情况要求x的子树中最小值,只需要去除LCA的儿子LCA \_ son的子树区域,剩下的取最小即可。 这种情况要求x的子树中最小值,只需要去除LCA的儿子LCA_son的子树区域,剩下的取最小即可。
a n s = m i n ( q u e r y ( 1 , 1 , d f n [ L C A _ s o n ] − 1 ) , q u e r y ( 1 , d f n [ L C A _ s o n ] + s i z [ L C A _ s o n ] , n ) ) ans=min(query(1,1,dfn[LCA \_ son]-1),query(1,dfn[LCA \_ son]+siz[LCA \_ son],n)) ans=min(query(1,1,dfn[LCA_son]−1),query(1,dfn[LCA_son]+siz[LCA_son],n)) -
e l s e else else
x
不
是
r
o
o
t
的
L
C
A
,
那
么
只
需
要
查
询
x
的
子
树
即
可
。
x不是root的LCA,那么只需要查询x的子树即可。
x不是root的LCA,那么只需要查询x的子树即可。
a
n
s
=
q
u
e
r
y
(
1
,
d
f
n
[
x
]
,
d
f
n
[
x
]
+
s
i
z
[
x
]
−
1
)
ans = query(1,dfn[x],dfn[x]+siz[x]-1)
ans=query(1,dfn[x],dfn[x]+siz[x]−1)
Code(85MS)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)
const ll mod = 998244353;
// const ll mod = 1e9 + 7;
// const double eps = 1e-6;
// const double PI = acos(-1);
// const double R = 0.57721566490153286060651209;
int root;
ll w[10000005];
/*--------------前向星建边---------------*/
const int MAXN = 1e6 + 10;
struct Edge {
int v, next;
}e[MAXN << 1];
int head[MAXN << 1], cnt;
inline void add(int u, int v) {
e[++cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt;
}
/*-------------轻重链剖分----------------*/
const int N = 1e6 + 10;
int siz[N], dep[N], fa[N], son[N];
void dfs1(int u, int par) {
dep[u] = dep[fa[u] = par] + (siz[u] = 1);
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if(v == par) continue;
dfs1(v, u);
siz[u] += siz[v];
if(!son[u] || siz[v] > siz[son[u]])
son[u] = v;
}
}
int top[N], dfn[N], nodeof[N], tim;
void dfs2(int u, int topf) {
nodeof[dfn[u] = ++tim] = u;
top[u] = topf;
if(!son[u]) return ;
dfs2(son[u], topf);
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
/*------------线段树维护---------------*/
#define lc u << 1
#define rc u << 1 | 1
#define mid (t[u].l + t[u].r) / 2
struct Tree {
int l, r;
ll mn, tag;
}t[N << 2];
inline void push_up(int u) {
t[u].mn = min(t[lc].mn, t[rc].mn);
}
inline void push_down(int u) {
if(!t[u].tag) return ;
t[lc].mn = t[u].tag;
t[rc].mn = t[u].tag;
t[lc].tag = t[u].tag;
t[rc].tag = t[u].tag;
t[u].tag = 0;
}
void build(int u, int l, int r) {
t[u].l = l; t[u].r = r; t[u].tag = 0;
if(l == r) {
t[u].mn = w[nodeof[l]];
return ;
}
int m = (l + r) >> 1;
build(lc, l, m);
build(rc, m + 1, r);
push_up(u);
}
void modify(int u, int ql, int qr, ll v) {
if(ql <= t[u].l && t[u].r <= qr) {
t[u].mn = v;
t[u].tag = v;
return ;
}
push_down(u);
if(ql <= mid) modify(lc, ql, qr, v);
if(qr > mid) modify(rc, ql, qr, v);
push_up(u);
}
ll query(int u, int ql, int qr) {
if(ql <= t[u].l && t[u].r <= qr) return t[u].mn;
push_down(u);
ll ans = 1e18;
if(ql <= mid) ans = min(ans, query(lc, ql, qr));
if(qr > mid) ans = min(ans, query(rc, ql, qr));
return ans;
}
void modify_chain(int x, int y, ll v) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
modify(1, dfn[top[x]], dfn[x], v);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
modify(1, dfn[x], dfn[y], v);
}
/*-------------LCA---------------*/
int LCA_son; // 公共祖先儿子
int LCA(int x, int y) {
LCA_son = 0;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
if(fa[top[x]] == y) LCA_son = top[x];
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
if(!LCA_son) LCA_son = son[x];
return x;
}
void solve() {
int n, m;
scanf("%d%d",&n,&m);
for(int i = 1;i < n; i++) {
int u, v;
scanf("%d%d",&u,&v);
add(u, v);
add(v, u);
}
for(int i = 1;i <= n; i++) {
scanf("%lld",&w[i]);
}
dfs1(1, 0);
dfs2(1, 1);
build(1, 1, n);
scanf("%d",&root);
while(m--) {
int opt; scanf("%d",&opt);
if(opt == 1) {
int id; scanf("%d",&id);
root = id;
}
else if(opt == 2) {
int x, y; ll v; scanf("%d%d%lld",&x,&y,&v);
modify_chain(x, y, v);
}
else if(opt == 3) {
int x; scanf("%d",&x);
if(x == root)
printf("%lld\n",t[1].mn);
else if(LCA(x, root) == x && dep[root] > dep[x]) {
if(dfn[LCA_son] + siz[LCA_son] <= n)
printf("%lld\n",min(query(1, 1, dfn[LCA_son] - 1), query(1, dfn[LCA_son] + siz[LCA_son], n)));
else
printf("%lld\n",query(1, 1, dfn[LCA_son] - 1));
}
else
printf("%lld\n",query(1, dfn[x], dfn[x] + siz[x] - 1));
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
int size=40<<20;
__asm__ ("movq %0,%%rsp\n"::"r"((char*)malloc(size)+size));
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}