题目链接: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. HDU3371 Connect the Cities

    题目描述: 有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的.现在想把所有的岛都连通起来,求最少的花费是多少. 输入: 第一行输入T,代表多少组数据. ...

  2. pat乙级1049

    浮点型乘整型和整型乘浮点型结果不同,不知为什么. double sum = 0.0; ; i < n; i++) { cin >> a[i]; sum += a[i] * (i + ...

  3. c++ 输入split

    日期格式为“yyyy/mm/dd”(即年/月/日)格式 scanf("%d/%d/%d", &year, &month, &day);

  4. coursera 算法二 week 1 wordnet

    这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...

  5. 让SAP云平台上的Web应用使用destination服务

    首先在SAP云平台里创建一个destination,维护service的end point: 然后打开SAP云平台的WebIDE,创建一个新的文件夹和新的HTML5 Application Descr ...

  6. 【BZOJ1059】[ZJOI2007] 矩阵游戏(匈牙利算法)

    点此看题面 大致题意: 有一个\(N*N\)的\(01\)矩阵,可以任意交换若干行和若干列,问是否有方案使得左上角到右下角的连线上全是\(1\). 题意转换 首先,让我们来对题意进行一波转化. 如果我 ...

  7. 2017.12.22 Java序列化中你不知道的事(一)

    Java 序列化简介 Java 对象序列化是 JDK 1.1 中引入的一组开创性特性之一,用于作为一种将 Java 对象的状态转换为字节数组,以便存储或传输的机制,以后,仍可以将字节数组转换回 Jav ...

  8. css img 等比例自动缩放

    按父容器宽度自动缩放,并且保持图片原本的长宽比 img{ width: auto; height: auto; max-width: 100%; max-height: 100%; }

  9. 管道命令'|' 和xargs find命令找到后把所有找到的删除

    管道符号,是unix功能强大的一个地方,符号是一条竖线:"|", 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为comm ...

  10. 【DBA-Oracle】更改Oracle数据字符集_转为常用的ZHS16GBK

    A.oracle server 端 字符集查询  select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LAN ...