题目链接: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. LeetCode Valid Anagram (简单题)

    题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序 ...

  2. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  3. 【强力卸载】使用Uninstall Tool卸载各类不易卸载的软件

    Uninstall Tool 经测试卸载MySql5.7.18成功. 下载地址: http://files.cnblogs.com/files/xiaohi/%E3%80%90%E8%BD%AF%E4 ...

  4. PHP:php遍历数组 foreach echo() list()总结

    php中可以用来遍历数组的方法有很多,如有:foreach语句.list().each(),这几个也是主要的方法,现总结如下: foreach语句遍历数组 foreach语句用于循环遍历数组,每进行一 ...

  5. web跨域及cookie相关知识总结

    原文:web跨域及cookie相关知识总结   之前对于跨域相关的知识一致都很零碎,正好现在的代码中用到了跨域相关的,现在来对这些知识做一个汇总整理,方便自己查看,说不定也可能对你有所帮助. 本篇主要 ...

  6. android studio 安装以及遇到的一些问题

    1 安装 jkd ,版本一般是最新的,下怎么样的看一下自己电脑符合那种要求,可以去官网下 https://www.oracle.com/technetwork/java/javase/download ...

  7. 零基础快速入门SpringBoot2.0教程 (二)

    一.SpringBoot2.x使用Dev-tool热部署 简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring. ...

  8. 基于GMap.NET地图下载器的开发和研究

    基于GMap.NET地图下载器的开发和研究 软件下载地址:https://pan.baidu.com/s/1ay0aOm3fiZ35vlfD8kFYFw 1.地图浏览功能 可以浏览谷歌地图.百度.ar ...

  9. 解决使用Application Loader上传ipa提示“上传appstore失败”

    试了好多次使用Application Loader上传ipa,一直提示上传失败,用其他mac电脑却可以,那就是环境有问题,笔者试过重装xcode,都无法解决问题, 查看日志类似是jdk版本问题,换了所 ...

  10. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...