Description
- 一棵樹上有n個節點,編號分別爲1到n,每個節點都有一個權值w。我們將以下面的形式來要求你對這棵樹完成一些操作: I. CHANGE u t : 把結點u的權值改爲t II. QMAX u v: 詢問從點u到點v的路徑上的節點的最大權值 III. QSUM u v: 詢問從點u到點v的路徑上的節點的權值和 注意:從點u到點v的路徑上的節點包括u和v本身
Input
- 輸入的第一行爲一個整數n,表示節點的個數。接下來n – 1行,每行2個整數a和b,表示節點a和節點b之間有一條邊相連。接下來n行,每行一個整數,第i行的整數wi表示節點i的權值。接下來1行,爲一個整數q,表示操作的總數。接下來q行,每行一個操作,以“CHANGE u t”或者“QMAX u v”或者“QSUM u v”的形式給出。 對於100%的數據,保證1<=n<=30000,0<=q<=200000;中途操作中保證每個節點的權值w在-30000到30000之間。
Output
- 對於每個“QMAX”或者“QSUM”的操作,每行輸出一個整數表示要求輸出的結果。
Sample Input
- 4
1 2
2 3
4 1
4 2 1 3
12
QMAX 3 4
QMAX 3 3
QMAX 3 2
QMAX 2 3
QSUM 3 4
QSUM 2 1
CHANGE 1 5
QMAX 3 4
CHANGE 3 6
QMAX 3 4
QMAX 2 4
QSUM 3 4
Sample Output*
- 4
1
2
2
10
6
5
6
5
16
題解
先說下樹鏈剖分。
關於幾個概念:
- 重兒子:節點
u 的重兒子就是u 的子節點中size[v] 最大的 - 輕兒子:除了重兒子以外的其他兒子
- 重邊:節點與其重兒子相連的邊
- 輕邊:節點與其輕兒子相連的邊
- 重鏈:重邊組成的路徑
- 輕鏈:輕邊
樹鏈剖分就是把對樹的操作分解爲對幾條重鏈的操作。
需要注意單獨一個節點也算重鏈。
如下圖:
樹中所有的重鏈:
1−4−9−13−14 2−6−11 3−7 10 8 12 5
樹鏈剖分需要藉助的幾個數組:
size[v]: 節點v與v的子樹的大小deep[v]: 節點v的深度max_son[v]: v的重兒子的編號mark[v]: 對樹重編號後v的編號(什麼是重編號下面會說)top[v]: 節點v所在的重鏈中深度最小的節點
步驟:
- 第一遍dfs,計算出
size,deep,max_son 數組,順便記錄下dfs序
(求LCA要用到)。 - 根據dfs序預處理出ST表(這些都是因爲樹上的操作往往需要求LCA,比如求點
u,v 的距離,就是從u 到lca的距離加上v 到lca的距離)。 - 第二遍dfs,對樹重編號以及求
top 數組。本次dfs 優先走重邊,每到一個節點mark[v] = ++num
。這樣可以看出,一條重鏈上的節點的mark值一定是連續的。 - 此時問題簡單多了,前面說過,樹鏈剖分就是把對樹的操作轉化爲對幾條重鏈的操作,那麼如何維護一條重鏈呢?
很顯然,因爲一條重鏈上的mark值都是連續的,所以可以用線段樹或splay維護。
再來說下本題
樹鏈剖分的大概思路已經明白了,那麼如何求
- 先找出
u,v 的lca。接下來以求u 到lca 的距離爲例:
- 求
u 和top[u] 的距離(u 和top[u] 在一條重鏈上,所以線段樹直接可以求)。 u=fa[top[u]] ,重複以上步驟直到deep[u]<deep[lca] 。
- 求
至此本題解決。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <climits>
#include <vector>
#define lch rt << 1
#define rch rt << 1 | 1
#define lson l, mid, lch
#define rson mid + 1, r, rch
using namespace std;
const int MAXN = 30005;
int n, q;
int value[MAXN];
vector<int> edges[MAXN];
int tot = 0;
int dfsver[MAXN << 1], first[MAXN];
int ST[MAXN << 1][20];
int siz[MAXN], deep[MAXN], max_son[MAXN];
int mark[MAXN], top[MAXN];
int fa[MAXN];
void dfs_size(int x, int pre, int dep) {
dfsver[++tot] = x;
first[x] = tot;
deep[x] = dep;
max_son[x] = 0;
siz[x] = 1;
for (int i = 0; i < edges[x].size(); i++) {
int nex = edges[x][i];
if (nex == pre) continue;
dfs_size(nex, x, dep + 1);
dfsver[++tot] = x;
fa[nex] = x;
siz[x] += siz[nex];
if (!max_son[x] || siz[nex] > siz[max_son[x]])
max_son[x] = nex;
}
}
int _num = 0;
void dfs_remark(int x, int pre, int topx) {
top[x] = topx;
mark[x] = ++_num;
if (max_son[x]) dfs_remark(max_son[x], x, topx);
for (int i = 0; i < edges[x].size(); i++) {
int nex = edges[x][i];
if (nex == pre) continue;
if (max_son[x] != nex)
dfs_remark(nex, x, nex);
}
}
void RMQ() {
for (int i = 1; i <= tot; i++)
ST[i][0] = dfsver[i];
for (int j = 1; (1 << j) <= tot; j++) {
for (int i = 1; i + (1 << j) - 1 < tot; i++) {
int a = ST[i][j - 1], b = ST[i + (1 << j - 1)][j - 1];
ST[i][j] = deep[a] < deep[b] ? a : b;
}
}
}
int LCA(int a, int b) {
int x = first[a], y = first[b];
if (x > y) swap(x, y);
int k = int(log(y - x + 1) / log(2));
int l = ST[x][k], r = ST[y - (1 << k) + 1][k];
int res = deep[l] < deep[r] ? l : r;
return res;
}
int sum[MAXN << 2], mx[MAXN << 2];
void push_up(int rt) { sum[rt] = sum[lch] + sum[rch]; mx[rt] = max(mx[lch], mx[rch]); }
void build_tree(int l, int r, int rt) {
int mid = l + r >> 1;
sum[rt] = 0;
mx[rt] = INT_MIN;
if (l == r) return;
build_tree(lson);
build_tree(rson);
}
void insert(int l, int r, int rt, int id, int val) {
if (l == r) {
sum[rt] = mx[rt] = val;
return;
}
int mid = l + r >> 1;
if (id <= mid) insert(lson, id, val);
else insert(rson, id, val);
push_up(rt);
}
struct Node{
int sum, mx;
Node() {}
Node(int a, int b) : sum(a), mx(b) {}
};
Node query(int l, int r, int rt, int L, int R) {
if (L <= l && R >= r)
return Node(sum[rt], mx[rt]);
int mid = l + r >> 1;
Node t;
int sum = 0, mx = INT_MIN;
if (L <= mid) {
t = query(lson, L, R);
sum += t.sum;
mx = max(mx, t.mx);
}
if (mid < R) {
t = query(rson, L, R);
sum += t.sum;
mx = max(mx, t.mx);
};
t = Node(sum, mx);
return t;
}
int main() {
scanf("%d", &n);
int a, b;
for (int i = 1; i < n; i++) {
scanf("%d %d", &a, &b);
edges[a].push_back(b);
edges[b].push_back(a);
}
for (int i = 1; i <= n; i++)
scanf("%d", &value[i]);
scanf("%d", &q);
dfs_size(1, 1, 1);
RMQ();
dfs_remark(1, 1, 1);
build_tree(1, n, 1);
for (int i = 1; i <= n; i++) insert(1, n, 1, mark[i], value[i]);
char op[8];
int x, y;
for (int i = 0; i < q; i++) {
scanf("%s", op);
scanf("%d %d", &x, &y);
if (op[0] == 'C') insert(1, n, 1, mark[x], y);
else {
int lca = LCA(x, y);
int sum = 0;
int mx = INT_MIN;
while (x && deep[top[x]] >= deep[lca]) {
Node t = query(1, n, 1, mark[top[x]], mark[x]);
sum += t.sum;
mx = max(mx, t.mx);
x = fa[top[x]];
}
if (x && deep[x] >= deep[lca]) {
Node t = query(1, n, 1, mark[lca], mark[x]);
sum += t.sum;
mx = max(mx, t.mx);
}
while (y && deep[top[y]] >= deep[lca]) {
Node t = query(1, n, 1, mark[top[y]], mark[y]);
sum += t.sum;
mx = max(mx, t.mx);
y = fa[top[y]];
}
if (y && deep[y] >= deep[lca]) {
Node t = query(1, n, 1, mark[lca], mark[y]);
sum += t.sum;
mx = max(mx, t.mx);
}
sum -= query(1, n, 1, mark[lca], mark[lca]).sum;
if (op[1] == 'M') printf("%d\n", mx);
else printf("%d\n", sum);
}
}
return 0;
}
然後再順便說一下LCT
本題用LCT算是相當好寫了。。。
我一開始用樹剖是因爲當時還不會LCT。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <climits>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 30005;
int n, m;
vector<int> edges[MAXN];
int rev[MAXN], fa[MAXN], tr[MAXN][2];
int q[MAXN], top = 0;
int val[MAXN], mx[MAXN], sum[MAXN];
void dfs_fa(int x) {
for (int i = 0; i < edges[x].size(); i++) {
if (edges[x][i] != fa[x]) {
fa[edges[x][i]] = x;
dfs_fa(edges[x][i]);
}
}
}
void pushup(int x) {
int l = tr[x][0], r = tr[x][1];
mx[x] = max(max(mx[l], mx[r]), val[x]);
sum[x] = sum[l] + sum[r] + val[x];
}
void pushdown(int x) {
int l = tr[x][0], r = tr[x][1];
if (rev[x]) {
rev[x] ^= 1; rev[l] ^= 1; rev[r] ^= 1;
swap(tr[x][0], tr[x][1]);
}
}
bool isroot(int x) {
return tr[fa[x]][0] != x && tr[fa[x]][1] != x;
}
void rotate(int x) {
int y = fa[x], z = fa[y];
int l, r;
if (tr[y][0] == x) l = 0;
else l = 1;
r = l ^ 1;
if (!isroot(y)) {
if (tr[z][0] == y) tr[z][0] = x;
else tr[z][1] = x;
}
fa[x] = z;
fa[y] = x;
fa[tr[x][r]] = y;
tr[y][l] = tr[x][r];
tr[x][r] = y;
pushup(y);
pushup(x);
}
void splay(int x) {
top = 0;
q[++top] = x;
for (int i = x; !isroot(i); i = fa[i])
q[++top] = fa[i];
while (top) pushdown(q[top--]);
while (!isroot(x)) {
int y = fa[x], z = fa[y];
if (!isroot(y)) {
if (tr[z][0] == y ^ tr[y][0] == x) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x) {
for (int t = 0; x; t = x, x = fa[x])
splay(x), tr[x][1] = t, pushup(x);
}
void makeroot(int x) {
access(x);
splay(x);
rev[x] ^= 1;
}
void change(int x, int t) {
access(x);
splay(x);
val[x] = t;
pushup(x);
}
void query_max(int x, int y) {
makeroot(x);
access(y);
splay(y);
printf("%d\n", mx[y]);
}
void query_sum(int x, int y) {
makeroot(x);
access(y);
splay(y);
printf("%d\n", sum[y]);
}
int main() {
scanf("%d", &n);
mx[0] = -INF;
int x, y;
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
dfs_fa(1);
for (int i = 1; i <= n; i++) {
scanf("%d", &val[i]);
sum[i] = mx[i] = val[i];
}
scanf("%d", &m);
char op[8];
for (int i = 0; i < m; i++) {
scanf("%s", op);
scanf("%d %d", &x, &y);
if (op[0] == 'C') change(x, y);
else if (op[1] == 'M') query_max(x, y);
else query_sum(x, y);
}
return 0;
}