P3369 【模板】普通平衡树(Splay)
题目链接:https://www.luogu.org/problemnew/show/P3369
修改了一下之前的模板,支持重复数值的存储
#include<bits/stdc++.h>
using namespace std;
struct node
{
int val;
node *father;
node *son[];
int cnt;
int siz;
} tree[],*root;
inline void init(node *p,int val=)
{
p->father=NULL;
p->son[]=p->son[]=NULL;
p->val=val;
p->siz=;
p->cnt=;
}
inline int siz(node *t)
{
return t == NULL ? : t->siz;
}
inline void update(node *t)//rotate与erase操作时需更新
{
t->siz = t->cnt;
t->siz += siz(t->son[]);
t->siz += siz(t->son[]);
}
inline bool son(node *f, node *s)
{
return f->son[] == s;
}
inline void rotate(node *t)
{
node *f = t->father;
node *g = f->father;
bool a = son(f, t), b = !a;
f->son[a] = t->son[b];
if (t->son[b] != NULL)
t->son[b]->father = f;
t->son[b] = f;
f->father = t;
t->father = g;
if (g != NULL)
g->son[son(g, f)] = t;
else
root = t;
update(t);
update(f);
}
inline void splay(node *t, node *p)
{
while (t->father != p)
{
node *f = t->father;
node *g = f->father;
if (g == p)
rotate(t);
else
{
if (son(g, f) ^ son(f, t))
rotate(t), rotate(t);
else
rotate(f), rotate(t);
}
}
update(t);
if(p!=NULL) update(p);
}
inline void insert(node* p)
{
if (root == NULL)
{
root = p;
return;
}
for(node* t=root; t; t = t->son[t->val < p->val])
{
if(t->val==p->val)
{
t->cnt++;
splay(t,NULL);
return;
}
if(t->son[t->val < p->val]==NULL)
{
t->son[t->val < p->val]=p;
p->father=t;
splay(p,NULL);
return;
}
}
}
inline void erase(node *t)
{
splay(t,NULL);
if (t->son[] == NULL)
{
root = t->son[];
if (root != NULL)
root->father = NULL;
}
else
{
node *p = t->son[];
while (p->son[] != NULL)
p = p->son[];
splay(p, t);
root = p;
root->father = NULL;
p->son[] = t->son[];
if (p->son[] != NULL)
p->son[]->father = p;
update(p);
}
}
int n,m;
bool flag;
inline node* findx(int kth)
{
node* p=root;
while()
{
if(kth<=siz(p->son[])) p=p->son[];
else
{
kth-=siz(p->son[])+p->cnt;
if(kth<=) return p;
p=p->son[];
}
}
}
inline node* findkth(int x)
{
node* p=root;
while()
{
if(p==NULL) return NULL;
if(p->val==x) return p;
p=p->son[p -> val < x];
}
}
int main()
{
int m;
scanf("%d",&m);
int tot=;
for(int i=;i<=m;i++)
{
int op;
scanf("%d",&op);
if(op==)
{
int x;
scanf("%d",&x);
init(&tree[++tot],x);
insert(&tree[tot]);
}
if(op==)
{
int x;
scanf("%d",&x);
node* tmp=findkth(x);
if(tmp!=NULL)
{
tmp->cnt--;
if(!tmp->cnt)
erase(tmp);
else
splay(tmp,NULL);
}
}
if(op==)
{
int x;
scanf("%d",&x);
node* tmp=findkth(x);
splay(tmp,NULL);
if(tmp->son[]==NULL)
puts("");
else
printf("%d\n",tmp->son[]->siz+);
}
if(op==)
{
int kth;
scanf("%d",&kth);
node *tmp=findx(kth);
if(tmp!=NULL) printf("%d\n",tmp->val);
}
if(op==)
{
int x;
scanf("%d",&x);
node* p=root;
int tmp;
while()
{
if(p==NULL) break;
if(p->val>=x)
p=p->son[];
else
{
tmp=p->val;
p=p->son[];
}
}
printf("%d\n",tmp);
}
if(op==)
{
int x;
scanf("%d",&x);
node* p=root;
int tmp;
while()
{
if(p==NULL) break;
if(p->val<=x)
p=p->son[];
else
{
tmp=p->val;
p=p->son[];
}
}
printf("%d\n",tmp);
}
}
}
P3369 【模板】普通平衡树(Splay)的更多相关文章
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- 【模板篇】splay(填坑)+模板题(普通平衡树)
划着划着水一不小心NOIP还考的凑合了… 所以退役的打算要稍微搁置一下了… 要准备准备省选了…. 但是自己已经啥也不会了… 所以只能重新拾起来… 从splay开始吧… splay我以前扔了个板子来着, ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 平衡树——splay 一
splay 一种平衡树,同时也是二叉排序树,与treap不同,它不需要维护堆的性质,它由Daniel Sleator和Robert Tarjan(没错,tarjan,又是他)创造,伸展树是一种自调整二 ...
随机推荐
- badboy页面脚本发生错误,解决方案
1.参考网址:https://jingyan.baidu.com/article/e9fb46e17537797520f76645.html?from=qqbrowser061108 本人亲自测试,方 ...
- 如何在Java代码中使用SAP云平台CloudFoundry环境的环境变量
本文使用的例子源代码在我的github上. 在我的公众号文章在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务介绍了如何通过Cloud Connector ...
- android RadioGroup设置某一个被选中
见码滚 mPriorityRadioGroup.clearCheck(); mStatusRadioGroup.clearCheck(); RadioButton r1 = (RadioButton) ...
- log4j.properties中的这句话“log4j.logger.org.hibernate.SQL=DEBUG ”该怎么写在log4j.xml里面呢?
http://www.cnblogs.com/gredswsh/p/log4j_xml_properties.html 请问:log4j.properties中的这句话“log4j.logger.or ...
- UVA 10375 Choose and divide(大数的表示)
紫上给得比较奇怪,其实没有必要用唯一分解定理.我觉得这道题用唯一分解只是为了表示大数. 但是分解得到的幂,累乘的时候如果顺序很奇怪也可能溢出.其实直接边乘边除就好了.因为答案保证不会溢出, 设定一个精 ...
- 2018.10.29 NOIP2018模拟赛 解题报告
得分: \(70+60+0=130\)(\(T3\)来不及打了,结果爆\(0\)) \(T1\):简单的求和(点此看题面) 原题: [HDU4473]Exam 这道题其实就是上面那题的弱化版,只不过把 ...
- DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解
本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参 ...
- 什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?
SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...
- Node 操作MySql数据库
1, 需要安装 MySQL 依赖 => npm i mysql -D 2, 封装一个工具类 mysql-util.js // 引入 mysql 数据库连接依赖 const mysql = re ...
- centos 关闭AliYunDun
执行命令: service aegis stop #停止服务 chkconfig --del aegis # 删除服务