题目链接: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)的更多相关文章

  1. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  2. 【洛谷P3369】普通平衡树——Splay学习笔记(一)

    二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...

  3. 洛谷.3369.[模板]普通平衡树(Splay)

    题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...

  4. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  5. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  6. 【模板篇】splay(填坑)+模板题(普通平衡树)

    划着划着水一不小心NOIP还考的凑合了… 所以退役的打算要稍微搁置一下了… 要准备准备省选了…. 但是自己已经啥也不会了… 所以只能重新拾起来… 从splay开始吧… splay我以前扔了个板子来着, ...

  7. 【BZOJ3224】Tyvj 1728 普通平衡树 Splay

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  8. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  9. luoguP3369[模板]普通平衡树(Treap/SBT) 题解

    链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...

  10. 平衡树——splay 一

    splay 一种平衡树,同时也是二叉排序树,与treap不同,它不需要维护堆的性质,它由Daniel Sleator和Robert Tarjan(没错,tarjan,又是他)创造,伸展树是一种自调整二 ...

随机推荐

  1. C#之linq

    本文根据30分钟LINQ教程学习作的笔记. 1.Guid.Empty Guid 结构: 表示全局唯一标识符 (GUID).Empty字段:Guid 结构的只读实例,其值均为零.用来设置初始值.   G ...

  2. OpenCV转为灰度图像 & 访问像素方法

    cvtColor(src, dst, CV_RGB2GRAY); 可转为灰度图像. 彩色图像像素访问:image.at<Vec3b>(i, j)[0],image.at<Vec3b& ...

  3. linux 命令——12 more (转)

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  4. libav(ffmpeg)简明教程(2)

    距离上一次教程又过去了将近一个多月,相信大家已经都将我上节课所说的东西所完全消化掉了. 这节课就来点轻松的,说说libav的命令使用吧. 注:遇到不懂的或者本文没有提到的可以用例如命令后加 --hel ...

  5. expect脚本中,变量的写法

    一.expect脚本中,变量的不同写法 shell脚本中定义时间变量的写法:time=`date "+%Y%m%d"` ==>>直接照搬到expect中,设置的变量是不 ...

  6. C#进阶之全面解析Lambda表达式

    引言 在实际的项目中遇到一个问题,我们经常在网上搜索复制粘贴,其中有些代码看着非常的简洁,比如Lambda表达式,但是一直没有去深入了解它的由来,以及具体的使用方法,所以在使用的时候比较模糊,其次,编 ...

  7. Servlet 的生命周期 及 注意事项 总结

    Servlet的生命周期 图解Servlet的生命周期 生命周期的各个阶段 实例化 :Servlet 容器创建 Servlet 的实例 初始化 :该容器调用init() 方法 请求处理 :如果请求Se ...

  8. C语言正整数除法向上取整

    在网上发现一个简单的向上取整方法: 这里我们用<>表示向上取整,[]表示向下取整,那么怎么来表示这个值呢? 我们可以证明: <N/M>=[(N-1)/M]+1    (0< ...

  9. Oracle数据库学习(三)

    6.关于null 数据库中null是一个未知数,没有任何值:进行运算时使用nvl,但是结果仍为空:在聚集函数中只有全部记录为空才会返回null. 7.insert插入 (1)单行记录插入 insert ...

  10. SummerVocation_Leaning--java动态绑定(多态)

    概念: 动态绑定:在执行期间(非编译期间)判断所引用的对象的实际类型,根据实际类型调用其相应的方法.如下例程序中,根据person对象的成员变量pet所引用的不同的实际类型调用相应的方法. 具体实现好 ...