bzoj 3779: 重组病毒
一道好题~~
一个点到根传染需要的时间是这段路径上不同颜色的数目,一个点子树到根平均传染时间就是加权平均数了(好像是废话)。
所以只要用线段树维护dfs序就这个可以了,换根的话一个点的子树要么在dfs序中不变,要么被截成了[1,l)和(r,n]两段(当这个点为当前root的祖先),l和r即为包含当前根的这个点的那个儿子的dfs序中的st和ed,只要分类讨论一下就可以了。
所以问题只剩什么时候在线段树上修改,我们发现1操作和LCT中的access操作很像,2操作就是make_root,每个splay就是一个相同的颜色段,那么一个点到根的距离就是LCT中虚边的个数,把虚边改实边子树-1,反过来子树+1,而LCT的复杂度是nlogn的,那直接做就好了。
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #define N 100005
- #define ll long long
- using namespace std;
- int head[N],nxt[N*],ver[N*],tot;int n,m;
- void addd(int a,int b)
- {
- tot++;nxt[tot]=head[a];head[a]=tot;ver[tot]=b;return ;
- }
- int rev[N],ch[N][],root,L[N],R[N],fa[N],faa[N];
- bool isroot(int x)
- {
- return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
- }
- void push_down(int x)
- {
- if(rev[x])
- {
- rev[x]^=;rev[ch[x][]]^=;rev[ch[x][]]^=;
- swap(L[ch[x][]],R[ch[x][]]);
- swap(L[ch[x][]],R[ch[x][]]);
- swap(ch[x][],ch[x][]);
- }
- return ;
- }
- void push_up(int x)
- {
- if(!ch[x][])L[x]=x;
- else L[x]=L[ch[x][]];
- if(!ch[x][])R[x]=x;
- else R[x]=R[ch[x][]];
- }
- void rotate(int p)
- {
- int q=fa[p],y=fa[q],x=(ch[q][]==p);
- ch[q][x]=ch[p][x^];fa[ch[q][x]]=q;
- ch[p][x^]=q;
- fa[p]=y;
- if(!isroot(q))
- {
- if(ch[y][]==q)ch[y][]=p;
- else ch[y][]=p;
- }
- fa[q]=p;
- push_up(q);
- }
- int q[N],topp;
- void splay(int x)
- {
- q[++topp]=x;
- for(int i=x;!isroot(i);i=fa[i])q[++topp]=fa[i];
- while(topp)push_down(q[topp--]);
- for(int y;!isroot(x);rotate(x))
- {
- y=fa[x];
- if(!isroot(y))
- {
- if((ch[fa[y]][]==y)^(ch[y][]==x))rotate(x);
- else rotate(y);
- }
- }
- push_up(x);
- }
- struct node
- {
- ll lazy,sum;
- }a[N*];
- int sz[N];
- int st[N],ed[N],z,jian[N],dep[N],top[N],son[N];
- int find(int x,int y)
- {
- while(top[y]!=top[x])
- {
- if(faa[top[y]]==x)return top[y];
- y=faa[top[y]];
- }
- return son[x];
- }
- void dfs(int x,int f)
- {
- st[x]=++z;jian[z]=dep[x];sz[x]=;L[x]=R[x]=x;
- for(int i=head[x];i;i=nxt[i])
- {
- if(ver[i]==f)continue;
- fa[ver[i]]=x;dep[ver[i]]=dep[x]+;faa[ver[i]]=x;
- dfs(ver[i],x);
- sz[x]+=sz[ver[i]];
- if(sz[ver[i]]>sz[son[x]])son[x]=ver[i];
- }
- ed[x]=z;
- return ;
- }
- void dffs(int x,int f,int tp)
- {
- top[x]=tp;
- if(son[x])dffs(son[x],x,tp);
- for(int i=head[x];i;i=nxt[i])
- {
- if(ver[i]==f||ver[i]==son[x])continue;
- dffs(ver[i],x,ver[i]);
- }
- }
- void pd(int x,int l,int mid,int r)
- {
- if(a[x].lazy)
- {
- a[x*].sum+=a[x].lazy*(mid-l+);
- a[x*+].sum+=a[x].lazy*(r-mid);
- a[x*].lazy+=a[x].lazy;a[x*+].lazy+=a[x].lazy;
- a[x].lazy=;
- }return ;
- }
- ll qur(int x,int l,int r,int lll,int rr)
- {
- if(rr<lll)return ;
- if(lll<=l&&rr>=r)
- {
- return a[x].sum;
- }
- int mid=(l+r)>>;
- pd(x,l,mid,r);
- if(lll>mid)return qur(x*+,mid+,r,lll,rr);
- if(rr<=mid)return qur(x*,l,mid,lll,rr);
- return qur(x*,l,mid,lll,rr)+qur(x*+,mid+,r,lll,rr);
- }
- void add(int x,int l,int r,int lll,int rr,ll z)
- {
- if(rr<lll)return ;
- if(lll<=l&&rr>=r)
- {
- a[x].sum+=z*(r-l+);
- a[x].lazy+=z;
- return ;
- }
- int mid=(l+r)>>;
- pd(x,l,mid,r);
- if(lll<=mid)add(x*,l,mid,lll,rr,z);
- if(rr>mid)add(x*+,mid+,r,lll,rr,z);
- a[x].sum=a[x*].sum+a[x*+].sum;
- }
- void build(int x,int l,int r)
- {
- if(l==r)
- {
- a[x].sum=jian[l];
- return ;
- }
- int mid=(l+r)>>;
- build(x*,l,mid);build(x*+,mid+,r);
- a[x].sum=a[x*].sum+a[x*+].sum;
- return ;
- }
- void gao(int x,int z)
- {
- if(x==root)
- {
- add(,,n,,n,z);
- }
- else if(st[x]<st[root]&&ed[x]>=ed[root])
- {
- int t=find(x,root);
- add(,,n,,st[t]-,z);
- add(,,n,ed[t]+,n,z);
- }
- else
- {
- add(,,n,st[x],ed[x],z);
- }
- }
- void access(int x)
- {
- for(int t=;x;t=x,x=fa[x])
- {
- splay(x);
- if(t)gao(L[t],-);
- if(ch[x][])
- {
- gao(L[ch[x][]],);
- }
- ch[x][]=t;
- push_up(x);
- }
- return ;
- }
- void make_root(int x)
- {
- access(x);splay(x);
- rev[x]^=;swap(L[x],R[x]);
- return ;
- }
- int qursize(int x)
- {
- if(x==root)return n;
- if(st[x]<st[root]&&ed[x]>=ed[root])
- {
- int t=find(x,root);
- return n-sz[t];
- }
- else return sz[x];
- }
- ll qurs(int x)
- {
- if(x==root)return qur(,,n,,n);
- if(st[x]<st[root]&&ed[x]>=ed[root])
- {
- int t=find(x,root);
- return qur(,,n,,st[t]-)+qur(,,n,ed[t]+,n);
- }
- return qur(,,n,st[x],ed[x]);
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- int t1,t2;
- for(int i=;i<n;i++)
- {
- scanf("%d%d",&t1,&t2);
- addd(t1,t2);addd(t2,t1);
- }
- dep[]=;
- dfs(,-);
- build(,,n);
- dffs(,-,);
- char c[];root=;int tmp;
- for(int i=;i<=m;i++)
- {
- scanf("%s",c+);
- scanf("%d",&tmp);
- if(c[]=='Q')
- {
- int size=qursize(tmp);
- ll ss=qurs(tmp);
- printf("%.10lf\n",((double)ss)/size);
- }
- else if(c[]=='L')
- {
- access(tmp);
- }
- else
- {
- make_root(tmp);
- root=tmp;
- }
- }
- return ;
- }
bzoj 3779: 重组病毒的更多相关文章
- bzoj 3779: 重组病毒 LCT+线段树+倍增
题目: 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. 实验在一个封闭 ...
- BZOJ 3779: 重组病毒(线段树+lct+树剖)
题面 escription 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病 ...
- BZOJ 3779 重组病毒 LCT+线段树(维护DFS序)
原题干(由于是权限题我就直接砸出原题干了,要看题意概述的话在下面): Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力 ...
- bzoj 3779 重组病毒——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...
- bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...
- bzoj 3779: 重组病毒【LCT+线段树维护dfs序】
%.8lf会WA!!%.8lf会WA!!%.8lf会WA!!要%.10lf!! 和4817有点像,但是更复杂. 首先对于操作一"在编号为x的计算机中植入病毒的一个新变种,在植入一个新变种时, ...
- bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论
题目大意 1.将x到当前根路径上的所有点染成一种新的颜色: 2.将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根: 3.查询以x为根的子树中所有点权值的平均值. 分析 原题codec ...
- BZOJ 3779 重组病毒 ——LCT 线段树
发现操作一很像一个LCT的access的操作. 然后答案就是路径上的虚边的数量. 然后考虑维护每一个点到根节点虚边的数量, 每次断开一条偏爱路径的时候,子树的值全部+1, 连接一条偏爱路径的时候,子树 ...
- 【BZOJ】3779 重组病毒
[算法]Link-Cut Tree+线段树(维护DFS序) [题解]整整三天……T_T 这篇题解比较资瓷:permui 这道题虽然树形态没有变化,但用lct写的原因在于把题目中的操作一进行了神转化:每 ...
随机推荐
- Windows ,获取硬盘物理序列号(VC++)
#include <windows.h> BOOL GetHDID(PCHAR pIDBufer) { HANDLE hDevice=NULL; hDevice=::Crea ...
- Google hack语法
基础语法: 1.语法说明: inurl: 在url地址栏中显示的信息页面 intext: 显示在正文信息中的内容页面 site: 限制显示你某个域名的所有页面 filetype: 搜索文件的后缀或者扩 ...
- 详细教你实现BST(二叉排序树)
查找基本分类如下: 线性表的查找 顺序查找 折半查找 分块查找 树表的查找 二叉排序树 平衡二叉树 B树 B+树 散列表的查找 今天介绍二叉排序树. 二叉排序树 ( Binary Sort Tree ...
- 浅谈jQuery构造函数
$()函数到底做的什么 jQuery在前端领域路人皆知,对于一向喜欢玩js的博主来说,虽然能力有限,但是还是很喜欢研究他的做为.那么一个简单的美元符号$与一对常见的()括号,jQuery底层到底做了哪 ...
- 获取session
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()) ...
- 团队博客作业Week4 --- 学霸网站--NABC
1.需求(Need) 伴随着经济的发展,科学技术取得了飞速的发展,互联网在各行各业的发展中取得了广泛的应用.随着这些事物的发展,我们每个人都会接触到相当庞大的数据.如何在这些数据中找到自己需要的,如何 ...
- No.100_第一次团队会议
任务的确立 这次会议,我们的主要目标是确定任务: 我们的任务有以下几个选择: 学霸网站,这个项目拥有以前的前端代码,我们再使用Django后端服务.上手难度较低,环境较好. 多平台时间管理软件. 安卓 ...
- StringBuffer 与 StringBuilder类的使用
/*如果需要频繁修改字符串 的内容,建议使用字符串缓冲 类(StringBuffer). StringBuffer 其实就是一个存储字符 的容器. 笔试题目:使用Stringbuffer无 参的构造函 ...
- Journal entry of the thirteenth chapter to chapter seventeenth(第十三章和十七章阅读与疑问)
第十三章: 软件测试的意义在于: a. 发现软件错误: b. 有效定义和实现软件成分由低层到高层的组装过程: c. 验证软件是否满足任务书和系统定义文档所规定的技术要求: d. ...
- 索引超出了数组界限。 在 System.Collections.Generic.Dictionary`2.Resize
博问:Dictionary 超出了数组界限 异常: Exception type: IndexOutOfRangeException Exception message: 索引超出了数组界限. 在 S ...