BZOJ 3991 寻宝游戏

Link

Solution

可以发现最短路程就是连接所有的宝物的一棵生成树的边权和×2\times 2 修改一个节点只会影响DFS序在它两边的点,可以用Treap维护。 然后就可做了。

Tips

看出了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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//Code by Lucida
#include<bits/stdc++.h>
#define get(x) scanf("%d",&x)
//#define debug(x) std::cout<<#x<<'='<<x<<std::endl
template <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}
template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}
const int MAXN=100000+10,LOG=30,INF=0x1f1f1f1f;
typedef long long LL;
using std::swap;
using std::pair;
using std::make_pair;
struct Edge{int to,v,pre;}e[MAXN<<1];int id[MAXN],ec;
void Adde(int f,int t,int v)
{
e[++ec].to=t;e[ec].v=v;e[ec].pre=id[f];id[f]=ec;
e[++ec].to=f;e[ec].v=v;e[ec].pre=id[t];id[t]=ec;
}
LL dist[MAXN];int dfn[MAXN];
namespace STLCA
{
int dc,dep[MAXN],dfs[MAXN<<1],li[MAXN],ro[MAXN],dfndc;
void DFS(int pos)
{
dfn[pos]=++dfndc;
dfs[li[pos]=ro[pos]=++dc]=pos;
for(int i=id[pos];i;i=e[i].pre)
{
int u=e[i].to;
if(dep[u]) continue;
dep[u]=dep[pos]+1;dist[u]=dist[pos]+e[i].v;
DFS(u);dfs[++dc]=pos;ro[pos]=ro[u];
}

}
int f[MAXN<<1][LOG],log_2[MAXN<<1];
void ST()
{
log_2[0]=-1;
for(int i=1;i<=dc;i++)
{
f[i][0]=dfs[i];
log_2[i]=log_2[i>>1]+1;
}
for(int j=1;j<=log_2[dc];j++)
for(int i=1;i<=dc-(1<<j)+1;i++)//j++
{
int pl=f[i][j-1],pr=f[i+(1<<j>>1)][j-1];
f[i][j]=dep[pl]<dep[pr]?pl:pr;
}
}
void Init()
{
dep[1]=1;DFS(1);ST();
}
int LCA(int p1,int p2)
{
p1=li[p1],p2=li[p2];
if(p1>p2) swap(p1,p2);
int gol=log_2[p2-p1+1],pl=f[p1][gol],pr=f[p2-(1<<gol)+1][gol];
return dep[pl]<dep[pr]?pl:pr;
}
}
LL Dis(int p1,int p2)
{
using namespace STLCA;
if(!p1 || !p2) return 0;
return dist[p1]+dist[p2]-dist[LCA(p1,p2)]*2;
}
const int P=1e5+7;
struct Treap
{
struct Node
{
Node *son[2];
int py,sz;pair<int,int> v;
void up(){sz=son[0]->sz+son[1]->sz+1;}
}*null,*root;
Treap()
{
null=new Node();
null->son[0]=null->son[1]=null;
null->py=P;null->sz=0;
root=null;
}
int size(){return root->sz;}
Node *newNode(pair<int,int> v)
{
static Node *ME=new Node[MAXN],*cur;
cur=ME++;cur->v=v;cur->py=rand()%P;
cur->sz=1;cur->son[0]=cur->son[1]=null;
return cur;
}
void trans(Node *&pos,int d)
{
Node *s=pos->son[d];
pos->son[d]=s->son[!d];
s->son[!d]=pos;pos->up(),s->up();
pos=s;
}
int adjust(Node *&pos)
{
int d=pos->son[0]->py>pos->son[1]->py;
if(pos->py>pos->son[d]->py)
{
trans(pos,d);
return !d;
}
else return -1;
//return -1;
}
void Insert(Node *&pos,pair<int,int> v)
{
if(pos==null)
pos=newNode(v);
else if(pos->v==v) assert(1);
else
{
int jud=pos->v<v;
Insert(pos->son[jud],v),pos->up();
adjust(pos);
}
}
void Insert(pair<int,int> v){Insert(root,v);}
void DelNode(Node *&pos)
{
int d=adjust(pos);
if(~d)
DelNode(pos->son[d]),pos->up();
else
pos=null;
}
void Delete(Node *&pos,pair<int,int> v)
{
if(pos->v==v)
{
pos->py=P;
DelNode(pos);//,pos->up();
}
else
{
int jud=pos->v<v;
Delete(pos->son[jud],v),pos->up();//无需adjust.?
}
}
void Delete(pair<int,int> v){Delete(root,v);}
pair<int,int> Pred(pair<int,int> v)
{
Node *pos=root;
pair<int,int> res=make_pair(-INF,0);
while(pos!=null)
{
if(pos->v<v)
{
chkmx(res,pos->v);
pos=pos->son[1];
}
else
pos=pos->son[0];
}
return res;
}
pair<int,int> Succ(pair<int,int> v)
{
Node *pos=root;
pair<int,int> res=make_pair(INF,0);
while(pos!=null)
{
if(pos->v>v)
{
chkmn(res,pos->v);
pos=pos->son[0];
}
else
pos=pos->son[1];
}
return res;
}
pair<int,int> First()
{
Node *pos=root;
while(pos->son[0]!=null) pos=pos->son[0];
return pos->v;
}
pair<int,int> Last()
{
Node *pos=root;
while(pos->son[1]!=null) pos=pos->son[1];
return pos->v;
}
}S;
bool treasured[MAXN];
LL Adjust(int pos)
{
static LL res=0;
treasured[pos]^=1;
pair<int,int> cur=make_pair(dfn[pos],pos);
int pred=S.Pred(cur).second,succ=S.Succ(cur).second;
if(!treasured[pos])//delete
{
res-=Dis(pred,pos);
res-=Dis(succ,pos);
res+=Dis(pred,succ);
S.Delete(cur);
}
else//insert
{
res+=Dis(pred,pos);
res+=Dis(succ,pos);
res-=Dis(pred,succ);
S.Insert(cur);
}
return res+Dis(S.First().second,S.Last().second);
//当问题被自己转化地做不了 要回头试试
}
int main()
{
//freopen("input","r",stdin);
srand(0x1f1f1f1f);
int n,m;get(n),get(m);
for(int i=1;i<=n-1;i++)
{
int x,y,z;get(x),get(y),get(z);
Adde(x,y,z);
}
STLCA::Init();
for(int i=1;i<=m;i++)
{
int t;get(t);
printf("%lld\n",Adjust(t));
}
return 0;
}
/* AC Record(Bugs) *
* WA null有了sz..一定要注意null会不会被pushup
* * * * * * * * * */