题目链接

  LCTrotate打错尬死

  容易发现本题就是问两边子树大小乘积,于是开个数组动态维护LCT每个节点虚子树上有多少点,在Access和Link的时候更新即可。

  

#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<map>
#define maxn 400020
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int size[maxn];
int stack[maxn],top; struct Splay{
struct Node{
int e[],fa,sum,tag,val;
}tree[maxn];
inline int iden(int x){ return x==tree[tree[x].fa].e[]; }
inline bool isroot(int x){ return x!=tree[tree[x].fa].e[]&&x!=tree[tree[x].fa].e[]; }
inline void connect(int x,int fa,int how){ tree[x].fa=fa; tree[fa].e[how]=x; }
inline void reverse(int x){
swap(tree[x].e[],tree[x].e[]);
tree[x].tag^=;
}
inline void update(int x){ tree[x].sum=tree[tree[x].e[]].sum+tree[tree[x].e[]].sum+size[x]+; }
inline void pushdown(int x){
if(tree[x].tag==) return;
if(tree[x].e[]) reverse(tree[x].e[]);
if(tree[x].e[]) reverse(tree[x].e[]);
tree[x].tag=;
}
void rotate(int x){
int y=tree[x].fa; int r=tree[y].fa;
int sony=iden(x),sonr=iden(y);
tree[x].fa=r; if(!isroot(y)) tree[r].e[sonr]=x;
int b=tree[x].e[sony^];
connect(b,y,sony);
connect(y,x,sony^);
update(y);
}
void pushto(int x){
top=;
while(!isroot(x)){
stack[++top]=x; x=tree[x].fa;
}
pushdown(x);
while(top) pushdown(stack[top--]);
}
void splay(int x){
pushto(x);
while(!isroot(x)){
int fa=tree[x].fa;
if(!isroot(fa)){
if(iden(fa)==iden(x)) rotate(fa);
else rotate(x);
}
rotate(x);
}
update(x);
}
void access(int x){
int last=;
while(x){
splay(x);
size[x]+=tree[tree[x].e[]].sum;
size[x]-=tree[last].sum;
tree[x].e[]=last;
update(x);
last=x; x=tree[x].fa;
}
return;
}
void makeroot(int x){
access(x);
splay(x);
reverse(x);
}
inline int findroot(int x){
access(x);
splay(x);
while(tree[x].e[]) x=tree[x].e[];
return x;
}
inline void split(int x,int y){
makeroot(x);
access(y);
splay(y);
}
inline void link(int x,int y){
split(x,y);
size[y]+=tree[x].sum;
tree[x].fa=y;
update(y);
}
}s; int cnt; int main(){
int n=read(),m=read();
for(int i=;i<=n;++i) s.tree[i].sum=;
for(int i=;i<=m;++i){
char c[];
scanf("%s",c);
int from=read(),to=read();
if(c[]=='A') s.link(from,to);
else{
s.split(from,to);
printf("%lld\n",(long long)(size[from]+)*(size[to]+));
}
}
return ;
}

【Luogu】P4219大融合(LCT)的更多相关文章

  1. [BZOJ4530][Bjoi2014]大融合 LCT + 启发式合并

    [BZOJ4530][Bjoi2014]大融合 试题描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是 ...

  2. 【洛谷 P4219】 [BJOI2014]大融合(LCT)

    题目链接 维护子树信息向来不是\(LCT\)所擅长的,所以我没搞懂qwq 权当背背模板吧.Flash巨佬的blog里面写了虽然我没看懂. #include <cstdio> #define ...

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

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

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

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

  5. [BJOI2014]大融合(LCT)

    题面 luogu bzoj是权限题.. 题解 \(LCT\)维护子树信息 因为\(LCT\)中有一些虚子树,\(splay\)维护不了. 所以要新开一个数组来记录 然后注意\(link\)时 是先\( ...

  6. 洛谷P4219 大融合

    LCT新姿势:维护子树信息. 不能带修,子树修改就要toptree了... 题意:动态加边,求子树大小. 解: 维护子树信息只要额外维护虚边连的儿子的信息即可.这里把siz的定义变成子树大小. 哪里会 ...

  7. Luogu4219 BJOI2014 大融合 LCT

    传送门 题意:写一个数据结构,支持图上连边(保证图是森林)和询问一条边两端的连通块大小的乘积.$\text{点数.询问数} \leq 10^5$ 图上连边,$LCT$跑不掉 支持子树$size$有点麻 ...

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

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

  9. BZOJ.4530.[BJOI2014]大融合(LCT)

    题目链接 BZOJ 洛谷 详见这 很明显题目是要求去掉一条边后两边子树sz[]的乘积. LCT维护的是链的信息,那么子树呢? 我们用s_i[x]来记录轻边连向x的子树的和(记作虚儿子),那么sum[x ...

随机推荐

  1. python_36_文件操作4

    f=open('test.txt','a',encoding='utf-8') #f.truncate()#截断,不指定将清空所有内容 f.truncate(5)#从头开始截断,截断5个字符 注:使用 ...

  2. TypeScript task

    Ctrl+Shift+B 生成 js 文件.

  3. Drupal常用的模块

    CCK (Content Construction Kit ) : 添加字段模块 Views:生成列表 Tinymce:(Wysiwyg Editor) 常用的编辑器之一 Ajax Form Buil ...

  4. PHP中可变变量到底有什么用?

    转自:http://blog.csdn.net/engine_1124/article/details/8660291 什么是可变变量? PHP提供了一种其他类型的变量——可变变量.可变变量允许我们动 ...

  5. MySql-8.0.12 安装教程随笔

    下载地址: https://www.mysql.com/downloads/ 现在最下边的社区版本,也就是免费版本 之后我们会看到有两个选择的下载,一个为zip压缩包格式,一个是Install版本,个 ...

  6. sql注入问题 java中将MySQL的数据库验证秘密加上 ' or '1'= '1 就可以出现万能密码

    password的字符串中,加上 ' or '1'= '1 就可以制作出万能密码. 原因如下: 原代码中密码是123456 执行数据库查询语句 实际上执行的SQL语句是: select * from ...

  7. JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift

    5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed ...

  8. python中生成器对象和return 还有循环的区别

    python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...

  9. pycharm配置Git托管

    利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程  转载https ...

  10. day14之模块

    一.列表生成式与生成表达式 1.列表生成式(数据量小) 要求:在列表内造100个元素 示例: l=[] for i in range(100): l.append('egg%s'%i) print(l ...