bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论
题目大意
1、将x到当前根路径上的所有点染成一种新的颜色;
2、将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根;
3、查询以x为根的子树中所有点权值的平均值。
分析
原题codechef ,Gangsters of Treeland
那题没有换根操作
用神转化把问题转操作1转化成access操作
操作3转化成每个点到根上有多少条虚边
用dfn序+线段树维护
现在多了个换根操作,只是线段树上加个分类讨论而已
注意
longdouble会Wa,double就A了
姿势
1.用dfn序判断x是否y的祖先
bool ispre(int x,int y){
if(x==y) return 0;//相等时不是祖先
return bg[x]<=bg[y]&&ed[y]<=ed[x];
}
2.分类讨论姿势
xxx(int x){
if(x==rt) xxx;
else if(ispre(x,rt)){
xxx;
}
else{
xxx;
}
}
3.多种数据结构时,用类似 seg_mdf()这样加前缀和下划线的方法
4.以后splay打翻转标记时就把儿子换了吧
5.可用注释分割代码
solution
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long LL;
const int M=100007;
inline int rd(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-')f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
}
int n,m,rt;
int g[M],te;
struct edge{
int y,nxt;
}e[M<<1];
void addedge(int x,int y){
e[++te].y=y;e[te].nxt=g[x];g[x]=te;
}
//////////////////////////////// split tree
int top[M],pre[M];
int bg[M],ed[M],tdfn;
int dep[M],sz[M];
int son[M],pid[M];
bool ispre(int x,int y){
if(x==y) return 0;//*************
return bg[x]<=bg[y]&&ed[y]<=ed[x];
}
void dfs1(int x){
sz[x]=1;
int p,y;
for(p=g[x];p;p=e[p].nxt)
if((y=e[p].y)!=pre[x]){
pre[y]=x;
dep[y]=dep[x]+1;
dfs1(y);
sz[x]+=sz[y];
if(sz[y]>sz[son[x]]) son[x]=y;
}
}
void dfs2(int x){
bg[x]=++tdfn;
pid[tdfn]=x;
if(son[x]){
top[son[x]]=top[x];
dfs2(son[x]);
}
int p,y;
for(p=g[x];p;p=e[p].nxt)
if((y=e[p].y)!=pre[x]&&y!=son[x]){
top[y]=y;
dfs2(y);
}
ed[x]=tdfn;
}
int jump(int x,int to){
for(;dep[top[x]]>dep[to];x=pre[x]){
x=top[x];
if(pre[x]==to) return x;
}
return pid[bg[to]+1];
}
LL getsz(int x){
if(x==rt) return n;
if(ispre(x,rt)){
int y=jump(rt,x);
return n-sz[y];
}
else{
return sz[x];
}
}
////////////////////////////////// Segment
struct Seg{
LL sum,tag;
}c[M<<2];
void seg_pushup(int x){
c[x].sum=c[x<<1].sum+c[x<<1|1].sum;
}
void seg_totag(int x,LL d,LL len){
c[x].sum+=d*len;
c[x].tag+=d;
}
void seg_pushdown(int x,LL aa,LL bb){
if(c[x].tag){
seg_totag(x<<1,c[x].tag,aa);
seg_totag(x<<1|1,c[x].tag,bb);
c[x].tag=0;
}
}
void seg_mdf(int x,int l,int r,int tl,int tr,LL d){
if(tl<=l&&r<=tr){
seg_totag(x,d,r-l+1);
return;
}
int mid=l+r>>1;
seg_pushdown(x,mid-l+1,r-mid);
if(tl<=mid) seg_mdf(x<<1,l,mid,tl,tr,d);
if(mid<tr) seg_mdf(x<<1|1,mid+1,r,tl,tr,d);
seg_pushup(x);
}
void seg_add(int x,LL d){
if(x==rt) return seg_totag(1,d,tdfn);
if(ispre(x,rt)){
int y=jump(rt,x);
seg_mdf(1,1,tdfn,bg[1],ed[1],d);
seg_mdf(1,1,tdfn,bg[y],ed[y],-d);
}
else{
seg_mdf(1,1,tdfn,bg[x],ed[x],d);
}
}
LL seg_get(int x,int l,int r,int tl,int tr){
if(tl<=l&&r<=tr) return c[x].sum;
int mid=l+r>>1;
seg_pushdown(x,mid-l+1,r-mid);
LL res=0;
if(tl<=mid) res+=seg_get(x<<1,l,mid,tl,tr);
if(mid<tr) res+=seg_get(x<<1|1,mid+1,r,tl,tr);
return res;
}
LL seg_sum(int x){
if(x==rt) return c[1].sum;
LL res=0;
if(ispre(x,rt)){
int y=jump(rt,x);
res+=seg_get(1,1,tdfn,bg[1],ed[1]);
res-=seg_get(1,1,tdfn,bg[y],ed[y]);
}
else{
res=seg_get(1,1,tdfn,bg[x],ed[x]);
}
return res;
}
/////////////////////////////////////// LCT
struct LCT{
int ch[2],p,rev;
int id,lf,rt;//**********************************
void init(int ii){
ch[0]=ch[1]=p=rev=0;
lf=rt=id=ii;
}
}a[M];
int stack[M],tot;
void torev(int x){
a[x].rev^=1;
swap(a[x].ch[0],a[x].ch[1]);//****
swap(a[x].lf,a[x].rt);
}
void pushup(int x){
a[x].lf=a[x].rt=a[x].id;
if(a[x].ch[0]) a[x].lf=a[a[x].ch[0]].lf;
if(a[x].ch[1]) a[x].rt=a[a[x].ch[1]].rt;
}
void pushdown(int x){
if(a[x].rev){
if(a[x].ch[0]) torev(a[x].ch[0]);
if(a[x].ch[1]) torev(a[x].ch[1]);
a[x].rev^=1;
}
}
bool isrt(int x){
int y=a[x].p;
return a[y].ch[0]!=x&&a[y].ch[1]!=x;
}
void clear(int x){
for(;!isrt(x);x=a[x].p) stack[++tot]=x;
for(stack[++tot]=x;tot>0;tot--) pushdown(stack[tot]);
}
void rot(int x){
int y=a[x].p;
int z=a[y].p;
int D=a[y].ch[1]==x,ss=D^1;
if(!isrt(y)) a[z].ch[a[z].ch[1]==y]=x;
a[x].p=z;
a[y].p=x;
if(a[x].ch[ss]) a[a[x].ch[ss]].p=y;
a[y].ch[D]=a[x].ch[ss];
a[x].ch[ss]=y;
pushup(y);
pushup(x);
}
void splay(int x){
int y,z;
for(clear(x);!isrt(x);rot(x)){
y=a[x].p;
z=a[y].p;
if(isrt(y)) continue;
if((a[y].ch[1]==x)!=(a[z].ch[1]==y)) rot(x);
else rot(y);
}
}
void access(int x){
for(int t=0;x;t=x,x=a[x].p){
splay(x);
if(a[x].ch[1]) seg_add(a[a[x].ch[1]].lf,1);
if(t) seg_add(a[t].lf,-1);
a[x].ch[1]=t;
pushup(x);
}
}
void ac(int x){
access(x);
splay(x);
}
void chgrt(int x){
ac(x);
torev(x);
}
///////////////////////////// main
int main(){
int i,x,y;
n=rd(),m=rd();rt=1;
for(i=1;i<n;i++){
x=rd(),y=rd();
addedge(x,y);
addedge(y,x);
}
pre[1]=0;
dep[1]=1;
dfs1(1);
top[1]=1;
dfs2(1);
for(i=1;i<=n;i++){
a[i].init(i);
if(pre[i]){
a[i].p=pre[i];
seg_add(i,1);
}
}
char s[13];
while(m--){
scanf("%s",s);
x=rd();
if(s[2]=='L'){
ac(x);
}
else if(s[2]=='C'){
chgrt(x);
rt=x;
}
else{
LL tp1=seg_sum(x);
LL tp2=getsz(x);
tp1+=tp2;
printf("%.10lf\n",(db)tp1/(db)tp2);
}
}
return 0;
}
bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论的更多相关文章
- bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...
- BZOJ 3779 重组病毒 ——LCT 线段树
发现操作一很像一个LCT的access的操作. 然后答案就是路径上的虚边的数量. 然后考虑维护每一个点到根节点虚边的数量, 每次断开一条偏爱路径的时候,子树的值全部+1, 连接一条偏爱路径的时候,子树 ...
- BZOJ 3779: 重组病毒(线段树+lct+树剖)
题面 escription 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病 ...
- BZOJ 3779 重组病毒 LCT+线段树(维护DFS序)
原题干(由于是权限题我就直接砸出原题干了,要看题意概述的话在下面): Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力 ...
- bzoj 3779: 重组病毒 LCT+线段树+倍增
题目: 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. 实验在一个封闭 ...
- bzoj 3779 重组病毒——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...
- bzoj 3779: 重组病毒
一道好题~~ 一个点到根传染需要的时间是这段路径上不同颜色的数目,一个点子树到根平均传染时间就是加权平均数了(好像是废话). 所以只要用线段树维护dfs序就这个可以了,换根的话一个点的子树要么在dfs ...
- BZOJ.4817.[SDOI2017]树点涂色(LCT DFS序 线段树)
题目链接 操作\(1.2\)裸树剖,但是操作\(3\)每个点的答案\(val\)很不好维护.. 如果我们把同种颜色的点划分到同一连通块中,那么向根染色的过程就是Access()! 最初所有点间都是虚边 ...
- bzoj4817/luogu3703 树点涂色 (LCT+dfs序+线段树)
我们发现,这个染色的操作他就很像LCT中access的操作(为什么??),然后就自然而然地想到,其实一个某条路径上的颜色数量,就是我们做一个只有access操作的LCT,这条路径经过的splay的数量 ...
随机推荐
- Golang TCP转发到指定地址
Golang TCP转发到指定地址 第二个版本,设置指定ip地址 代码 // tcpForward package main import ( "fmt" "net&qu ...
- spring IOC注解与xml配置
转载自:https://blog.csdn.net/u014292162/article/details/52277756 IOC 1小案例 将对象的依赖交给配置文件来配置(配置文件的名字是可以任意的 ...
- 如何用纯 CSS 创作一台拍立得照相机
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/YjYgey 可交互视频 此视频是可 ...
- Kafka 基础实战 :消费者和生产者实例
学习地址: http://www.jikexueyuan.com/course/2036.html
- ubuntu中卸载没有安装完全的软件包
sudo apt-get autoclean sudo apt-get clean sudo apt-get autoremove
- Python基础学习总结__Day3
一.集合 1.特性:无序且天生去重,格式为{} 2.作用: (1)去重 (2)关系测试 3.可调用函数(常见对列表操作) (1)取交集:A.intersection(B) (2)取并集:A.union ...
- LeetCode(238) Product of Array Except Self
题目 Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj 2251 三维地图最短路径问题 bfs算法
题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...
- 《小团团团队》第八次团队作业:Alpha冲刺
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 小团团团队 作业学习目标 (1)掌握软件测试基础技术; (2)学 ...