bzoj3224: Tyvj 1728 普通平衡树(splay)
3224: Tyvj 1728 普通平衡树
题目:传送门
题解:
啦啦啦啦又来敲个模版水经验啦~
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 struct node 8 { 9 int d,c,n,f,son[2]; 10 }tr[110000];int len,root; 11 void add(int d,int f) 12 { 13 len++;tr[len].d=d;tr[len].n=tr[len].c=1; 14 tr[len].son[0]=tr[len].son[1]=0;tr[len].f=f; 15 if(d<tr[f].d)tr[f].son[0]=len; 16 else tr[f].son[1]=len; 17 } 18 void update(int x) 19 { 20 int lc=tr[x].son[0],rc=tr[x].son[1]; 21 tr[x].c=tr[lc].c+tr[rc].c+tr[x].n; 22 } 23 int findip(int d) 24 { 25 int x=root; 26 while(tr[x].d!=d) 27 { 28 if(d<tr[x].d) 29 { 30 if(tr[x].son[0]==0)break; 31 else x=tr[x].son[0]; 32 } 33 else 34 { 35 if(tr[x].son[1]==0)break; 36 else x=tr[x].son[1]; 37 } 38 } 39 return x; 40 } 41 void rotate(int x,int w) 42 { 43 int f=tr[x].f,ff=tr[f].f; 44 int r,R; 45 r=tr[x].son[w],R=f; 46 tr[R].son[1-w]=r; 47 if(r!=0)tr[r].f=R; 48 49 r=x,R=ff; 50 if(tr[R].son[0]==f)tr[R].son[0]=r; 51 else tr[R].son[1]=r; 52 tr[r].f=R; 53 54 r=f,R=x; 55 tr[R].son[w]=r; 56 tr[r].f=R; 57 58 update(f);update(x); 59 } 60 void splay(int x,int rt) 61 { 62 while(tr[x].f!=rt) 63 { 64 int f=tr[x].f,ff=tr[f].f; 65 if(ff==rt) 66 { 67 if(tr[f].son[0]==x)rotate(x,1); 68 else rotate(x,0); 69 } 70 else 71 { 72 if(tr[ff].son[0]==f && tr[f].son[0]==x)rotate(f,1),rotate(x,1); 73 else if(tr[ff].son[1]==f && tr[f].son[1]==x)rotate(f,0),rotate(x,0); 74 else if(tr[ff].son[1]==f && tr[f].son[0]==x)rotate(x,1),rotate(x,0); 75 else if(tr[ff].son[0]==f && tr[f].son[1]==x)rotate(x,0),rotate(x,1); 76 } 77 } 78 if(rt==0)root=x; 79 } 80 void ins(int d) 81 { 82 if(root==0) 83 { 84 add(d,0);root=len; 85 return ; 86 } 87 int x=findip(d); 88 if(tr[x].d==d) 89 { 90 tr[x].n++; 91 update(x); 92 splay(x,0); 93 } 94 else 95 { 96 add(d,x); 97 update(x); 98 splay(len,0); 99 } 100 } 101 void del(int d) 102 { 103 int x=findip(d);if(tr[x].d!=d)return ; 104 splay(x,0); 105 if(tr[x].n>1){tr[x].n--;update(x);return ;} 106 if(tr[x].son[0]==0 && tr[x].son[1]==0){root=0;len=0;} 107 else if(tr[x].son[0]!=0 && tr[x].son[1]==0){root=tr[x].son[0];tr[root].f=0;} 108 else if(tr[x].son[0]==0 && tr[x].son[1]!=0){root=tr[x].son[1];tr[root].f=0;} 109 else 110 { 111 int p=tr[x].son[0]; 112 while(tr[p].son[1]!=0)p=tr[p].son[1]; 113 splay(p,x); 114 115 int r=tr[x].son[1],R=p; 116 tr[R].son[1]=r; 117 tr[r].f=R; 118 119 root=p;tr[root].f=0; 120 update(root); 121 } 122 } 123 void findpaiming(int d) 124 { 125 int x=findip(d);splay(x,0);int lc=tr[x].son[0]; 126 printf("%d\n",tr[lc].c+1); 127 } 128 void findshuzi(int k) 129 { 130 if(tr[root].c<k){printf("-1\n");return ;} 131 int x=root; 132 while(1) 133 { 134 int lc=tr[x].son[0],rc=tr[x].son[1]; 135 if(k<=tr[lc].c)x=lc; 136 else if(k>tr[lc].c+tr[x].n)k-=tr[lc].c+tr[x].n,x=rc; 137 else break; 138 } 139 printf("%d\n",tr[x].d); 140 } 141 void findqianqu(int d) 142 { 143 int x=findip(d);splay(x,0); 144 if(d<=tr[x].d && tr[x].son[0]!=0) 145 { 146 x=tr[x].son[0]; 147 while(tr[x].son[1]!=0)x=tr[x].son[1]; 148 } 149 if(d<=tr[x].d)x=0; 150 printf("%d\n",tr[x].d); 151 } 152 void findhouji(int d) 153 { 154 int x=findip(d);splay(x,0); 155 if(d>=tr[x].d && tr[x].son[1]!=0) 156 { 157 x=tr[x].son[1]; 158 while(tr[x].son[0]!=0)x=tr[x].son[0]; 159 } 160 if(d>=tr[x].d)x=0; 161 printf("%d\n",tr[x].d); 162 } 163 /* 164 void dell(int l,int r) 165 { 166 int lc=findqianqu(tr[l].d),rc=findhouji(tr[r].d); 167 spaly(lc,0);spaly(rc,lc); 168 tr[rc].son[0]=0;len-=tr[tr[rc].lc].c; 169 update(rc);update(lc); 170 } 171 */ 172 int main() 173 { 174 int n;scanf("%d",&n);root=0;len=0; 175 for(int i=1;i<=n;i++) 176 { 177 int opt,d;scanf("%d%d",&opt,&d); 178 if(opt==1)ins(d); 179 else if(opt==2)del(d); 180 else if(opt==3)findpaiming(d); 181 else if(opt==4)findshuzi(d); 182 else if(opt==5)findqianqu(d); 183 else if(opt==6)findhouji(d); 184 } 185 return 0; 186 }
转载于:https://www.cnblogs.com/CHerish_OI/p/8810443.html
bzoj3224: Tyvj 1728 普通平衡树(splay)相关推荐
- bzoj3224: Tyvj 1728 普通平衡树(打个splay暖暖手)
(其实今天好热啊? 题目大意:插入,删除,k小,前驱后继,数的排名. splay和treap裸题...过几天补个treap的 splay: #include<iostream> #incl ...
- bzoj3224 Tyvj 1728 普通平衡树题解--Treap
题面: Description您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有 ...
- 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
冬令营被平衡树坑了之后,打算苦练一番数据结构(QAQ). 先是打了一下想学好久的替罪羊树. 替罪羊树实现方法很简单,就是在不满足平衡条件的时候暴力重构子树. 调试小结: 1.删除操作分两类情况:如果某 ...
- 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14480 Solved: 6275 Descripti ...
- BZOJ 3224: Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- BZOJ 3224: Tyvj 1728 普通平衡树【Treap】
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一些数 ...
- BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 9629 Solved: 4091 [Submit][S ...
- Treap树堆(bzoj 3224: Tyvj 1728 普通平衡树)
Treap树堆:一种排序二叉树(中序遍历权值有序) 每个节点有两个关键字:key[]和rand[] 其中key[]满足二叉搜索树性质,rand[]满足堆性质(即Tree+Heap=Treap)即 如果 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
最新文章
- (C++)1027 打印沙漏
- php7 java8_php7 vs java8 vs nodejs5 vs lua5.2 计算性能比较
- dubbo集群和负载均衡
- 数据结构——堆(转载)
- python数组转换为列表_python - 将一系列数组转换为单个列表 - SO中文参考 - www.soinside.com...
- python平均工资-2019年我国程序员薪资统计,看看你出于什么水平?
- matlab蚁群算法 降维,基于蚁群算法的路由问题研究
- 【PSO运输优化】基于MATLAB的PSO运输优化算法的仿真
- degree of freedom of a leg of a dog
- Python练习 | Python 可迭代对象 迭代器
- 2018第九届蓝桥杯C/C++ B国赛 —— 第六题:矩阵求和
- Linux 中断处理浅析
- skywalking原理_微服务链路追踪原理
- 安全云盘项目(一):1.5 bufferevent服务端代码事件策略
- WebView基本使用
- 8086汇编语言(一) 汇编语言源程序
- 用python计算方程的根_Python程序计算ax^2+bx+c=0方程根
- JAVA之父,詹姆斯·高斯林传奇人生
- RT进程组的cpu带宽限制
- 001 《两、三位数除以一位数,笔算》三年级下
热门文章
- Android SnackBar:你值得拥有的信息提示控件
- linux ubuntu 桌面,Ubuntu Linux 入门(三):熟悉 Ubuntu Linux 桌面环境
- linux命令的导入,[导入]Linux基本命令
- 前后端、多语言、跨云部署,全链路追踪到底有多难?
- 阿里巴巴如何改善开发人员在 K8s 上的体验?
- larvel mysql count,php – 模型中的Laravel计数函数,然后sortBy count()
- 利用python数据可视化_想用Python做数据可视化?先迈过这个“坎”
- oracle异常如何处理,ORACLE异常处理总结
- php yield 个人小解_php 技术 yield 问题
- java程序员修炼之道 pdf_?活动丨和大咖云风来场1对1交流,分享《程序员修炼之道》心得...