题目链接: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. 关于android界面菜单,project显示问题

    刚用android studio不久,遇到了一个问题:界面菜单project不小心被关掉了,百度各种搜索无结果.我想大多数人都遇到过类似问题,此类问题其实很简单但是对于初学者来说就有点苦恼了.所以在此 ...

  2. 安装express

    就目前来说安装express需要走几个步骤,要不就会出现在检查版本的时候就会出现,expres不是内部的命令或者是这种 安装的步骤: 1. 先是输入npm install -g express-gen ...

  3. js实现排序去重计算字符次数

    /*去重*/ var arr=[1,4,4,7,3,9,0,3,2,1,"你好","你","你好","你 "]; var ...

  4. 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 ...

  5. Android(java)学习笔记81:在TextView组件中利用Html插入文字或图片

    1. TextView中利用Html插入文字或者图片: 首先我们看看代码: (1)activity_main.xml: <LinearLayout xmlns:android="htt ...

  6. 【BZOJ2006】[NOI2010] 超级钢琴(堆+RMQ)

    点此看题面 大致题意: 要你求出区间和前\(k\)大的区间的区间和之和,其中每个区间的大小在\(L\)与\(R\)之间. 堆+\(RMQ\) 这道题目,我们可以先对\(1\sim n\)中的每一个\( ...

  7. 动态规划专题(一)——状压DP

    前言 最近,决定好好恶补一下我最不擅长的\(DP\). 动态规划的种类还是很多的,我就从 状压\(DP\) 开始讲起吧. 简介 状压\(DP\)应该是一个比较玄学的东西. 由于它的时间复杂度是指数级的 ...

  8. iOS 集成支付宝过程中 我遇到的一些坑,请大家注意啦(ALI69错误,ALI64错误)

    支付宝很早一段时间就集成了,之前由于一直忙于开发就没有总结,今天整理桌面的时候看到,当时做支付时候的一些散落的笔记,就稍微整理一下,给大家分享一下. 第一:当时调用支付宝的时候,总是调不起来,进过断点 ...

  9. java中的两同两小一大原则

    子类覆盖父类要遵循“两同两小一大” “两同”即方法名相同,形参列表相同 “两小”指的是子类方法返回值类型应比父类方法返回值类型更小或相等,子类方法声明抛出的异常类应比父类方法声明抛出的异常类更小或相等 ...

  10. Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码

    随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox. Radiobutton . DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的 ...