Loj 2230. 「BJOI2014」大融合 (LCT 维护子树信息)
链接: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 维护子树信息)的更多相关文章
- LOJ#2230. 「BJOI2014」大融合
LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...
- 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息
题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量 ...
- 【LOJ】#2230. 「BJOI2014」大融合
题解 我现在真是太特么老年了 一写数据结构就颓废,难受 这题就是用lct维护子树 ???lct怎么维护子树 这样想,我们给每个点记录虚边所在的子树大小,只发生在Access和link的时候 这样的话我 ...
- BZOJ4530[Bjoi2014]大融合——LCT维护子树信息
题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它的简单路径的数 ...
- bzoj 4530 [Bjoi2014]大融合——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 LCT维护子树 siz .设 sm[ ] 表示轻儿子的 siz 和+1(1是自己的si ...
- bzoj 4530 大融合 —— LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 用LCT维护子树 size,就是实边和虚边分开维护: 看博客:https://blog ...
- [BJOI2014]大融合 LCT维护子树信息
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...
- 大融合——LCT维护子树信息
题目 [题目描述] 小强要在 $N$ 个孤立的星球上建立起一套通信系统.这套通信系统就是连接 $N$ 个点的一个树.这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树 ...
- P4219 [BJOI2014]大融合 LCT维护子树大小
\(\color{#0066ff}{ 题目描述 }\) 小强要在\(N\)个孤立的星球上建立起一套通信系统.这套通信系统就是连接\(N\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一 ...
随机推荐
- App自动化-python基础
定义类:类变量.成员变量.局部变量:构造函数.类方法:实例化对象: # -*- coding: utf-8 -*- ''' Created on 2019-6-25 @author: adminstr ...
- Luogu P1198 [JSOI2008]最大数 单调队列/ST表
开一个单调队列,下标递增,值递减. 然后在上面二分最大数. 如果加上并查集可以做到接近线性. 还有一种是插入一个数然后,从后向前更新ST表. #include<cstdio> #inclu ...
- POJ3764
题目 POJ3764 The xor-longest Path 原题传送门 主要思路: 1.求出每个点到根节点(这里是树,所以直接取0)路径上所有权值xor和为d[i],则任意两点间路径xor和则为 ...
- ARP 之 发送请求arp_solicit
概述 arp_solicit用来发送ARP请求,首先会根据ARP_ANNOUNCE参数来选取源地址,然后判断是否达到内核发送次数上限,未达到则调用内核arp_send_dst函数发送,如果达到上限,则 ...
- PHP-生产随机密码
public function dd(){ error_reporting(E_ALL^E_NOTICE^E_WARNING);$arr1 = array(0,1,2,3,4,5,6,7,8,9);$ ...
- [JDBC]批量提交插入语句以提高数据插入速度(效率提升不明显)
// Initialize conn&stmt Connection conn=null; Statement stmt=null; ... conn=dataSource.getConnec ...
- [Java]简单计算下一段Java代码段运行了多少秒
long startTime = System.currentTimeMillis(); ...... long endTime = System.currentTimeMillis(); logge ...
- redis 的使用 及 配置文件解读
redis-server命令 redis-server /usr/local/redis/conf/redis.conf #加配置文件绝对路径启动redis服务 redis-server /usr/l ...
- 前端知识扫盲-VUE知识篇一(VUE核心知识)
最近对整个前端的知识做了一次复盘,总结了一些知识点,分享给大家有助于加深印象. 想要更加理解透彻的同学,还需要大家自己去查阅资料或者翻看源码.后面会陆续的更新一些相关注知识点的文章. 文章只提出问题, ...
- Nginx知识
OpenResty最佳实践->location匹配规则传说中图片防盗链的爱恨情仇