Splay的初步学习
具体是啥,qwq
有时间再补吧,贴一下代码;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#define MAXN 10086666
using namespace std;
int f[MAXN],cnt[MAXN],value[MAXN];
int sons[MAXN][],sub_size[MAXN];
int root,whole_size;
int m,num,be_dealt;
inline int read()
{
int x = ;
int f = ;
char ch = getchar();
while(!isdigit(ch))
{
if(ch == '-')
f = -;
ch = getchar();
}
while(isdigit(ch))
{
x = x * + ch - ;
ch = getchar();
}
return x * f;
}
inline void S_clear(int x)
{
sons[x][] = sons[x][] = ;
f[x] = cnt[x] = value[x] = ;
sub_size[x] = ;
}
inline bool get_which(int x)
{
return sons[f[x]][] == x;
}
inline void update(int x)
{
if(x)
{
sub_size[x] = cnt[x];
if(sons[x][])
sub_size[x] += sub_size[sons[x][]];
if(sons[x][])
sub_size[x] += sub_size[sons[x][]];
}
return ;
}
inline void rotate(int x)
{
int father = f[x];
int g_father = f[father];
int which_son = get_which(x);
sons[father][which_son] = sons[x][which_son ^ ];
f[sons[father][which_son]] = father;
sons[x][which_son ^ ] = father;
f[father] = x;
f[x] = g_father;
if(g_father)
sons[g_father][sons[g_father][] == father] = x;
update(father);
update(x);
}
inline void splay(int x)
{
for(int fa;fa = f[x];rotate(x))
if(f[fa])
rotate((get_which(x)) == get_which(fa) ? fa : x);
root = x;
}
inline void insert(int x)
{
if(!root)
{
whole_size++;
sons[whole_size][] = sons[whole_size][] = f[whole_size] = ;
root = whole_size;
sub_size[whole_size] = cnt[whole_size]++;
value[whole_size] = x;
return ;
}
int now = root;
int fa = ;
while()
{
if(x == value[now])
{
cnt[now]++;
update(now);
update(fa);
splay(now);
break;
}
fa = now;
now = sons[now][value[now] < x];
if(!now)
{
whole_size++;
sons[whole_size][] = sons[whole_size][] = ;
f[whole_size] = fa;
sub_size[whole_size] = cnt[whole_size] = ;
sons[fa][value[fa] < x] = whole_size;
value[whole_size] = x;
update(fa);
splay(whole_size);
break;
}
}
}
inline int find_sum(int x)
{
int now = root;
while()
{
if(sons[now][] && x <= sub_size[sons[now][]])
now = sons[now][];
else
{
int temp = (sons[now][] ? sub_size[sons[now][]] : ) + cnt[now];
if(x <= temp)
return value[now];
x -= temp;
now = sons[now][];
}
}
}
inline int find_num(int x)
{
int now = root;
while()
{
if(sons[now][] && x <= sub_size[sons[now][]])
now = sons[now][];
else
{
int temp = (sons[now][] ? sub_size[sons[now][]] : ) + cnt[now];
if(x <= temp)
return value[now];
x -= temp;
now = sons[now][];
}
}
}
inline int find_rank(int x)
{
int now = root;
int ans = ;
while()
{
if(x < value[now])
now = sons[now][];
else
{
ans += (sons[now][] ? sub_size[sons[now][]] : );
if(x >= value[now])
{
splay(now);
return ans + ;
}
ans += cnt[now];
now = sons[now][];
}
}
}
inline int find_pre()
{
int now = sons[root][];
while(sons[now][])
now = sons[now][];
return now;
}
inline int find_suffix()
{
int now = sons[root][];
while(sons[now][])
now = sons[now][];
return now;
}
inline void my_delete(int x)
{
int kkk = find_rank(x);
if(cnt[root] > )
{
cnt[root]--;
update(root);
return ;
}
if(!sons[root][] && !sons[root][])
{
S_clear(root);
root = ;
return ;
}
if(!sons[root][])
{
int old_root = root;
root = sons[root][];
f[root] = ;
S_clear(old_root);
return ;
}
else
if(!sons[root][])
{
int old_root = root;
root = sons[root][];
f[root] = ;
S_clear(old_root);
return ;
}
int left_max = find_pre();
int old_root = root;
splay(left_max);
sons[root][] = sons[old_root][];
f[sons[old_root][]] = root;
S_clear(old_root);
update(root);
}
int main()
{
scanf("%d",&m);
for(int i=;i<=m;i++)
{
num = read();
be_dealt = read();
switch(num)
{
case : insert(be_dealt);break;
case : my_delete(be_dealt);break;
case : printf("%d\n",find_rank(be_dealt));break;
case : printf("%d\n",find_num(be_dealt));break;
case : insert(be_dealt);printf("%d\n",value[find_pre()]);my_delete(be_dealt);break;
case : insert(be_dealt);printf("%d\n",value[find_suffix()]);my_delete(be_dealt);break;
}
}
return ;
}
Splay的初步学习的更多相关文章
- json2.js的初步学习与了解
json2.js的初步学习与了解,想要学习json的朋友可以参考下. json2.js的初步学习与了解 1.)该js的下载地址是:http://www.json.org/json2.js 2.)在页面 ...
- 老周的ABP框架系列教程 -》 一、框架理论初步学习
老周的ABP框架系列教程 -- 一.框架理论初步学习 1. ABP框架的来源与作用简介 1.1 简介 1.1.1 ABP框架全称为"ASP.NET Boilerplate ...
- 初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助
初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助,如果有bug或者更好的优化方案,也请批评与指正,谢谢,代码如下: var fs = require('f ...
- EF Codefirst 初步学习(二)—— 程序管理命令 更新数据库
前提:搭建成功codefirst相关代码,参见EF Codefirst 初步学习(一)--设置codefirst开发模式 具体需要注意点如下: 1.确保实体类库程序生成成功 2.确保实体表类库不缺少 ...
- 初步学习python
自计算机诞生以来,也伴随着计算机语言的诞生,现在,全世界的编程语言有600多种,但流行的编程语言也就20多种. Java和C一直占据着前两名.但是近年来伴随着人工智能的发展,Python发展迅猛,以其 ...
- Git的初步学习
前言 感谢! 承蒙关照~ Git的初步学习 为什么要用Git和Github呢?它们的出现是为了用于提交项目和存储项目的,是一种很方便的项目管理软件和网址地址. 接下来看看,一家公司的基本流程图: 集中 ...
- 语法分析器初步学习——LISP语法分析
语法分析器初步学习——LISP语法分析 本文参考自vczh的<如何手写语法分析器>. LISP的表达式是按照前缀的形式写的,比如(1+2)*(3+4)在LISP中会写成(*(+ 1 2)( ...
- 状态保持以及AJAX的初步学习
嘿嘿,今天学习的有点迷茫哦,主要学习把验证码使用在登录页面时间的一些逻辑,学习这个时间并没有那么的迷惑哦,可是自己写程序时间倒是有点反应迟钝,不过还好总是在最后搞清楚啦,另外就是一步一步的学习是接近项 ...
- LinQ的初步学习与总结
嘿嘿,说起来ORM和LinQ,就感觉离我好遥远的,在学校是没有学习的,所以总感觉学习了LinQ就是大神,现在嘛,终于也体会一点,感觉LinQ只是初步学习,没有太难,当然以后使用在项目中就没有这样的简单 ...
随机推荐
- 关于Sublime text 3如何编辑less并转(编译)成css文件
今天开始学习使用less这个强大方便的前端工具,本来是考虑用koala(专门编辑less的软件)来使用less的,但是发现sublime编辑器也可以实现对less的编译及高亮显示代码,这样既能少用一个 ...
- js工具库---Lodash
Lodash是一个一致性.模块化.高性能的 JavaScript 实用工具库 为什么选择 Lodash ? Lodash 通过降低 array.number.objects.string 等等的使用难 ...
- Java编写一个随机产生小学生四则运算题30道
//注:这个程序还没有实现的地方为分数的计算方法未能实现,只是简单的两个数运算,没有实现多个数,四则运算中的数没有涉及0. package 课堂测试1; import java.io.File; im ...
- 浅析PHP中的闭包和匿名函数
PHP闭包和匿名函数使用的句法与普通函数相同,但闭包和匿名函数其实是伪装成函数的对象(Closure类的实例) .下面给大家介绍PHP中的闭包和匿名函数知识,需要的朋友参考下吧 闭包是指在创建时封 ...
- poj3162 树形dp|树的直径 + 双单调队列|线段树,好题啊
题解链接:https://blog.csdn.net/shiqi_614/article/details/8105149 用树形dp是超时的,, /* 先求出每个点可以跑的最长距离dp[i][0|1] ...
- jmeter从CSV中获取非正常string
jmeter从CSV中获取非正常string,如CSV中有一列值为{"firstname":"Jade"},那么在beanshell中如何获取并解析? 一般的用 ...
- jmeter csv中获取带引号的数据详情
最近在工作中,对jmeter实践的点滴的记录这里分享,不一定正确,仅供参考和讨论,有想法的欢迎留言.谈论. 1技巧1:从csv中获取带引号的数据详情 背景:我们从csv中获取数据,在jmeter中使用 ...
- C++ Primer 笔记——const 限定符
1.因为const对象一旦创建后其值就不能再改变,所以const对象必须初始化. 2.默认情况下const对象只在文件内有效,如果想在多个文件之间共享const对象,必须在变量的定义之前添加exter ...
- bootstrap-table 刷新页面数据
bom.bootstrapTable('load',msg['object']);//这一步 务必要添加. if(msg['code']==1){ bom.find('tbody').css('dis ...
- 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化,而报【远程服务器返回错误: (500) 内部服务器错误】
1. 修改web.config文件可以改变这个默认值(上传1个G) <configuration> <system.web> <httpRuntime maxReques ...