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只是初步学习,没有太难,当然以后使用在项目中就没有这样的简单 ...
随机推荐
- Linux基础实操一
开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 使用命令退出虚拟终端2上登录的用户 使用快捷键切 ...
- C# Parallel并发执行相关问题
1.Parallel并发执行 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...
- poj2836 状态压缩dp
自己的做法是枚举i,j作为顶点的矩形,然后再更新状态S,但是这种做法是错误的 正解是先把所有矩形对求出来,然后枚举状态S,每个处理每个状态时再枚举已经求出的矩形对,用旧状态更新新状态 #include ...
- 右键菜单添加打开CMD选项
转载: https://www.cnblogs.com/mkdd/p/8649139.html#undefined 目前用的win7sp1系统,平时打开CMD窗口通常用三种方法:1.win+R然后输入 ...
- 为什么访问json接口出现文件下载
在IE9,10,11下,当服务器端返回数据格式为json,且明确设置Content-Type为”application/json;charset=utf-8“时,会提示文件下载.如图所示: 解决办法是 ...
- Java+selenium之WebDriver的抛出异常分析(七)
NoSuchElementException 1.检查元素的定位器是否正确 2.如果定位器正确,增加休眠时间 3.等待了足够的时间依然找不到的话,更换定位器的定位方式 NoSuchWindowExce ...
- Flask-WTF中的csrf保护
CSRF 保护 这部分文档介绍了 CSRF 保护. 为什么需要 CSRF? Flask-WTF 表单保护你免受 CSRF 威胁,你不需要有任何担心.尽管如此,如果你有不包含表单的视图,那么它们仍需要保 ...
- NodeJs——router报错原因
rout.js var http = require('http'); var url = require('url'); var router = require('./models/router. ...
- js中匿名函数和回调函数
匿名函数: 通过这种方式定义的函数:(没有名字的函数) 作用:当它不被赋值给变量单独使用的时候 1.将匿名函数作为参数传递给其他函数 2.定义某个匿名函数来执行某些一次性任务 var f = func ...
- 个人笔记本安装多个jdk(jdk1.7,jdk1.8,jdk1.9,jdk10.0)出现的问题
1.个人笔记本已经安装jdk1.7,jdk1.8,(之前没有在意这个问题).最近想学习jdk10.0,安装以后,环境变量变成了jdk10.0,就是cmd输入命令java -version,显示版本是j ...