题目大意:

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

1. 插入x数

2. 删除x数(若有多个相同的数,因只删除一个)

3. 查询x数的排名(若有多个相同的数,因输出最小的排名)

4. 查询排名为x的数

5. 求x的前驱(前驱定义为小于x,且最大的数)

6. 求x的后继(后继定义为大于x,且最小的数)

思路:

直接splay

今天才敲。。。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstdlib>
  5 #include<cstring>
  6 #include<algorithm>
  7 #include<vector>
  8 #include<queue>
  9 #define inf 2139062143
 10 #define ll long long
 11 #define MAXN 100100
 12 #define MOD 1000000007
 13 using namespace std;
 14 inline int read()
 15 {
 16     int x=0,f=1;char ch=getchar();
 17     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
 18     while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
 19     return x*f;
 20 }
 21 int ch[MAXN][2],fa[MAXN],sz,cnt[MAXN],val[MAXN],size[MAXN],rt;
 22 int which(int x) {return x==ch[fa[x]][1];}
 23 int find_pre()
 24 {
 25     int pos=ch[rt][0];
 26     while(ch[pos][1]) pos=ch[pos][1];
 27     return pos;
 28 }
 29 int find_sub()
 30 {
 31     int pos=ch[rt][1];
 32     while(ch[pos][0]) pos=ch[pos][0];
 33     return pos;
 34 }
 35 void upd(int x)
 36 {
 37     if(!x) return ;
 38     size[x]=cnt[x]+size[ch[x][1]]+size[ch[x][0]];
 39 }
 40 void rotate(int pos)
 41 {
 42     int f=fa[pos],ff=fa[f],k=which(pos);
 43     ch[f][k]=ch[pos][k^1],fa[ch[f][k]]=f,fa[f]=pos,ch[pos][k^1]=f,fa[pos]=ff;
 44     if(ff) ch[ff][ch[ff][1]==f]=pos;
 45     upd(f);upd(pos);
 46 }
 47 void splay(int x)
 48 {
 49     for(int f;f=fa[x];rotate(x))
 50         if(fa[f]) rotate((which(x)==which(f)?f:x));
 51     rt=x;
 52 }
 53 void Insert(int x)
 54 {
 55     int pos=rt,f=0;
 56     while(1)
 57     {
 58         if(val[pos]==x) {cnt[pos]++,upd(pos);upd(f);splay(pos);return ;}
 59         f=pos,pos=ch[pos][x>val[pos]];
 60         if(!pos)
 61         {
 62             ch[++sz][0]=ch[sz][1]=0,fa[sz]=f,val[sz]=x,cnt[sz]=size[sz]=1,ch[f][x>val[f]]=sz;
 63             upd(f);splay(sz);return ;
 64         }
 65     }
 66 }
 67 void insert(int x)
 68 {
 69     if(!rt) {val[++sz]=x,ch[sz][0]=ch[sz][1]=fa[sz]=0,cnt[sz]=size[sz]=1,rt=sz;return;}
 70     Insert(x);
 71 }
 72 int find_rank(int x)
 73 {
 74     int res=0,pos=rt;
 75     while(1)
 76     {
 77         if(x<val[pos]) pos=ch[pos][0];
 78         else
 79         {
 80             res+=size[ch[pos][0]];
 81             if(val[pos]==x) {splay(pos);return res+1;}
 82             res+=cnt[pos],pos=ch[pos][1];
 83         }
 84     }
 85 }
 86 int find_num(int x)
 87 {
 88     int tmp=0,pos=rt;
 89     while(1)
 90     {
 91         if(ch[pos][0]&&x<=size[ch[pos][0]]) pos=ch[pos][0];
 92         else
 93         {
 94             tmp=size[ch[pos][0]]+cnt[pos];
 95             if(x<=tmp) return val[pos];
 96             x-=tmp,pos=ch[pos][1];
 97         }
 98     }
 99 }
100 void dlt(int x)
101 {
102     find_rank(x);
103     if(cnt[rt]>1) {cnt[rt]--;return ;}
104     if(!ch[rt][0]&&!ch[rt][1]) {rt=0;return ;}
105     if(!ch[rt][0]||!ch[rt][1])
106     {
107         int k=!ch[rt][1]?0:1;
108         rt=ch[rt][k],fa[rt]=0;
109         return ;
110     }
111     int k=find_pre(),tmp=rt;
112     splay(k);fa[ch[tmp][1]]=rt;
113     ch[rt][1]=ch[tmp][1],rt=k;
114 }
115 int main()
116 {
117     int T=read(),opt,x;
118     while(T--)
119     {
120         opt=read(),x=read();
121         if(opt==1) insert(x);
122         if(opt==2) dlt(x);
123         if(opt==3) printf("%d\n",find_rank(x));
124         if(opt==4) printf("%d\n",find_num(x));
125         if(opt==5) {insert(x);printf("%d\n",val[find_pre()]);dlt(x);}
126         if(opt==6) {insert(x);printf("%d\n",val[find_sub()]);dlt(x);}
127     }
128 }

View Code

转载于:https://www.cnblogs.com/yyc-jack-0920/p/8047274.html

bzoj 3224 Tyvj 1728 普通平衡树相关推荐

  1. BZOJ 3224: Tyvj 1728 普通平衡树【Treap】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一些数 ...

  2. BZOJ 3224: Tyvj 1728 普通平衡树 treap

    3224: Tyvj 1728 普通平衡树 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  3. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 9629  Solved: 4091 [Submit][S ...

  4. Treap树堆(bzoj 3224: Tyvj 1728 普通平衡树)

    Treap树堆:一种排序二叉树(中序遍历权值有序) 每个节点有两个关键字:key[]和rand[] 其中key[]满足二叉搜索树性质,rand[]满足堆性质(即Tree+Heap=Treap)即 如果 ...

  5. 【Treap】[BZOJ 3224]Tyvj 1728 普通平衡树

    平衡树的入门题目,也可以用Treap来实现,我觉得Treap的核心就是那个rand()了用来保证树不退化成链,同时还是有平衡树的特性..总体来说我觉得比较好些,而且比较快 #include <c ...

  6. bzoj:3224: Tyvj 1728 普通平衡树

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

  7. 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14480  Solved: 6275 Descripti ...

  8. 【BZOJ】3224: Tyvj 1728 普通平衡树

    [题意] 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相同的数,因输出最小的排名) 4. 查询排名为x的数 5. 求x的前驱(前驱定义为小于x,且最 ...

  9. bzoj3224: Tyvj 1728 普通平衡树(splay)

    3224: Tyvj 1728 普通平衡树 题目:传送门 题解: 啦啦啦啦又来敲个模版水经验啦~ 代码: 1 #include<cstdio> 2 #include<cstring& ...

最新文章

  1. 【转载】目前为止看到描述VSCode编写C++配置文件最清楚的一篇文章
  2. 阿里巴巴与清华大学成立联合实验室,探索下一代人机自然交互
  3. 26行代码AC_试题 历届试题 日期问题 | 第八届蓝桥杯B组第七题
  4. Feign 超时设置
  5. 公用表表达式(CTE)
  6. python爬电影_零基础Python爬虫实现(爬取最新电影排行)
  7. 高质量外链该怎样做?
  8. 黑莓7100刷机及修改PIN,完美破解超越输入法
  9. python 爬虫之路教程
  10. LCS算法 文本对比的实现
  11. 宝塔Linux面板 软件商店中安装不了任何php版本的解决方法
  12. 计算机控制室防火危险级为,8.2 空气调节 - 【已作废】火力发电厂与变电站设计防火规范 GB50229-2006 - 消防规范大全 - 消防资源网!...
  13. 用Arduino板为另一块Arduino烧写(更新)BootLoader
  14. css-loader和style-loader
  15. 浅析Linux下的子系统
  16. 【机器学习】word2vec学习笔记(三):word2vec源码注释
  17. 矩阵的压缩存储(随机稀疏矩阵的建立和输出)
  18. [阿发你好]C/C++学习指南
  19. Jackson - 将 JSON字符串转换为 List
  20. linux版teamview15.5.3

热门文章

  1. VHDL中的分辨函数
  2. 搜狗输入法在idea打不了汉字_IDEA开发软件在linux环境下使用搜狗输入法无法进行中文输入...
  3. etl数据抽取工具_数据同步工具ETL、ELT傻傻分不清楚?3分钟看懂两者区别
  4. java 限制参数类型_java定义受限制的类型参数操作
  5. springcloud 整合 gateway_从Spring Cloud到Kubernetes的微服务迁移实践
  6. 5.Lambert光照Diffuse Shader
  7. c语言程序后退_c语言中向后退一格是啥符号?
  8. HMM前向算法,维比特算法,后向算法,前向后向算法代码
  9. Training a classifier
  10. SQL游标使用方法SQL游标使用方法(转)