0%

[BZOJ3589]动态树

题面描述

求若干条树链的并

题解

做法有很多,首先是用容斥把树链的并转化为树链的交,或者是建出虚树,利用 \(dfs\) 序虚树求值,但两者细节较多,所以直接用线段树的区间覆盖标记了 虚树的性质挺巧妙的,可能之后会整理

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
#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
return f*x;
}
const int N = 2e5+5;
struct Edge
{
int next, to;
Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[N<<2];
int tot, head[N];
void _add(int x, int y) { edge[++tot] = Edge(head[x], y); head[x] = tot; }
void add(int x, int y) { _add(x, y); _add(y, x); }
int n, T;
int size[N], son[N], top[N], fa[N], dfn[N], nfd[N], d[N], t;
void dfs1(int x, int f)
{
fa[x] = f, d[x] = d[f]+1, size[x] = 1;
for(int i = head[x]; i; i = edge[i].next)
{
int y = edge[i].to; if(y == f) continue;
dfs1(y, x); size[x] += size[y];
son[x] = size[son[x]]>size[y]?son[x]:y;
}
}
void dfs2(int x, int topf)
{
top[x] = topf, dfn[x] = ++t, nfd[t] = x;
if(son[x]) dfs2(son[x], topf);
for(int i = head[x]; i; i = edge[i].next)
{
int y = edge[i].to;
if(fa[x] == y||son[x] == y) continue;
dfs2(y, y);
}
}
int v[N<<2], a[N<<2], val[N<<2], cov[N<<2];
void pusha(int x, int l, int r, int d) { v[x] += d*(r-l+1), a[x] += d; }
void pushup(int x) { v[x] = v[x<<1]+v[x<<1|1]; val[x] = val[x<<1]+val[x<<1|1]; }
void pushc(int x, int d) { val[x] = v[x]*(d-1), cov[x] = d; }
void pushdown(int x, int l, int r)
{
int mid = (l+r)>>1, d = a[x]; a[x] = 0;
pusha(x<<1, l ,mid, d); pusha(x<<1|1, mid+1, r, d);
if(cov[x]) pushc(x<<1, cov[x]), pushc(x<<1|1, cov[x]), cov[x] = 0;
}
int query(int x, int l, int r, int ql, int qr)
{
if(ql <= l&&r <= qr) return val[x];
int mid = (l+r)>>1; int ret = 0; pushdown(x, l, r);
if(ql <= mid) ret += query(x<<1, l, mid, ql, qr);
if(qr > mid) ret += query(x<<1|1, mid+1, r, ql, qr);
return ret;
}
void add(int x, int l, int r, int ql, int qr, int d)
{
if(ql <= l&&r <= qr) return pusha(x, l, r, d);
int mid = (l+r)>>1; pushdown(x, l, r);
if(ql <= mid) add(x<<1, l, mid, ql, qr, d);
if(qr > mid) add(x<<1|1, mid+1, r, ql, qr, d);
pushup(x);
}
void change(int x, int l, int r, int ql, int qr, int d)
{
if(ql <= l&&r <= qr) return pushc(x, d);
int mid = (l+r)>>1; pushdown(x, l, r);
if(ql <= mid) change(x<<1, l, mid, ql, qr, d);
if(qr > mid) change(x<<1|1, mid+1, r, ql, qr, d);
pushup(x);
}
void color(int x, int y)
{
while(top[x] != top[y])
{
if(d[top[x]] < d[top[y]]) swap(x, y);
change(1, 1, n, dfn[top[x]], dfn[x], 2); x = fa[top[x]];
}
if(d[x] < d[y]) swap(x, y);
change(1, 1, n, dfn[y], dfn[x], 2);
}
int p[N];
bool cmp(int x, int y) { return dfn[x] < dfn[y]; }
int main()
{
n = read(); pushc(1, 1);
for(int i = 1; i < n; ++i)
{
int x = read(), y = read();
add(x, y);
}
dfs1(1, 0); dfs2(1, 1);
T = read();
while(T--)
{
int opt = read(), x, y, k, ans = 0;
if(opt == 0) x = read(), k = read(), add(1, 1, n, dfn[x], dfn[x]+size[x]-1, k);
else
{
k = read();
for(int i = 1; i <= k; ++i) x = read(), y = read(), color(x, y);
printf("%d\n", val[1]&0x7fffffff); pushc(1, 1);
}
}
return 0;
}