bzoj3779: 重组病毒 link-cut-tree
这道题看了做了个神转换.....推荐个博客给各位大爷看看吧神犇传送门
代码敲了半天....题目也读了半天 线段树维护的东西很容易和lct混在一起 调了调能过也是很开心啊 运气比较好吧233
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int M=<<;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,m,rt;
int next[M],sum,father[M],deep[M],first[M],second[M];
struct Edge{int u,v,from;}e[*M];
void ins(int a,int b){sum++; e[sum].u=a; e[sum].v=b; e[sum].from=next[a]; next[a]=sum;}
void insert(int a,int b){ins(a,b); ins(b,a);}
struct node{int l,r; LL sum,tag;}tr[M*];
int c[M][],fa[M],dfsum,rev[M];
void build(int x,int l,int r){
tr[x].l=l; tr[x].r=r;
if(l==r) return ;
int mid=(l+r)>>;
build(x<<,l,mid); build(x<<^,mid+,r);
}
void cal(int x,int w){tr[x].sum+=1LL*(tr[x].r-tr[x].l+)*w;}
void push_up(int x){tr[x].sum=tr[x<<].sum+tr[x<<^].sum;}
void push_down(int x){
if(!tr[x].tag) return ;
int w=tr[x].tag; tr[x].tag=;
int l=x<<,r=x<<^;
tr[l].tag+=w; cal(l,w);
tr[r].tag+=w; cal(r,w);
}
void push_add(int x,int L,int R,int w){
if(L>R) return ;
if(L<=tr[x].l&&tr[x].r<=R){
cal(x,w);
tr[x].tag+=1LL*w; return ;
}
if(tr[x].l==tr[x].r) return ;
push_down(x);
int mid=(tr[x].l+tr[x].r)>>;
if(L<=mid) push_add(x<<,L,R,w);
if(R>mid) push_add(x<<^,L,R,w);
push_up(x);
}
LL push_ans(int x,int L,int R){
if(L>R) return ;
if(L<=tr[x].l&&tr[x].r<=R) return tr[x].sum;
//if(tr[x].l==tr[x].r) return 0;
LL ans=; push_down(x);
int mid=(tr[x].l+tr[x].r)>>;
if(L<=mid) ans+=push_ans(x<<,L,R);
if(R>mid) ans+=push_ans(x<<^,L,R);
return ans;
}
void dfs(int x,int old){
father[x]=fa[x]=old;
first[x]=++dfsum;
deep[x]=deep[old]+;
push_add(,first[x],first[x],deep[x]);
for(int i=next[x];i;i=e[i].from) if(e[i].v!=old) dfs(e[i].v,x);
second[x]=dfsum;
}
bool isrt(int x){return !x||(c[fa[x]][]!=x&&c[fa[x]][]!=x);}//0也算
void down(int x){
if(!rev[x]) return ;
rev[x]=;
int l=c[x][],r=c[x][];
if(l) swap(c[l][],c[l][]),rev[l]^=;
if(r) swap(c[r][],c[r][]),rev[r]^=;
}
void rotate(int x){
int y=fa[x],z=fa[y],l=,r=;
if(c[y][]==x) l=,r=;
if(!isrt(y)) c[z][c[z][]==y]=x;
fa[y]=x; fa[x]=z; fa[c[x][r]]=y;
c[y][l]=c[x][r]; c[x][r]=y;
}
int st[M],top;
void splay(int x){
st[++top]=x; for(int i=x;!isrt(i);i=fa[i]) st[++top]=fa[i];
while(top) down(st[top--]);
while(!isrt(x)){
int y=fa[x],z=fa[y];
if(!isrt(y)){
if(c[z][]==y^c[y][]==x) rotate(x);
else rotate(y);
}
rotate(x);
}
}
bool inson(int x,int y){return first[x]<=first[y]&&second[y]<=second[x];}
int wson(int x,int y){
for(int i=next[x];i;i=e[i].from) if(e[i].v!=father[x])
if(first[e[i].v]<=first[y]&&second[y]<=second[e[i].v]) return e[i].v;
return ;
}
void add(int x,int w){
if(x==rt) push_add(,,n,w);
else if(inson(x,rt)){
int p=wson(x,rt);
push_add(,,first[p]-,w);
push_add(,second[p]+,n,w);
}
else push_add(,first[x],second[x],w);
}
int find(int x){
while(c[x][]) down(x),x=c[x][];
return x;
}
void acs(int x){
int y=;
while(x){
splay(x);
if(c[x][]) add(find(c[x][]),);
if(y) add(find(y),-);
c[x][]=y; y=x; x=fa[x];
}
}
double addup(int x){
if(x==rt) return (double)push_ans(,,n)/n;
if(inson(x,rt)){
int p=wson(x,rt);
return ((double)push_ans(,,first[p]-)+(double)push_ans(,second[p]+,n))/(n-(second[p]-first[p]+));
}
else return (double)push_ans(,first[x],second[x])/(second[x]-first[x]+);
}
void mrt(int x){splay(x); rt=x; swap(c[x][],c[x][]); rev[x]^=;}
int main()
{
int x,y;
n=read(); m=read();
for(int i=;i<n;i++) x=read(),y=read(),insert(x,y);
rt=; build(,,n); dfs(,);
char ch[];
for(int i=;i<=m;i++){
scanf("%s",ch); x=read();
if(ch[]=='Q') printf("%.10lf\n",addup(x));
else{
acs(x);
if(ch[]=='C') mrt(x);
}
}
return ;
}
bzoj3779: 重组病毒 link-cut-tree的更多相关文章
- [BZOJ3779]重组病毒:Link-Cut Tree+线段树
分析 其实其他的题解说的都很清楚了. 一个点出发感染到根结点所花费的时间是路径上虚边的条数+1. RELEASE相当于\(access()\). RECENTER相当于\(makeroot()\).( ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
随机推荐
- Sphinx与coreseek
Sphinx : 高性能SQL全文检索引擎 分类 编程技术 Sphinx是一款基于SQL的高性能全文检索引擎,Sphinx的性能在众多全文检索引擎中也是数一数二的,利用Sphinx,我们可以完成比数据 ...
- Tapestry 权威讲解-备份
http://blog.csdn.net/mindhawk/article/details/5021371#introduction
- 「日常训练」 Mike and Frog (CFR305D2C)
题意与分析 (Codeforces 548C) 我开始以为是一条数学题,死活不知道怎么做,无奈看题解,才知这是一条暴力,思维江化了- - 题意大概是这样的: 两个东西的初始高度分别为h1,h2&quo ...
- 【个人训练】(POJ3279)Fliptile
最近在刷kuangbin神犇的各种套题....感觉自己好弱啊.....还是要多多训练,跟上大神的脚步.最近的这十几题都比较水,记下来这一条我比较印象深刻.也比较难的题目吧(之后应该不会再有水题写了,珍 ...
- Ubuntu下使用Git_3
这里是我举得小白阶段比较困难的地方了, 当在我们向远程数据库推送数据之前,有其他用户向远程数据库推送的相同的文件的时候,服务器会拒绝我们的推送,这个时候就需要我们来整合这两个文件 如图说是,现在显示的 ...
- eclipse返回快捷键
1.图上第一个箭头(Ctrl + Q) 返回上一个编辑点(编辑,修改代码) 2.图上第二个箭头(Alt + Left) 返回上一个操作点(点击进入方法等操作) 3.图上第三个箭头(Alt + Righ ...
- nodeJs 调试异步程序追踪异步报错
DeprecationWarning: Calling an asynchronous function without callback is deprecated. 翻译: 不建议在不回调的情况下 ...
- BZOJ 3670 NOI2014 动物园 KMP+dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意概述:令num[i]表示字符串由1~i的字符形成的前缀中不相重叠的相同前后缀的数 ...
- cocos2d-x环境搭建 摘自百度文库
cocos2d-x环境搭建 引言:笔者在网上寻觅了很多资料,最终发现了这份实际可用的文档,供大家参考.源地址:http://wenku.baidu.com/view/93f7b0f1102de2bd9 ...
- Understand:高效代码静态分析神器详解(一) | 墨香博客 http://www.codemx.cn/2016/04/30/Understand01/
Understand:高效代码静态分析神器详解(一) | 墨香博客 http://www.codemx.cn/2016/04/30/Understand01/ ===== 之前用Windows系统,一 ...