BZOJ3224普通平衡树——非旋转treap
题目:
此为平衡树系列第一道:普通平衡树您需要写一种数据结构,来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
n<=100000 所有数字均在-107到107内。
输入样例:
10 1 106465 4 1 1 317721 1 460929 1 644985 1 84185 1 89851 6 81968 1 492737 5 493598
输出样例:
106465 84185 492737
变量声明:size[x],以x为根节点的子树大小;ls[x],x的左儿子;rs[x],x的右子树;r[x],x节点的随机数;v[x],x节点的权值。
root,树的总根;tot,树的大小。
非旋转treap不同于旋转treap需要靠旋转来维护平衡树的性质,他的操作可以用简单暴力来形容——只有合并和断裂两个操作。他不但有treap的优良性质,还有许多优点:支持可持久化和区间操作,常数比splay小。
下面介绍一下非旋转treap的这两个操作:
1.断裂
就是去掉一条边,把treap拆分成两棵树,对于区间操作可以进行两次断裂来分割出一段区间再进行操作。
以查找value为例,从root往下走,如果v[x]>value,那么下一步走ls[x],之后的点都比x小,把x接到右树上,下一次再接到右树上的点就是x的左儿子。
v[x]<=value与上述类似,在这里不加赘述。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void split( int x, int &lroot, int &rroot, int val) { if (!x) { lroot=rroot=0; return ; } if (v[x]<=val) { lroot=x; split(rs[x],rs[lroot],rroot,val); } else { rroot=x; split(ls[x],lroot,ls[rroot],val); } up(x); } |
2.合并
就是把断裂开的树合并起来,因为要维护堆的性质所以按可并堆来合并。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void merge( int &x, int a, int b) { if (!a||!b) { x=a+b; return ; } if (r[a]<r[b]) { x=a; merge(rs[x],rs[a],b); } else { x=b; merge(ls[x],a,ls[b]); } up(x); } |
为了方便删除,所以建议把相同权值的点分开来加入树中,不要都放在同一个点。
非旋转treap代码比较短(为了清晰我写的比较长qwq)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<iostream> using namespace std; int INF=1000000000; int n; int opt,x; int r[100010]; int ls[100010]; int rs[100010]; int size[100010]; int v[100010]; int root; int tot; void up( int x) { size[x]=size[ls[x]]+size[rs[x]]+1; } void build( int &x, int val) { tot++; size[tot]=1; r[tot]= rand (); v[tot]=val; ls[tot]=rs[tot]=0; x=tot; } void merge( int &x, int a, int b) { if (!a||!b) { x=a+b; return ; } if (r[a]<r[b]) { x=a; merge(rs[x],rs[a],b); } else { x=b; merge(ls[x],a,ls[b]); } up(x); } void split( int x, int &lroot, int &rroot, int val) { if (!x) { lroot=rroot=0; return ; } if (v[x]<=val) { lroot=x; split(rs[x],rs[lroot],rroot,val); } else { rroot=x; split(ls[x],lroot,ls[rroot],val); } up(x); } void insert_sum( int val) { int x=0; int y=0; int z=0; build(z,val); split(root,x,y,val); merge(x,x,z); merge(root,x,y); } void delete_sum( int val) { int x=0; int y=0; int z=0; split(root,x,y,val); split(x,x,z,val-1); merge(z,ls[z],rs[z]); merge(x,x,z); merge(root,x,y); } void ask_rank( int val) { int x=0; int y=0; split(root,x,y,val-1); printf ( "%d\n" ,size[x]+1); merge(root,x,y); } void ask_sum( int x, int num) { while (size[ls[x]]+1!=num) { if (num<=size[ls[x]]) { x=ls[x]; } else { num-=(size[ls[x]]+1); x=rs[x]; } } printf ( "%d\n" ,v[x]); } void ask_front( int val) { int x=0; int y=0; split(root,x,y,val-1); if (size[x]==0) { printf ( "0\n" ); } else { ask_sum(x,size[x]); } merge(root,x,y); } void ask_back( int val) { int x=0; int y=0; split(root,x,y,val); ask_sum(y,1); merge(root,x,y); } int main() { srand (16); scanf ( "%d" ,&n); for ( int i=1;i<=n;i++) { scanf ( "%d%d" ,&opt,&x); if (opt==1) { insert_sum(x); } else if (opt==2) { delete_sum(x); } else if (opt==3) { if (tot==0) { printf ( "0\n" ); } else { ask_rank(x); } } else if (opt==4) { if (tot==0) { printf ( "0\n" ); } else { ask_sum(root,x); } } else if (opt==5) { if (tot==0) { printf ( "0\n" ); } else { ask_front(x); } } else if (opt==6) { if (tot==0) { printf ( "0\n" ); } else { ask_back(x); } } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步