脑洞大开加偏执人格——可持久化treap版的Link Cut Tree2
试了一下先上再下的Treap方式,很高兴,代码变短了,但是,跑的变慢了!!!其实慢得不多,5%左右。而且这个版本的写法不容易写错。。只要会一般可持久化Treap的人写着都不难。。。就是相对于(压行的)Splay还是长了点。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm> using namespace std;
int father[];
struct node
{
int num;
int key;
int left;
int right;
node* ls;
node* rs;
bool turn;
node* f;
node()
{
key=rand();
f=NULL;
}
}no[]; int root; void swap(node** a,node** b)
{
node* c=*a;
*a=*b;
*b=c;
} void swap(int* a,int *b)
{
int c=*a;
*a=*b;
*b=c;
} void pushdown(node* now)
{
if (now->turn)
{
swap(&now->ls,&now->rs);
if (now->ls)
{
now->ls->turn=!now->ls->turn;
swap(&now->ls->left,&now->ls->right);
}
if (now->rs)
{
now->rs->turn=!now->rs->turn;
swap(&now->rs->left,&now->rs->right);
}
}
now->turn=false;
} void update(node* now)
{
now->left=now->right=now->num;
if (now->ls)
{
now->left=now->ls->left;
now->ls->f=now;
}
if (now->rs)
{
now->right=now->rs->right;
now->rs->f=now;
}
} node* merge(node* a,node* b)
{
if (!a) {update(b);return b;}
if (!b) {update(a);return a;}
pushdown(a);
pushdown(b);
if (a->key<b->key)
{
a->rs=merge(a->rs,b);
a->rs->f=a;
update(a);
return a;
}
else
{
b->ls=merge(a,b->ls);
b->ls->f=b;
update(b);
return b;
}
} struct nodepair
{
node* l;
node* r; nodepair(node* a,node* b)
{
l=a;
r=b;
}
}; bool Ro[];
int fi=; int findroot(int num)
{
node* now=&no[num];
while (now->f)
{
if (now->f->ls==now) Ro[fi++]=true;
else
Ro[fi++]=false;
now=now->f;
}
return now->num;
} nodepair split(node* now,int dep)
{
if (dep==)
{
pushdown(now);
nodepair ret(now,now->rs);
if (now->rs)
{
now->rs->f=NULL;
now->rs=NULL;
}
now->f=NULL;
update(now);
return ret;
}
if (now->turn) Ro[dep]=!Ro[dep];
pushdown(now);
if (Ro[dep])
{
nodepair km=split(now->ls,dep-);
now->ls=km.r;
update(now);
now->f=NULL;
return nodepair(km.l,now);
}
else
{
nodepair km=split(now->rs,dep-);
now->rs=km.l;
update(now);
now->f=NULL;
return nodepair(now,km.r);
}
} nodepair split(int num)
{
fi=;
int root=findroot(num);
return split(&no[root],fi-);
} node* access(int num)
{
int now=num;
node* lasttree=NULL;
while (now!=)
{
nodepair km=split(now);
if (lasttree)
father[lasttree->left]=;
lasttree = merge(km.l,lasttree);
if (km.r)
father[km.r->left]=now;
now=father[lasttree->left];
}
return lasttree;
} bool query(int x,int y)
{
int k1=access(x)->left;
int k2=access(y)->left;
return k1==k2;
} void changeroot(int n)
{
node* road=access(n);
road->turn=!road->turn;
swap(&road->left,&road->right);
} void Connect(int x,int y)
{
changeroot(y);
access(x);
father[y]=x;
} void destroy(int x,int y)
{
changeroot(x);
access(x);
father[y]=;
} int main()
{
int n;
cin>>n;
for (int i=;i<=n;++i)
{
no[i].num=i;
father[i]=;
}
int q;
char cmd[];
scanf("%d",&q);
for (int i=;i<=q;++i)
{
int j,k;
scanf("%s%d%d",cmd,&j,&k);
if (cmd[]=='C')
{
Connect(j,k);
}
if (cmd[]=='Q')
{
if (query(j,k))
printf("Yes\n");
else
printf("No\n");
}
if (cmd[]=='D')
{
destroy(j,k);
}
}
return ;
}
我在试试压一下代码吧,总之不想学Splay。。。
脑洞大开加偏执人格——可持久化treap版的Link Cut Tree2的更多相关文章
- 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree
一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...
- 平衡树与可持久化treap
平衡树(二叉树) 线段树不支持插入or删除一个数于是平衡树产生了 常见平衡树:treap(比sbt慢,好写吧),SBT(快,比较好写,有些功能不支持),splay(特别慢,复杂度当做根号n来用,功能强 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- BZOJ 3595: [Scoi2014]方伯伯的Oj SBT+可持久化Treap
3595: [Scoi2014]方伯伯的Oj Time Limit: 6 Sec Memory Limit: 256 MBSubmit: 102 Solved: 54[Submit][Status ...
- 【模板】可持久化文艺平衡树-可持久化treap
题目链接 题意 对于各个以往的历史版本实现以下操作: 在第 p 个数后插入数 x . 删除第 p 个数. 翻转区间 [l,r],例如原序列是 \(\{5,4,3,2,1\}\),翻转区间 [2,4] ...
- 高rong效chang的可持久化treap
很多人觉得可持久化treap很慢,但是事实上只是他们可持久化treap的写法不对.他们一般是用split和merge实现所有功能,但是这样会有许多不必要的分裂.其实我们可以用一种特殊的方式来实现插入和 ...
- 可持久化Treap
终于写了一次可持久化Treap,做的是可持久化序列的模板题. Treap Treap=Tree+Heap,是一个随机化的数据结构.它的每个节点至少有两个关键字,一个是我们要存储的\(val\),一个是 ...
- Codeforces - 38G 可持久化Treap 区间操作
题意:\(n\)个人排队,每个人有重要度\(p\)和不要脸度\(c\),如果第\(i\)个人的重要度大于第\(i-1\)个人的重要度,那么他们之间可以交换,不要脸度-1,交换后先前的第\(i\)个人也 ...
- Codeforces - 675D 可持久化Treap 树形操作
题意:模拟二叉树的构造过程,给出\(n\)个节点,每次从根插入,小于当前节点转到左儿子,否则右儿子,输出第\([2,n]\)个节点的父亲的权值 直接手动模拟会被链式结构T掉 网上找了下发现二叉树的性质 ...
随机推荐
- CAD计算两曲线间最短路径(com接口)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Day 12 字符串和正则表达式
使用正则表达式 正则表达式相关知识 在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要,正则表达式就是用于描述这些规则的工具,换句话说正则表达式是一种工具,它定义了字符串的匹配 ...
- Linux:安装CentOS 6.x和CentOS 7.x
1,准备镜像,这里到阿里云镜像网站下载 https://opsx.alibaba.com/mirror 2,安装CentOS6.10 打开vmware workstation--> 典型--&g ...
- Linux之内核管理及故障排错
一.Centos6启动流程: 加电自检PSOT 引导加载器BootLoader MBR(GRUB第一阶段)||(GRUB第1.5阶段)(GRUB第2阶段) 加载内核(vmlinuz.initramfs ...
- LVM和RAID
RAID: Redundant Arrays of Inexpensive Disks Independent Berkeley: A case for Redundent Arrays of Ine ...
- python最好用的IDE及查看源码的方法
一.PyCharm 很多语言都有比较流行的开发工具,比如JAVA 的Eclipse, C#,C++的VisualStudio,最好的Python 开发IDE就是PyCharm 可以直接调试代码,试运行 ...
- 第九节:numpy之随机数组及随机排列
- 个人学习记录--取表中Name相同的最大值,非Group By,可延伸
), qy ), je INT); INSERT INTO @t SELECT '产品一', '北京', UNION ALL SELECT '产品一', '上海', UNION ALL SELECT ...
- 【IntelliJ IDEA】idea上安装Translation插件后,需要AppKey才能生效的解决方案
使用idea安装的翻译插件translation,但是使用的时候并不友好 无奈,如果想使用翻译软件并且更方便的话,可以如下: 可以选择将translation进行卸载 清除缓存并进行重启 然后再启动之 ...
- cocoa 运行apple脚本文件的方法
NSString* path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt&q ...