[COGS2701]:动态树
题面
Sol
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define ls ch[0][x]
# define rs ch[1][x]
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 10);
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, Q, ch[2][_], fa[_], size[_], sum[_], rev[_], S[_];
IL bool Son(RG int x){ return ch[1][fa[x]] == x; }
IL bool Isroot(RG int x){ return ch[0][fa[x]] != x && ch[1][fa[x]] != x; }
IL void Reverse(RG int x){ if(!x) return; swap(ls, rs); rev[x] ^= 1; }
IL void Update(RG int x){ sum[x] = sum[ls] + sum[rs] + size[x] + 1; }
IL void Pushdown(RG int x){ if(!rev[x]) return; Reverse(ls); Reverse(rs); rev[x] = 0; }
IL void Rotate(RG int x){
RG int y = fa[x], z = fa[y], c = Son(x);
if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;
ch[!c][x] = y; fa[y] = x; Update(y);
}
IL void Splay(RG int x){
S[S[0] = 1] = x;
for(RG int y = x; !Isroot(y); y = fa[y]) S[++S[0]] = fa[y];
while(S[0]) Pushdown(S[S[0]--]);
for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
Update(x);
}
IL void Access(RG int x){
for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), size[x] += sum[ch[1][x]] - sum[y], ch[1][x] = y, Update(x);
}
IL void Makeroot(RG int x){ Access(x); Splay(x); Reverse(x); }
IL int Findroot(RG int x){ Access(x); Splay(x); while(ch[0][x]) x = ch[0][x]; return x; }
IL void Link(RG int x, RG int y){ Makeroot(x); Makeroot(y); fa[x] = y; size[y] += sum[x]; Update(y); }
int main(RG int argc, RG char* argv[]){
freopen("dynamic_tree.in", "r", stdin); freopen("dynamic_tree.out", "w", stdout);
n = Read(); Q = Read();
while(Q--){
RG int op = Read(), x = Read(), y, rt;
if(op == 3) y = Read(), rt = Findroot(x), Link(x, y), Makeroot(rt);
else if(op == 2) Access(x), printf("%d\n", size[x] + 1);
else Makeroot(x);
}
return 0;
}
[COGS2701]:动态树的更多相关文章
- [cogs2701]动态树
题面戳我 sol 比较裸啊. 注意操作顺序就行了. code #include<cstdio> #include<algorithm> using namespace std; ...
- 如何利用FineReport制作动态树报表
在对数据字段进行分类管理时,利用动态树折叠数据是一个很好的方法,也就是点击数据前面的加号才展开对应下面的数据,如下图.那这样的效果在制作报表时该如何实现呢? 下面以报表工具FineReport为例介绍 ...
- 动态树之LCT(link-cut tree)讲解
动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...
- 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)
3589: 动态树 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 405 Solved: 137[Submit][Status][Discuss] ...
- BZOJ-2049 Cave洞穴勘测 动态树Link-Cut-Tree (并查集骗分TAT)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 5833 Solved: 2666 [Submit] ...
- 学习笔记-动态树Link-Cut-Tree
--少年你有梦想吗? --少年你听说过安利吗? 安利一个集训队讲解:http://wenku.baidu.com/view/75906f160b4e767f5acfcedb 关于动态树问题,有多种方法 ...
- BZOJ 3589 动态树(子树操作,链查询)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3589 题意:给出一棵有根树,两种操作:(1)以u为根的子树所有节点权值加上一个数字 ...
- Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s 内存限制:1.0GB 总提交次数:227 AC次数:67 平均分:49. ...
- 动态树 Link-Cut Trees
动态树 动态树问题, 即要求我们维护一个由若干棵子结点无序的有根树组成的森林. 要求这个数据结构支持对树的分割.合并,对某个点到它的根的路径的某些操作,以及对某个点的子树进行的某些操作. 在这里我们考 ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
随机推荐
- docker dead but pid file exists
CentOS 6安装docker 报docker dead but pid file exists 执行 yum install epel-release yum install docker-io ...
- asp.net Global.asax 不运行解决
asp.net application的站点发布后 Global.asax 未运行,搞了好久终于解决, 解决方法如下: publish设置 该设置经测试在win server 2003 和2008 都 ...
- ios开发常用RGB色值
iOS中RGB常用的色值,同时可将对颜色的设置定义成宏,方便开发应用,如: // RGB颜色转换(16进制->10进制) #define UIColorFromRGB(rgbValue) [UI ...
- 一个脚本从git上pull 并更新到服务器
#/bin/bash cd /src/pid01-beta/ echo "update pid01.." git pull ]; then echo "update pi ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- Windows Server 2016-图形化备份域控制器
上边几章节我们补充了有关Windows Server 2016系统层面的相关内容,本章切回Active Directory正题,继续围绕AD域相关内容进行不断梳理补充.Windows Server B ...
- Elasticsearch教程-从入门到精通(转载)
转载,原文地址:http://mageedu.blog.51cto.com/4265610/1714522?utm_source=tuicool&utm_medium=referral 各位运 ...
- linux 获取CPU个数
#include<stdio.h> #include<unistd.h> int main() { int cpu_num; cpu_num = sysconf(_SC_NPR ...
- 基于JDK1.8的LinkedList剖析
之前写了一篇ArrayList,那么今天就写一篇他的姊妹篇,LinkedList. 众所周知,ArrayList底层数据是数组,可以在O(1)的时间内get到数据,但删除和插入就要O(n)时间复杂度. ...
- 老男孩Python全栈开发(92天全)视频教程 自学笔记06
day6课程内容: tuple(元祖) 创建元祖: tup0=() #没有元素的一个元祖 tup1=(20,)#只有一个元素的元祖 元祖可读,不可修改 Dictionary(字典)#Python里唯一 ...