题目传送门

这道题看了做了个神转换.....推荐个博客给各位大爷看看吧神犇传送门

代码敲了半天....题目也读了半天 线段树维护的东西很容易和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的更多相关文章

  1. [BZOJ3779]重组病毒:Link-Cut Tree+线段树

    分析 其实其他的题解说的都很清楚了. 一个点出发感染到根结点所花费的时间是路径上虚边的条数+1. RELEASE相当于\(access()\). RECENTER相当于\(makeroot()\).( ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  4. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  5. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  6. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  7. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  8. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  9. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  10. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

随机推荐

  1. How to set pycharm configure for remoting development

    配置pycharm远程连接,点击pycharm的tools,选择deployment选项,选择configuration. 2 点击左侧的加号按钮,新增一个连接,取个名字,根据个人配置选择协议,这里选 ...

  2. 『JavaScript』模仿接口

    JavaScript中并没有内置的创建或实现接口的方法.这里将利用JavaScript的灵活性,来实现与接口意义相同的功能. 什么是接口? 接口的好处: 接口提供了一种用以说明一个对象应该具有哪些方法 ...

  3. Gradle下载及安装教程

    Gradle是基于Groovy语言的项目自动化建构工具,在使用Gradle之前常用的构建工具有Ant和Maven,使用这些工具我们可以用来管理项目依赖,打包,部署和发布等.使用Gradle我们将需要的 ...

  4. Python 3基础教程21-列表和元组

    本文介绍列表也元组,先来看看他们的定义. # 元组和列表 # 元组的定义 x = 5,6,2,6 # 或者这样写 x = (5,6,2,6) # 列表定义 y = [5,6,2,6] # 元组的使用, ...

  5. APP功能性测试-4

    弱网络测试 使用fiddler模拟低速环境 使用fiddler抓取手机上某个应用的包 手机连接fiddler fiddler 代理地址127.0.0.1默认端口8888 只抓http协议(https, ...

  6. 分布式文件系统---GlusterF

      1.1 分布式文件系统 1.1.1 什么是分布式文件系统 相对于本机端的文件系统而言,分布式文件系统(英语:Distributed file system, DFS),或是网络文件系统(英语:Ne ...

  7. 5.爬虫 requests库讲解 高级用法

    0.文件上传 import requests files = {'file': open('favicon.ico', 'rb')} response = requests.post("ht ...

  8. UVA 11883 Repairing a Road(最短路径+暴力枚举)

    You live in a small town with R bidirectional roads connecting C crossings and you want to go from c ...

  9. Week2 Teamework from Z.XML 软件分析与用户需求调查(二)应用助手功能评测

    评测人:薛亚杰 周敏轩. 说明:言辞激烈,请勿介意. 软件使用概述 我们团队这次评测的必应助手是必应缤纷桌面的一个小功能,根据评测人员试用几天后发现,它的作用大概就是能够用一种看上去比较生动的形式来给 ...

  10. Week7 Teamework from Z.XML-任务分配

    任务分配 Z.XML任务初步分配新鲜出炉,请关注! 初步估计,我们的项目需要191小时.但是根据敏捷开发的方法,我们将在开发过程中根据情况迅速调整任务分配,以适应当时问题.