BZOJ 3779 重组病毒 ——LCT 线段树
发现操作一很像一个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 线段树的更多相关文章
- BZOJ 3779 重组病毒 LCT+线段树(维护DFS序)
原题干(由于是权限题我就直接砸出原题干了,要看题意概述的话在下面): Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力 ...
- bzoj 3779: 重组病毒 LCT+线段树+倍增
题目: 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. 实验在一个封闭 ...
- bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...
- bzoj 3779 重组病毒——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- BZOJ 3779: 重组病毒(线段树+lct+树剖)
题面 escription 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病 ...
- bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论
题目大意 1.将x到当前根路径上的所有点染成一种新的颜色: 2.将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根: 3.查询以x为根的子树中所有点权值的平均值. 分析 原题codec ...
- bzoj 3779: 重组病毒
一道好题~~ 一个点到根传染需要的时间是这段路径上不同颜色的数目,一个点子树到根平均传染时间就是加权平均数了(好像是废话). 所以只要用线段树维护dfs序就这个可以了,换根的话一个点的子树要么在dfs ...
- bzoj 3779: 重组病毒【LCT+线段树维护dfs序】
%.8lf会WA!!%.8lf会WA!!%.8lf会WA!!要%.10lf!! 和4817有点像,但是更复杂. 首先对于操作一"在编号为x的计算机中植入病毒的一个新变种,在植入一个新变种时, ...
随机推荐
- c#网站对服务器的数据库的备份与还原
在网站上备份数据库 1.前台页面 <div> 需要备份的文件名:<asp:TextBox ID="bfFile" runat="server" ...
- 洛谷 P1330 封锁阳光大学
题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...
- JavaScript(appendChild添加节点)
首先,我们有一个编辑器,有一个简单的HTML页面,页面的级别分别 --> html -->head[title,meta,script,link] -- body,然后再新建一个inde ...
- javascript“类”与继承总结和回顾
Javascipt语法不支持"类"(class)[es6已经支持],但是有模拟类的方法.今天我主要谈谈Javascipt中模拟“类”的方法及js中继承的总结和回顾. js中实现“类 ...
- css--背景和列表
背景 背景样式: background-color 设置元素的背景颜色 background-image 把图像设置为背景 background-repeat 设置背景图像是否重复及如 ...
- spring源码学习之容器的基本实现
最近想拿出一部分时间来学习一下spring的源码,还特意买了一本书结合来看,当然主要是学习并跟着作者的思路来踏上学习spring的源码的道路,特意在此记录一下,<spring源码深度解析> ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- (46)zabbix报警媒介:Jabber
Jabber有第三方插件,能让Jabber用户和MSN.YahooMessager.ICQ等IM用户相互通讯.因为Google遵从Jabber协议,并且Google已经将Gtalk的服务器开放给了其它 ...
- day20-python之装饰器
1.装饰器 #!/usr/bin/env python # -*- coding:utf-8 -*- import time def cal(l): start_time=time.time() re ...
- python-格式化输出(考试必考)
Python与用户交互 如何交互 我们使用input()方法来与用户交互,但是无论我们输入的值是数字类型.字符串类型.列表类型,input的接收值都是字符串类型. name = input('请输入你 ...