发现操作一很像一个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. IT之家学院:使用CMD命令行满速下载百度云

    转自:https://www.toutiao.com/a6545305189685920259/?tt_from=android_share&utm_campaign=client_share ...

  2. 正则表达说明—Pattern API

    字符类 [abc]                              匹配a.b.c任意一个字符 [^abc]   匹配除了a.b.c外的任意一个字符 [a-zA-Z]     匹配a-z或A ...

  3. Linux if 命令判断条件总结

    Linux if 命令判断条件总结Linux if命令 关于文件属性的判断式 -a 如果文件存在 -b 如果文件存在,且该文件是区域设备文件 -c 当file存在并且是字符设备文件时返回真 -d 当p ...

  4. js 字符串常用操作

    function Class_String_Common(){ //将字符串 myString 的 start位置 和 end位置 之间的内容替换为 replaceStr this.replace1 ...

  5. 0x00000124蓝屏问题解决方法

    windows7-32位系统: 0x00000124蓝屏是系统问题,win7才有的, xp系统没有 . 解决办法:下载win7蓝屏补丁包解压安装就ok了. 说明:win7蓝屏补丁KB(25286140 ...

  6. 洛谷 P1204 [USACO1.2]挤牛奶Milking Cows

    题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开 ...

  7. Bellman-Ford与SPFA

    一.Bellman-Ford Bellman-Ford 算法是一种用于计算带权有向图中单源最短路径(当然也可以是无向图).与Dijkstra相比的优点是,也适合存在负权的图. 若存在最短路(不含负环时 ...

  8. WPF知识点全攻略06- WPF逻辑树(Logical Tree)和可视树(Visual Tree)

    介绍概念之前,先来分析一段代码: xaml代码如下: <Window x:Class="WpfApp1.MainWindow" xmlns="http://sche ...

  9. git 添加 ,密匙

    转载此处   https://blog.csdn.net/xiayiye5/article/details/79652296

  10. 关于js作用域问题详解

    执行上下文 函数表达式和函数声明 1. console.log(a); // ReferenceError: a is not defined // ReferenceError(引用错误)对象表明一 ...