发现操作一很像一个LCT的access的操作。

然后答案就是路径上的虚边的数量。

然后考虑维护每一个点到根节点虚边的数量,

每次断开一条偏爱路径的时候,子树的值全部+1,

连接一条偏爱路径的时候,子树的值全部-1。

然后就用线段树维护DFS序就可以了。

但是还有一个换根的操作,发现线段树不能换根,所以直接在线段树上分类讨论进行更新就可以了。

然后makeroot操作就可以换根了。

#include <map>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define maxn 200005 int data[200005]; int n,m,dfn[maxn],id[maxn],f[maxn][21],in[maxn],out[maxn];
int tim[maxn],dep[maxn]; char opt[11];int x; namespace ST{
ll sum[maxn<<3],tag[maxn<<3];
void update(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
return ;
}
void pushdown(int o,int l,int r)
{
if (tag[o]!=0)
{
int mid=l+r>>1;
sum[o<<1]+=(mid-l+1)*tag[o];
sum[o<<1|1]+=(r-mid)*tag[o];
tag[o<<1]+=tag[o];
tag[o<<1|1]+=tag[o];
tag[o]=0;
}
return ;
}
void build(int o,int l,int r)
{
if (l==r)
{
sum[o]=data[l];
tag[l]=0;
return ;
}
int mid=l+r>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
update(o);
return ;
}
void modify(int o,int l,int r,int L,int R,int f)
{
if (L<=l&&r<=R)
{
tag[o]+=f;
sum[o]+=(r-l+1)*f;
return ;
}
pushdown(o,l,r);
int mid=l+r>>1;
if (R<=mid) return modify(o<<1,l,mid,L,R,f),update(o);
else if (L>mid) return modify(o<<1|1,mid+1,r,L,R,f),update(o);
else return modify(o<<1,l,mid,L,R,f),modify(o<<1|1,mid+1,r,L,R,f),update(o);
}
ll querysum(int o,int l,int r,int L,int R)
{
if (L<=l&&r<=R) return sum[o];
pushdown(o,l,r);
int mid=l+r>>1;
if (R<=mid) return querysum(o<<1,l,mid,L,R);
else if (L>mid) return querysum(o<<1|1,mid+1,r,L,R);
else return querysum(o<<1,l,mid,L,R)+querysum(o<<1|1,mid+1,r,L,R);
}
int Second_LCA(int a,int b)
{
if (dep[a]>dep[b]) swap(a,b);
int dist=dep[b]-dep[a]-1; //printf("dist %d\n",dist);
D(i,20,0) if ((dist>>i)&1) b=f[b][i];
return b;
}
int LCA(int a,int b)
{
if (dep[a]>dep[b]) swap(a,b);
int dist=dep[b]-dep[a];
D(i,20,0) if ((dist>>i)&1) b=f[b][i];
if (a==b) return b;
D(i,20,0) if (f[b][i]!=f[a][i]) b=f[b][i],a=f[a][i];
return f[b][0];
}
double query(int rt,int o)
{
if (o==rt)
{
return (1.0*querysum(1,1,n,1,n))/(1.0*n);
}
else if (LCA(rt,o)==rt) return (querysum(1,1,n,in[o],out[o]))/(1.0*(out[o]-in[o]+1));
else if (LCA(rt,o)==o)
{
int tmp=Second_LCA(rt,o);
return (1.0*querysum(1,1,n,1,n)-1.0*querysum(1,1,n,in[tmp],out[tmp]))/(1.0*(n-out[tmp]+in[tmp]-1));
}
else
{
return (1.0*querysum(1,1,n,in[o],out[o]))/(1.0*(out[o]-in[o]+1));
}
}
void add(int rt,int o,int f)
{
if (!o) return ;
if (o==rt) return modify(1,1,n,1,n,f);
else if (LCA(rt,o)==rt) return modify(1,1,n,in[o],out[o],f);
else if (LCA(rt,o)==o)
{
int tmp=Second_LCA(rt,o);
modify(1,1,n,1,n,f);
modify(1,1,n,in[tmp],out[tmp],-f);
}
else return modify(1,1,n,in[o],out[o],f);
}
} namespace LCT{
int rev[maxn],ch[maxn][2],fa[maxn];
int sta[maxn],top,rt=1;
bool isroot(int o)
{return (ch[fa[o]][0]!=o)&&(ch[fa[o]][1]!=o);}
void pushdown(int o)
{
if (rev[o])
{
rev[o]^=1;
rev[ch[o][0]]^=1;
rev[ch[o][1]]^=1;
swap(ch[o][0],ch[o][1]);
}
}
void rot(int x)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1; r=l^1;
if (!isroot(y))
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
}
void splay(int x)
{
sta[top=1]=x;
for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
while (top) pushdown(sta[top--]); while (!isroot(x))
{
int y=fa[x];
if (!isroot(y))
{
int z=fa[y];
if (ch[y][0]==x^ch[z][0]==y) rot(x);
else rot(y);
}
rot(x);
}
}
int find(int x)
{
pushdown(x);
while (ch[x][0]) x=ch[x][0],pushdown(x);
return x;
}
void access(int x)
{
for (int t=0;x;t=x,x=fa[x])
{
splay(x);
ST::add(rt,find(ch[x][1]),1);
ch[x][1]=t;
ST::add(rt,find(t),-1);
}
}
void makeroot(int x)
{
access(x);
splay(x);
rev[x]^=1;
rt=x;
}
} namespace Graph{
int h[maxn],to[maxn],ne[maxn],en=0,tot=0;
void add(int a,int b){to[en]=b;ne[en]=h[a];h[a]=en++;}
void dfs(int o,int fa)
{
in[o]=dfn[o]=++tot; id[tot]=o; f[o][0]=fa;
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fa) dep[to[i]]=dep[o]+1,tim[to[i]]=tim[o]+1,dfs(to[i],o);
out[o]=tot;
}
} int main()
{
memset(Graph::h,-1,sizeof Graph::h);
scanf("%d%d",&n,&m);
F(i,1,n-1)
{
int a,b;
scanf("%d%d",&a,&b);
Graph::add(a,b);Graph::add(b,a);
}
tim[1]=1; Graph::dfs(1,0);
F(i,1,n) LCT::fa[i]=f[i][0];
F(i,1,20) F(j,1,n) f[j][i]=f[f[j][i-1]][i-1];
F(i,1,n) data[dfn[i]]=tim[i];
ST::build(1,1,n);
F(i,1,m)
{
scanf("%s%d",opt,&x);
switch(opt[2])
{
case 'Q': printf("%.10lf\n",ST::query(LCT::rt,x)); break;
case 'C': LCT::makeroot(x); break;
case 'L': LCT::access(x); break;
}
}
}

  

BZOJ 3779 重组病毒 ——LCT 线段树的更多相关文章

  1. BZOJ 3779 重组病毒 LCT+线段树(维护DFS序)

    原题干(由于是权限题我就直接砸出原题干了,要看题意概述的话在下面): Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力 ...

  2. bzoj 3779: 重组病毒 LCT+线段树+倍增

    题目: 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. 实验在一个封闭 ...

  3. bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...

  4. bzoj 3779 重组病毒——LCT维护子树信息

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...

  5. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  6. BZOJ 3779: 重组病毒(线段树+lct+树剖)

    题面 escription 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病 ...

  7. bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论

    题目大意 1.将x到当前根路径上的所有点染成一种新的颜色: 2.将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根: 3.查询以x为根的子树中所有点权值的平均值. 分析 原题codec ...

  8. bzoj 3779: 重组病毒

    一道好题~~ 一个点到根传染需要的时间是这段路径上不同颜色的数目,一个点子树到根平均传染时间就是加权平均数了(好像是废话). 所以只要用线段树维护dfs序就这个可以了,换根的话一个点的子树要么在dfs ...

  9. bzoj 3779: 重组病毒【LCT+线段树维护dfs序】

    %.8lf会WA!!%.8lf会WA!!%.8lf会WA!!要%.10lf!! 和4817有点像,但是更复杂. 首先对于操作一"在编号为x的计算机中植入病毒的一个新变种,在植入一个新变种时, ...

随机推荐

  1. Android 视频录制 java.lang.RuntimeException: start failed.

    //mRecorder.setVideoSize(320, 280); // mRecorder.setVideoFrameRate(5); mRecorder.setOutputFile(viodF ...

  2. Android项目中包名的修改

    通常修改包名时会造成R文件错误,并且有时带有原因不明的Manifest文件中多处文本混乱. 所以,将目前认为最为简洁方便的修改包名流程记录如下: 假设我们目前的包名为com.pepper.util,我 ...

  3. 电脑连接海信电视 HDMI

    注意:我们家的电视是海信的,所以不能代表所有的电视哦~~~ 家里电视有线电视已经过期很长时间了,早就想把电脑连接到电视上用电视做显示器的心了,今天来兴趣了,就弄了一下!!! 用电脑连接电视需要先解决两 ...

  4. 自学Spring Boot

    简介: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...

  5. Bootstrap历练实例:响应式导航栏

    响应式的导航栏 为了给导航栏添加响应式特性,您要折叠的内容必须包裹在带有 classes .collapse..navbar-collapse 的 <div> 中.折叠起来的导航栏实际上是 ...

  6. Bootstrap历练实例:小的按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  7. ios之UIPopoverController

    UIPopoverController是iPad上的iOS开发会常用到的一个组件(在iPhone设备上不允许使用),这个组件上手很简单,因为他的显示方法很少,而且参数简单,但我在使用过程中还常碰到各种 ...

  8. Clover启动mbr的win7/win8

    对以传统bios安装在mbr分区的win7/WIN8也可以使用EFI引导直接进入win.首先进win提取EFI引导文件,以管理员员身份运行cmd,输入如下命令 bcdboot c:\windows / ...

  9. 如何用纯 CSS 创作一个菱形 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKzjqK 可交互视频教 ...

  10. verilog random使用

    “$random函数调用时返回一个32位的随机数,它是一个带符号的整形数...”,并给出了一个例子: _________________________________________________ ...