版本比较
[BZOJ1095][ZJOI2007]捉迷藏 & Query on a tree IV(树链剖分)
正文
增加 59 行删除 59 行
11233456789101112131415161718192021222324252627282989101112131415161718192021222324252627282930313233343536373839404142434432333435363738394041424344454647483 行未改动5253545556565758596012 行未改动7374757677777879808121 行未改动1031041051061071081091071081091101111121111121131141151163 行未改动1201211221231241251241251261271281271281291301311321321331341341351361371361371381391401416 行未改动14814915015115215215315415515610 行未改动1671681691701711711721731741756 行未改动18218318418518618718818618718818919019119219319419519619719719819919920020120220316 行未改动[Query on a tree IV](https://www.luogu.com.cn/problem/SP2666)和这题基本是一样的,大家可以去交一下 [Query on a tree IV](https://www.luogu.com.cn/problem/SP2666)和这题基本是一样的,大家可以去交一下
首先,我们求出树的重链,然后**对于每一条链,建一颗线段树** 首先,我们求出树的重链,然后**对于每一条链,建一颗线段树**树大概长这样:(其中用红边连起来的是一条条重链)
**在线段树上,我们维护**: **Opt(u):经过 u节点代表的链的其中一段 的两个白点间的最长路径长度 MaxL(u):u节点代表的链的左端点到最远的白点的距离 MaxR(u):u节点代表的链的右端点到最远的白点的距离** 怎么维护呢? 我们再定义一些辅助变量方便描述: **D(i):节点i到最远的白点的距离 D2(i):节点i到次远的白点的距离 Dist(x,y):节点x,y之间的距离 Lc:线段树上左儿子 Rc:线段树上右儿子** 当 l==r 即链只有一个节点时: **若u为黑色: MaxL(u)=MaxR(u)=D(L) Opt(u)=D(L)+D2(L) 若u为白色: MaxL(u)=MaxR(u)=Max{D(L),0} Opt(u)=Max{D(L)+D2(L),D(L)}** 然后考虑如何push_up: **MaxL(u)=Max{MaxL(Lc),Dist(L,mid+1)+MaxL(Rc)} MaxR(u)=Max{MaxR(Rc),Dist(mid,R)+MaxR(Lc)} Opt(u)=Max{Opt(Lc),Opt(Rc),MaxR(Lc)+MaxL(Rc)+Dist(mid,mid+1)}** **在线段树上,我们维护**:**Opt(u):经过 u节点代表的链的其中一段 的两个白点间的最长路径长度MaxL(u):u节点代表的链的左端点到最远的白点的距离MaxR(u):u节点代表的链的右端点到最远的白点的距离**怎么维护呢?我们再定义一些辅助变量方便描述:**D(i):节点i到最远的白点的距离D2(i):节点i到次远的白点的距离Dist(x,y):节点x,y之间的距离Lc:线段树上左儿子Rc:线段树上右儿子**当 l==r 即链只有一个节点时:**若u为黑色:MaxL(u)=MaxR(u)=D(L)Opt(u)=D(L)+D2(L) 若u为白色: MaxL(u)=MaxR(u)=Max{D(L),0}Opt(u)=Max{D(L)+D2(L),D(L)}**然后考虑如何push_up:**MaxL(u)=Max{MaxL(Lc),Dist(L,mid+1)+MaxL(Rc)}MaxR(u)=Max{MaxR(Rc),Dist(mid,R)+MaxR(Lc)}Opt(u)=Max{Opt(Lc),Opt(Rc),MaxR(Lc)+MaxL(Rc)+Dist(mid,mid+1)}**这样我们就维护好线段树啦,接下来**考虑怎么在树上修改一个点的颜色**:假设我们要修改黄色点的颜色,那么我们肯定要修改被黄色笔框住的这条链 然后蓝色点肯定也被影响了,所以我们接下来修改被蓝色笔框住的这条链 再接下来绿色点也被影响力,所以我们修改被绿色笔框住的这条链 而下面已经没有点被影响了,修改结束 可见要修改一个点,我们只需要**一层层链跳上去**直到**当前的链头无父节点**为止 然后,我们还要考虑一下**如何维护D(i)和D2(i)** (节点到最远和次远白点的距离): 对此,我们**对每个点维护一个大根堆**,记录这个点到每个白点的距离 **D(i)=s.top(); s.pop(); D2(i)=s.top(); s.push(D(i));** 这样就可以求出D(i)和D2(i)啦 最后,**在全局用个堆维护每条链的Opt**,就可以直接查询了 假设我们要修改黄色点的颜色,那么我们肯定要修改被黄色笔框住的这条链然后蓝色点肯定也被影响了,所以我们接下来修改被蓝色笔框住的这条链再接下来绿色点也被影响力,所以我们修改被绿色笔框住的这条链而下面已经没有点被影响了,修改结束可见要修改一个点,我们只需要**一层层链跳上去**直到**当前的链头无父节点**为止然后,我们还要考虑一下**如何维护D(i)和D2(i)** (节点到最远和次远白点的距离):对此,我们**对每个点维护一个大根堆**,记录这个点到每个白点的距离**D(i)=s.top();s.pop();D2(i)=s.top();s.push(D(i));**这样就可以求出D(i)和D2(i)啦最后,**在全局用个堆维护每条链的Opt**,就可以直接查询了这是Query on a tree IV的代码:```cpp#include<iostream>#include<cstdio>3 行未改动const int maxn=100005;const int inf=1e9;struct work{ priority_queue <int> f,g; inline void ins(int v) { if(v != -inf) f.push(v); } inline void ins(int v) { if(v != -inf) f.push(v); } inline void era(int v) { if(v != -inf) g.push(v); } inline int top() { while(1){ if(f.empty()) return -inf;12 行未改动int wn,n,m,head[maxn],cnt,col[maxn],rt[maxn],onp;int sz[maxn],fa[maxn],dep[maxn],son[maxn];int tid[maxn],ord[maxn],ind,top[maxn],len[maxn];inline int read() { int p=0,w=1; int p=0,w=1; char ch=getchar(); while(ch>'9'||ch<'0') {if(ch=='-') w = -1; ch=getchar();} while(ch>='0'&&ch<='9') p=p*10+ch-'0',ch=getchar(); return p*w;21 行未改动 int v=edge[i].v; if(v==fa[x]||v==son[x]) continue; h[x].ins(st[rt[v]].l+dep[v]-dep[x]); } int d1=h[x].top(); h[x].era(d1); int d2=h[x].top(); int d1=h[x].top(); h[x].era(d1); int d2=h[x].top(); h[x].ins(d1); st[u].l=st[u].r=max(d1,0); st[u].v=max(0,max(d1,d1+d2));//Opt(u)=Max{D(L)+D2(L),D(L)} 白色 st[u].l=st[u].r=max(d1,0); st[u].v=max(0,max(d1,d1+d2));//Opt(u)=Max{D(L)+D2(L),D(L)} 白色 return; } int mid =(l+r)>>1; build(st[u].ls,l,mid);3 行未改动void update(int u,int l,int r,int v,int s){ if(l==r){ if(v==s){ int d1=h[v].top(); h[v].era(d1); int d2 = h[v].top(); h[v].era(d1); int d2 = h[v].top(); h[v].ins(d1); if(col[v]) st[u].l=st[u].r=d1,st[u].v=d1+d2;//Opt=D(L)+D2(L) 黑色 else st[u].l=st[u].r=max(d1,0),st[u].v=max(0,max(d1,d1+d2));//Opt(u)=Max{D(L)+D2(L),D(L)} 白色 if(col[v]) st[u].l=st[u].r=d1,st[u].v=d1+d2;//Opt=D(L)+D2(L) 黑色 else st[u].l=st[u].r=max(d1,0),st[u].v=max(0,max(d1,d1+d2));//Opt(u)=Max{D(L)+D2(L),D(L)} 白色 } else{ h[v].ins(st[rt[s]].l+dep[s]-dep[v]); int d1 = h[v].top(); int d1 = h[v].top(); h[v].era(d1); int d2 = h[v].top(); int d2 = h[v].top(); h[v].ins(d1); if(col[v]) st[u].l=st[u].r=d1,st[u].v=d1+d2;//Opt=D(L)+D2(L) 黑色 else st[u].l=st[u].r=max(d1,0),st[u].v=max(0,max(d1,d1+d2));//Opt(u)=Max{D(L)+D2(L),D(L)} 白色 if(col[v]) st[u].l=st[u].r=d1,st[u].v=d1+d2;//Opt=D(L)+D2(L) 黑色 else st[u].l=st[u].r=max(d1,0),st[u].v=max(0,max(d1,d1+d2));//Opt(u)=Max{D(L)+D2(L),D(L)} 白色 } return; } int mid=(l+r)>>1;6 行未改动 for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; int w=edge[i].w; if(v==fa[u]) continue; fa[v]=u; fa[v]=u; dep[v]=dep[u]+w; dfs1(v); sz[u]+=sz[v]; if(sz[v]>sz[son[u]]) son[u]=v;10 行未改动 int v=edge[i].v; if(v==fa[u]||v==son[u]) continue; dfs2(v,v); }} }int main(){ ios::sync_with_stdio(false); memset(head,-1,sizeof(head)); wn=n=read();6 行未改动 dfs1(1); dfs2(1,1); ans.ins(0); for(int i=n;i;i--){ int u=ord[i]; if(u!=top[u]) continue; build(rt[u],tid[u],tid[u]+len[u]-1); //每一条链建一线段树 int u=ord[i]; if(u!=top[u]) continue; build(rt[u],tid[u],tid[u]+len[u]-1); //每一条链建一线段树 ans.ins(st[rt[u]].v); } m=read(); char ch; for(int i=1;i<=m;i++){ ch=getchar(); while(ch!='C'&&ch!='A') ch=getchar(); if(ch=='C'){ int x=read(); int x=read(); col[x]^= 1; if(col[x]==0) wn++; if(col[x]==0) wn++; else wn --; for(int u=x,p=u;u;u=fa[u]){ int tp=top[u]; int p1=st[rt[tp]].v,d1=st[rt[tp]].l;16 行未改动