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\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一 ...
随机推荐
- 【vue】@click绑定的函数,如何同时传入事件对象和自定义参数
知识很久不用的话,果然是容易忘的... 记记笔记,希望能加深点印象吧. [仅仅传入事件对象] html: <div id="app"> <button @clic ...
- luogu P4194 矩阵
嘟嘟嘟 先二分. 令二分的值为\(mid\),则对于每一行都要满足\(|\sum_{i = 1} ^ {n} (A_{ij} - B_{ij})|\),把绝对值去掉,就得到了\((\sum_{i = ...
- Windows下安装Elasticsearch6.4.1和Head,IK分词器
所需运行环境 1.安装jdk1.8(步骤略) 2.安装git(步骤略)3.安装nodejs(步骤略) 一.ElasticSearch的安装 下载elasticsearch6.4.1,将下载后的es解压 ...
- group by用法提示:select涉及字段规则
工资表t_salary如下: id month name salary 1 201601 Jim 12 2 201601 Bruce 30 3 201601 Peter 23 ...
- 【SR汇总】基于深度学习方法
1.SRCNN.FSRCNN (Learning a Deep Convolutional Network for Image Super-Resolution, ECCV2014) (Acceler ...
- <JavaScript>数组的sort()方法中比较函数是怎么工作的
sort()函数比较时调用的是每个数组项的toString()方法,并非按数值大小进行比较,所以往往得不到我们想要的结果. 比如: ,,,,]; values.sort( ); alert(value ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- js中bind解析
一.arguments的含义 // arguments 是一个对应于传递给函数的参数的类数组对象 function a(){ console.log(arguments); } a(); // Arg ...
- JavaScript如何封装插件
什么是封装呢? 我的理解就是 把一个功能单独做成一个组件,就像做饺子,以前做饺子必须自己先用面粉做饺子皮,再做饺子馅,然后再手工包饺子,但是现在人们发明了自动包饺子机器,虽然机器里面的每一步骤和你自己 ...
- centos7安装redis3.2.5集群
安装参照 https://blog.csdn.net/mingliangniwo/article/details/54600640 https://blog.csdn.net/u013820 ...