链接:https://loj.ac/problem/2230

思路:

设立siz数组保存虚点信息,sum表示总信息

维护子树信息link操作和access操作需要进行一些改动

可参考博客:https://www.cnblogs.com/GXZlegend/p/7061458.html

实现代码;

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ls c[x][0]
#define rs c[x][1]
const int M = 4e5+;
const int inf = 1e9;
int top;
int sum[M],c[M][],val[M],fa[M],rev[M],mn[M],S[M];
int siz[M];
inline void up(int x){
sum[x] = sum[ls] + sum[rs] + siz[x] + ;
} inline void pushrev(int x){
swap(ls,rs); rev[x] ^= ;
} inline bool isroot(int x){
return c[fa[x]][]!=x&&c[fa[x]][]!=x;
} inline void rotate(int x){
int y = fa[x],z = fa[y];
int k = c[y][] == x;
if(!isroot(y)) c[z][c[z][]==y]=x;
fa[x] = z;
c[y][k] = c[x][k^]; fa[c[x][k^]] = y;
c[x][k^] = y; fa[y] = x;
up(y); up(x);
} inline void pushdown(int x){
if(rev[x]){
if(ls) pushrev(ls);
if(rs) pushrev(rs);
rev[x] = ;
}
} inline void splay(int x){
S[top=]=x;
for(int i = x;!isroot(i);i=fa[i]) S[++top] = fa[i];
while(top) pushdown(S[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);
}
} inline void access(int x){
//维护子树
for(int y = ;x;y = x,x = fa[x])
splay(x),siz[x] += sum[rs] - sum[y],c[x][] = y,up(x);
//for(int y = 0;x;y = x,x = fa[x])
//splay(x),c[x][1] = y,up(x);
} inline void makeroot(int x){
access(x); splay(x); pushrev(x);
} inline void split(int x,int y){
makeroot(x); access(y); splay(y);
} inline void link(int x,int y){
//维护子树
makeroot(x); makeroot(y); fa[x] = y; siz[y]+=sum[x]; up(y);
// makeroot(x);fa[x] = y;
} inline void cut(int x,int y){
split(x,y); fa[x] = c[y][] = ; up(y);
} inline int findroot(int x){
access(x); splay(x);
while(ls) x = ls;
return x;
} int main()
{
int n,q,u,v;
scanf("%d%d",&n,&q);
char op[];
while(q--){
scanf("%s",op); scanf("%d%d",&u,&v);
if(op[] == 'A') link(u,v);
else {
split(u,v);
printf("%lld\n",1LL*sum[u]*(sum[v]-sum[u]));
}
}
}

Loj 2230. 「BJOI2014」大融合 (LCT 维护子树信息)的更多相关文章

  1. LOJ#2230. 「BJOI2014」大融合

    LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...

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

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

  3. 【LOJ】#2230. 「BJOI2014」大融合

    题解 我现在真是太特么老年了 一写数据结构就颓废,难受 这题就是用lct维护子树 ???lct怎么维护子树 这样想,我们给每个点记录虚边所在的子树大小,只发生在Access和link的时候 这样的话我 ...

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

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

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

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

  6. bzoj 4530 大融合 —— LCT维护子树信息

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 用LCT维护子树 size,就是实边和虚边分开维护: 看博客:https://blog ...

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

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

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

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

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

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

随机推荐

  1. MySQL基础练习---牛客网的数据以及典型题目

    1 部门表departments 部门no和部门名称 2 部门员工表 dept_emp 每个部门对应的员工信息 3 部门经理表 dept_manager 每个部门的经理信息 4 员工表 employe ...

  2. 牛客练习赛52 B题【树状数组维护区间和{查询区间和,如果区间元素重复出现则计数一次}】补题ing

    [题目] 查询区间和,如果区间元素重复出现则计数一次. 链接:https://ac.nowcoder.com/acm/contest/1084/B [题解] 将询问按r排序,维护每个数最后出现的位置, ...

  3. PHP mysqli_get_charset() 函数

    mysqli_get_charset() 函数返回字符集对象. <?php $con=mysqli_connect("localhost","my_user&quo ...

  4. UnhandledExceptionPolicy

    winform程序未捕获异常解决方法  EventType clr20r3 P1 http://blog.csdn.net/chichaodechao/article/details/8294922

  5. Hibernate 4 升级到 Hibernate 5 的时候 SessionFactory 不能使用

    在 Hibernate 4 升级到 5 的时候老的 sessionFactory 出现错误. public static SessionFactory initSession() { Configur ...

  6. Vue_(Router路由)-vue-router路由的基本用法

    vue-router官网:传送门 vue-router起步:传送门 vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用 单页应用:Single Page Applicati ...

  7. Leetcode题目分类整理

    一.数组 8) 双指针 ---- 滑动窗口 例题: 3. Longest Substring Without Repeating Characters 描述:Given a string, find ...

  8. 递推,求至少连续放置三个U的危险组合

    UVA580-Critical Mass 题意 有两种方块,L和U,有至少三个连续的U称为危险组合,问有多少个危险组合 solution: 至少这个概念比较难求 ,所以转化为(1ll<<n ...

  9. 用Qt生成dll类库及调用方法

    空白工程新建DLL后,将DLL和LIB文件放入需要调用的“指定目录” 项目->属性->连接器->常规->附加库目录->添加“指定目录” 项目->属性->连接器 ...

  10. 发布机制-灰度发布-例子:QZone

    ylbtech-发布机制-灰度发布-例子:QZone QZone是另外一个采用灰度发布的例子.大家都知道,QZone在过去的一年中改进是巨大的,从以前慢悠悠的老爷爷变成了一个充满青春活力的小伙子.其中 ...