题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530

用LCT维护子树 size,就是实边和虚边分开维护;

看博客:https://blog.csdn.net/neither_nor/article/details/52979425

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=1e5+;
int n,fa[xn],siz[xn],lt[xn],c[xn][],sta[xn],top,rev[xn];
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
bool isroot(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
void pushup(int x){siz[x]=lt[x]+siz[c[x][]]+siz[c[x][]]+;}//
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
void rever(int x){rev[x]^=; swap(c[x][],c[x][]);}
void psd(int x)
{
if(!rev[x])return;
rever(c[x][]); rever(c[x][]);
rev[x]=;
}
void splay(int x)
{
sta[top=]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];
while(top)psd(sta[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x)
{
for(int t=;x;t=x,x=fa[x])
{
splay(x);
//if(c[x][1])siz[x]-=siz[c[x][1]],lt[x]+=siz[c[x][1]];
if(c[x][])lt[x]+=siz[c[x][]];
c[x][]=t;
//if(t)siz[x]+=siz[t],lt[x]-=siz[t];
if(t)lt[x]-=siz[t];
pushup(x);
}
}
void makeroot(int x)
{
access(x); splay(x); rever(x);
}
void link(int x,int y)
{
makeroot(x); access(y); splay(y);
fa[x]=y; lt[y]+=siz[x]; pushup(y);
}
char dc[];
int main()
{
n=rd(); int m=rd();
for(int i=,x,y;i<=m;i++)
{
scanf("%s",dc); x=rd(); y=rd();
if(dc[]=='A')link(x,y);
else
{
makeroot(x); access(y); splay(y);
printf("%lld\n",(ll)(siz[y]-siz[x])*siz[x]);
}
}
return ;
}

bzoj 4530 大融合 —— LCT维护子树信息的更多相关文章

  1. 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息

    题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量 ...

  2. bzoj 4530 [Bjoi2014]大融合——LCT维护子树信息

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 LCT维护子树 siz .设 sm[ ] 表示轻儿子的 siz 和+1(1是自己的si ...

  3. BZOJ4530[Bjoi2014]大融合——LCT维护子树信息

    题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它的简单路径的数 ...

  4. 大融合——LCT维护子树信息

    题目 [题目描述] 小强要在 $N$ 个孤立的星球上建立起一套通信系统.这套通信系统就是连接 $N$ 个点的一个树.这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树 ...

  5. Loj 2230. 「BJOI2014」大融合 (LCT 维护子树信息)

    链接:https://loj.ac/problem/2230 思路: 设立siz数组保存虚点信息,sum表示总信息 维护子树信息link操作和access操作需要进行一些改动 可参考博客:https: ...

  6. [BJOI2014]大融合 LCT维护子树信息

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...

  7. bzoj 3779 重组病毒——LCT维护子树信息

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...

  8. P4219 [BJOI2014]大融合 LCT维护子树大小

    \(\color{#0066ff}{ 题目描述 }\) 小强要在\(N\)个孤立的星球上建立起一套通信系统.这套通信系统就是连接\(N\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一 ...

  9. 洛谷4219 BJOI2014大融合(LCT维护子树信息)

    QWQ 这个题目是LCT维护子树信息的经典应用 根据题目信息来看,对于一个这条边的两个端点各自的\(size\)乘起来,不过这个应该算呢? 我们可以考虑在LCT上多维护一个\(xv[i]\)表示\(i ...

随机推荐

  1. Asp.Net 5 Web Hook

    首先,然我们来看一下WebHooks是什么.WebHooks是一个协议.它们是HTTP回调技术.并且它们是"用户定义的HTTP回调".你和 (或) 您的应用程序在有什么事情发生时会 ...

  2. win7 32位下载安装redis并安装php_redis扩展

    redis打包文件下载地址:http://files.cnblogs.com/files/cuiwenyuan/Redis-3.2.100-Windows-32.zip php_redis.dll下载 ...

  3. IOS navigationItem 设置返回button,title图片和rightBarButtonItem

    1.自己定义返回button UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" st ...

  4. rtmp播放器

    rtmp测试地址: rtmp://live.hkstv.hk.lxdns.com/live 有的时候连接不上,不是很流畅 参考: 1,simplest flashmedia example http: ...

  5. UDP标准模型

    伪代码 #服务端 #创建UDP服务器 ss = socket() #创建一个服务器套接字 ss.bind() #绑定服务器套接字 inf_loop: #服务器无限循环 cs = ss.recvfrom ...

  6. 【leetcode刷题笔记】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 316python 基础之计算机基础、Python简介、变量、注释、基础数据类型初识、if、while、语句

    一.计算机基础. cpu:相当于人的大脑,运算与控制中心. 速度 飞机 内存:4G,8G,16G....暂时存储,供给cpu数据. 速度 高铁.成本高,断电即消失. 硬盘:相当于你电脑的数据库,存储着 ...

  8. linux下安装telnet(centos7)

    1 环境 centos 7 yum已配置好 网络连接正常 2 检查是否已安装相关软件 rpm -qa|grep telnet 3 安装相关组件 yum install xinetd yum insta ...

  9. hd acm1018

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  10. Ansible playbook练习

    示例1:创建用户的Playbook --- - name: create user hosts: openstack gather_facts: false tasks: - name: create ...