BZOJ 4538 网络

Link

Solution

因为询问的是不覆盖一个点的路径 那么就把修改也转化成补集,可以通过树剖DFS序解决 感觉并不是很难想,但就是想不到。。树剖DFS序看起来有无限可能。

Code

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "lucida"

using std::priority_queue;
using std::swap;
using std::sort;
using std::pair;
const int MAXN=1e5+11,MAXM=2e5+11;
struct Edge {
int to;Edge *pre;
Edge(int to,Edge *pre):to(to),pre(pre) {}
void *operator new(size_t) {
static Edge *Me=alloc(Edge,MAXN<<1);
return Me++;
}
}*G[MAXN];
void Adde(int f,int t) {
G[f]=new Edge(t,G[f]);
G[t]=new Edge(f,G[t]);
}
struct MagicHeap {
priority_queue<int> app,rmv;
void Maintain() {
while(!app.empty() && !rmv.empty() && app.top()==rmv.top()) {
app.pop();
rmv.pop();
}
}
int Top() {
Maintain();
return !app.empty()?app.top():-1;
}
void Push(int x) {
Maintain();
app.push(x);
}
void Remove(int x) {//app中必须有!
Maintain();
rmv.push(x);
}
};
namespace segtree {
struct Node {
Node *son[2];
MagicHeap heap;
Node() {
son[0]=son[1]=0;
}
void *operator new(size_t) {
static Node *Me=alloc(Node,MAXN<<2);
return Me++;
}
};
struct SegTree {
Node *root;
int L,R;
void Build(Node *&pos,int L,int R) {
pos=new Node;
if(L!=R) {
int Mid=(L+R)>>1;
Build(pos->son[0],L,Mid);
Build(pos->son[1],Mid+1,R);
}
}
void Edit(Node *pos,int L,int R,int l,int r,int v) {
if(l<=L && R<=r)
v>0?pos->heap.Push(v):pos->heap.Remove(-v);
else {
int Mid=(L+R)>>1;
if(l<=Mid) Edit(pos->son[0],L,Mid,l,r,v);
if(Mid+1<=r) Edit(pos->son[1],Mid+1,R,l,r,v);
}
}
int Access(Node *pos,int L,int R,int goal) {
int res=pos->heap.Top();
if(L!=R) {
int Mid=(L+R)>>1;
if(goal<=Mid) chkmx(res,Access(pos->son[0],L,Mid,goal));
else chkmx(res,Access(pos->son[1],Mid+1,R,goal));
}
return res;
}
SegTree() {}
SegTree(int L,int R):L(L),R(R) {
Build(root,L,R);
}
void Edit(int l,int r,int v) {//正数插入 负数删除相反数
if(l<=r) Edit(root,L,R,l,r,v);
}
int Query(int pos) {
return Access(root,L,R,pos);
}
};
}using segtree::SegTree;
namespace divide {
SegTree seg;
int cht[MAXN],dep[MAXN],sz[MAXN],fa[MAXN],prefer[MAXN],dfs[MAXN],lbd[MAXN],rbd[MAXN],dc;
void DFS(int pos) {
sz[pos]=1;dep[pos]=dep[fa[pos]]+1;
for(Edge *e=G[pos];e;e=e->pre)
if(fa[pos]!=e->to) {
int u=e->to;
fa[u]=pos;
DFS(u);
sz[pos]+=sz[u];
if(sz[u]>=sz[prefer[pos]]) prefer[pos]=u;
}
}
void Assign(int pos,int top) {
cht[pos]=top;
dfs[++dc]=pos;
lbd[pos]=dc;
if(prefer[pos]) {
Assign(prefer[pos],top);
for(Edge *e=G[pos];e;e=e->pre)
if(e->to!=fa[pos] && e->to!=prefer[pos]) {
int u=e->to;
Assign(u,u);
}
}
rbd[pos]=dc;
}
void Divide() {
DFS(1);
Assign(1,1);
new(&seg) SegTree(1,dc);
}
void CpChain(int p1,int p2,int v) {
static pair<int,int> range[MAXN];
int rc=0;
while(cht[p1]!=cht[p2]) {
if(dep[cht[p1]]<dep[cht[p2]]) swap(p1,p2);
range[++rc]=pair<int,int>(lbd[cht[p1]],lbd[p1]);
p1=fa[cht[p1]];
}
if(dep[p1]>dep[p2]) swap(p1,p2);
range[++rc]=pair<int,int>(lbd[p1],lbd[p2]);
sort(range+1,range+1+rc);
seg.Edit(1,range[1].first-1,v);
for(int i=2;i<=rc;++i)
seg.Edit(range[i-1].second+1,range[i].first-1,v);
seg.Edit(range[rc].second+1,dc,v);//没有判后面这一部分
}
int Query(int x) {
return seg.Query(lbd[x]);
}
}using namespace divide;

int main() {
// freopen("input","r",stdin);
int n,m;is>>n>>m;
for(int i=1;i<=n-1;++i) {
int x,y;is>>x>>y;
Adde(x,y);
}
Divide();
static struct {int a,b,v;}op[MAXM];
for(int i=1;i<=m;++i) {
int opt,t,x;is>>opt;
switch(opt) {
case 0:
is>>op[i].a>>op[i].b>>op[i].v;
CpChain(op[i].a,op[i].b,op[i].v);
break;
case 1:
is>>t;
CpChain(op[t].a,op[t].b,-op[t].v);
break;
case 2:
is>>x;
os<<Query(x)<<'\n';
break;
}
}
return 0;
}