AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
思路:
劳资敲了一个多星期;
劳资终于a了;
劳资一直不a是因为一个小错误;
劳资最后看的模板;
劳资现在很愤怒;
劳资不想谈思路!!!
来,上代码:
#include <cstdio>
using namespace std;
#define maxn 1000005
struct SplayTreeNodeType {
int w,key,opi,size,ch[];
};
struct SplayTreeNodeType tree[maxn];
int n,root,tot;
inline void updata(int now)
{
tree[now].size=tree[now].w;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
}
inline int pre()
{
int now=tree[root].ch[];
while(tree[now].ch[]) now=tree[now].ch[];
return now;
}
inline int suc()
{
int now=tree[root].ch[];
while(tree[now].ch[]) now=tree[now].ch[];
return now;
}
inline int getson(int now)
{
return tree[tree[now].opi].ch[]==now;
}
void rotate(int now)
{
int opi=tree[now].opi,fopi=tree[opi].opi,pos=getson(now);
tree[opi].ch[pos]=tree[now].ch[pos^];
tree[tree[opi].ch[pos]].opi=opi;
tree[now].ch[pos^]=opi;tree[opi].opi=now;
tree[now].opi=fopi;
if(fopi) tree[fopi].ch[tree[fopi].ch[]==opi]=now;
updata(opi),updata(now);
}
void splay(int now)
{
for(int opi;opi=tree[now].opi;rotate(now))
{
if(tree[opi].opi) rotate(getson(now)==getson(opi)?opi:now);
}
root=now;
}
int rank(int x)
{
int now=root,ans=;
while()
{
if(x<tree[now].key) now=tree[now].ch[];
else
{
ans+=tree[now].ch[]?tree[tree[now].ch[]].size:;
if(x==tree[now].key)
{
splay(now);
return ans+;
}
ans+=tree[now].w;
now=tree[now].ch[];
}
}
}
int rank_(int x)
{
int now=root;
while()
{
if(tree[now].ch[]&&x<=tree[tree[now].ch[]].size) now=tree[now].ch[];
else
{
int tmp=(tree[now].ch[]?tree[tree[now].ch[]].size:)+tree[now].w;
if(x<=tmp) return tree[now].key;
x-=tmp;now=tree[now].ch[];
}
}
}
inline void clear(int now)
{
tree[now].ch[]=tree[now].ch[]=tree[now].w=tree[now].size=tree[now].key=tree[now].opi=;
}
inline void create(int x)
{
tree[++tot].key=x;
tree[tot].w=tree[tot].size=;
tree[tot].ch[]=tree[tot].ch[]=tree[tot].opi=;
}
void insert(int x)
{
if(!root) create(x),root=tot;
else
{
int now=root,opi=;
while()
{
if(tree[now].key==x)
{
tree[now].w++;
tree[now].size++;
splay(now);
break;
}
opi=now;
now=tree[now].ch[x>tree[opi].key];
if(!now)
{
create(x);
tree[tot].opi=opi;
tree[opi].ch[x>tree[opi].key]=tot;
splay(tot);
break;
}
}
}
}
void del(int x)
{
int t=rank(x);
if(tree[root].w>)
{
tree[root].w--;
tree[root].size--;
return ;
}
if(!tree[root].ch[]&&!tree[root].ch[])
{
clear(root);
root=;
return ;
}
if(!tree[root].ch[])
{
int tmp=root;
root=tree[root].ch[];
tree[root].opi=;
clear(tmp);
return ;
}
if(!tree[root].ch[])
{
int tmp=root;
root=tree[root].ch[];
tree[root].opi=;
clear(tmp);
return ;
}
int pre1=pre(),tmp=root;
splay(pre1);
tree[root].ch[]=tree[tmp].ch[];
tree[tree[tmp].ch[]].opi=root;
clear(tmp);updata(root);
}
inline void in(int &now)
{
register int if_z=;now=;
register char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
}
int main()
{
in(n);int ty,x;
while(n--)
{
in(ty),in(x);
if(ty==) insert(x);
if(ty==) del(x);
if(ty==) printf("%d\n",rank(x));
if(ty==) printf("%d\n",rank_(x));
if(ty==) insert(x),printf("%d\n",tree[pre()].key),del(x);
if(ty==) insert(x),printf("%d\n",tree[suc()].key),del(x);
}
return ;
}
AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937
[USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...
- AC日记——[ZJOI2015]幻想乡战略游戏 洛谷 P3345
[ZJOI2015]幻想乡战略游戏 思路: 树剖暴力转移: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...
- AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203
[HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...
- AC日记——斐波那契数列 洛谷 P1962
斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——[JLOI2014]松鼠的新家 洛谷 P3258
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- AC日记——让我们异或吧 洛谷 P2420
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
随机推荐
- eclipse中设置JVM内存
一. 修改jdk 使用内存: 找到eclispe 中window->preferences->Java->Installed JRE ,点击右侧的Edit 按钮,在编辑界面中的 ...
- Spring核心技术(十四)——ApplicationContext的额外功能
在前文的介绍中我们知道,org.springframework.beans.factory包提供了一些基本的功能来管理和控制Bean,甚至通过编程的方式来实现.org.springframework. ...
- 动态加载css,js
function dynamicLoadCss(url) { var head = document.getElementsByTagName('head')[0]; var link = docum ...
- 洛谷P1079 Vigenère 密码
题目链接:https://www.luogu.org/problemnew/show/P1079
- 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像
纯代码不解释. 在CusotmerControllers中添加上传方法 /// <summary> /// ajax上传用户头像 /// </summary> /// < ...
- 使用 Sconfig.cmd 配置服务器核心服务器
使用 Sconfig.cmd 配置服务器核心服务器 适用对象:Windows Server 2012 R2, Windows Server 2012 在 Windows Server 2012 中,你 ...
- ogre3D学习基础12 --- 让机器人动起来(移动模型动画)
学了那么长时间,才学会跑起来.My Ogre,动起来. 第一,还是要把框架搭起来,这里我们用到双端队列deque,前面已经简单介绍过,头文件如下: #include "ExampleAppl ...
- MFC深入浅出读书笔记第一部分
最近看侯捷的MFC深入浅出,简单总结一下. 第一章首先就是先了解一下windows程序设计的基础知识,包括win32程序开发基础,什么*.lib,*.h,*.cpp的,程序入口点WinMain函数,窗 ...
- [oldboy-django][2深入django]老师管理--查看,添加,编辑
# 添加老师(下拉框多选) 数据库设计: class Teacher(models.Model): name = models.CharField(max_length=64) cls = model ...
- HLG1125 循环小数2
循环小数 II Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 155(55 users) Total Accepted: 92(51 ...