HYSBZ 3224 Tyvj 1728 普通平衡树 splay模版
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3224
splay详解: http://blog.csdn.net/clove_unique/article/details/50630280
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 198 | #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<map> #include<set> #define lson i<<1 #define rson i<<1|1 using namespace std; const int N=1e5+5; int f[N],ch[N][2],sz[N],cnt[N],key[N]; int tot,root; inline void Clear( int x) { ch[x][0]=ch[x][1]=f[x]=sz[x]=cnt[x]=key[x]=0; } inline int get( int x) { return ch[f[x]][1]==x; } void update( int x) { if (x) { sz[x]=cnt[x]; if (ch[x][0]) sz[x]+=sz[ch[x][0]]; if (ch[x][1]) sz[x]+=sz[ch[x][1]]; } } void Rotate( int x) { int fa=f[x],ff=f[fa],kind=get(x); ch[fa][kind]=ch[x][kind^1];f[ch[x][kind^1]]=fa; ch[x][kind^1]=fa;f[fa]=x; f[x]=ff; if (ff) ch[ff][ch[ff][1]==fa]=x; update(fa); update(x); } void splay( int x) { for ( int fa;(fa=f[x]);Rotate(x)) if (f[fa]) Rotate((get(x)==get(fa)?fa:x)); root=x; } void Insert( int x) { if (root==0) { tot++; ch[tot][0]=ch[tot][1]=f[tot]=0; sz[tot]=cnt[tot]=1; key[tot]=x; root=tot; return ; } int now=root,fa=0; while (1) { if (x==key[now]) { cnt[now]++; update(now); update(fa); splay(now); return ; } fa=now; now=ch[now][key[now]<x]; if (now==0) { tot++; ch[tot][0]=ch[tot][1]=0; f[tot]=fa; sz[tot]=cnt[tot]=1; ch[fa][key[fa]<x]=tot; key[tot]=x; update(fa); splay(tot); return ; } } } int Find( int x) { int now=root,ans=0; while (1) { if (x<key[now]) now=ch[now][0]; else { ans+=(ch[now][0])?sz[ch[now][0]]:0; if (x==key[now]) { splay(now); return ans+1; } ans+=cnt[now]; now=ch[now][1]; } } } int Findx( int x) { int now=root; while (1) { if (ch[now][0]&&x<=sz[ch[now][0]]) now=ch[now][0]; else { int tem=ch[now][0]?sz[ch[now][0]]:0; tem+=cnt[now]; if (x<=tem) return key[now]; x-=tem;now=ch[now][1]; } } } int pre() { int now=ch[root][0]; while (ch[now][1]) now=ch[now][1]; return now; } int nextt() { int now=ch[root][1]; while (ch[now][0]) now=ch[now][0]; return now; } void del( int x) { Find(x); if (cnt[root]>1) { cnt[root]--; return ; } if (!ch[root][0]&&!ch[root][1]) { Clear(root); root=0; return ; } if (!ch[root][0]) { int oldroot=root; root=ch[root][1]; f[root]=0; Clear(oldroot); return ; } else if (!ch[root][1]) { int oldroot=root; root=ch[root][0]; f[root]=0; Clear(oldroot); return ; } int leftbig=pre(),oldroot=root; splay(leftbig); f[ch[oldroot][1]]=root; //因为提上来的是最靠近的数,所以不会产生左子树 ch[root][1]=ch[oldroot][1]; Clear(oldroot); update(root); } int main() { int n; while ( scanf ( "%d" ,&n)!=EOF) { tot=root=0; int opt,x; for ( int i=1;i<=n;i++) { scanf ( "%d%d" ,&opt,&x); switch (opt) { case 1:Insert(x); break ; case 2:del(x); break ; case 3: printf ( "%d\n" ,Find(x)); break ; case 4: printf ( "%d\n" ,Findx(x)); break ; case 5:Insert(x); printf ( "%d\n" ,key[pre()]);del(x); break ; case 6:Insert(x); printf ( "%d\n" ,key[nextt()]);del(x); break ; } } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!