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\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一 ...
随机推荐
- 001_C/C++笔试题_考察C/C++语言基础概念
(一)文章来自:C/C++笔试题-主要考察C/C++语言基础概念.算法及编程,附参考答案 (二)基础概念 2. 头文件中的ifndef/define/endif的作用? 答:防止该头文件被重复引用. ...
- 顺序表元素位置倒置示例c++实现
#include <iostream> #define MAXSIZE 100 using namespace std; void reverse(int a[],int n)//对数组元 ...
- codeforces865C
Gotta Go Fast CodeForces - 865C You're trying to set the record on your favorite video game. The gam ...
- jQuery属性操作之html属性操作
jQuery的属性操作, 是对html文档中的属性进行读取.设置和移除操作.比如,attr(). removeAttr(). 1. attr() attr()可以设置属性值或者返回被选元素的属性值 1 ...
- D. Shortest Cycle(floyd最小环)
D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 18.二叉树的镜像 Java
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- php多线程的概念
来源:http://www.cnblogs.com/zhenbianshu/p/7978835.html 多线程 线程 首先说下线程: 线程(thread) 是操作系统能够进行运算调度的最小单位.它被 ...
- ElasticSearch3:RestAPI
1.设置分片数和副本数 es7默认主分片数和主分片副本数都为1,通过 default_template 指定分片数 PUT http://192.168.8.101:9200/_template/de ...
- legend3---11、php前端模块化开发
legend3---11.php前端模块化开发 一.总结 一句话总结: 把常用的前端块(比如课程列表,比如评论列表)放进模块列表里面,通过外部php变量给数据,可以很好的实现复用和修改 页面调用 @p ...
- R语言:各类型数据文件的导入
导入csv: read.csv() 导入txt: read.table() 注意,txt文件编码为unicode的导入r会报错,需转换成ANSI 读入excel:需要安装xlsx包,安装此包前先下载好 ...