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是否能 ...
随机推荐
- Harbor HA部署-使用Ceph RADOS后端
1. 前言 Harbor 1.4.0版本开始提供了HA部署方式,和非HA的主要区别就是把有状态的服务分离出来,使用外部集群,而不是运行在本地的容器上.而无状态的服务则可以部署在多个节点上,通过配置上层 ...
- BZOJ 1222: [HNOI2001]产品加工
F[i]表示第一个机器用了i的时间,第二个机器的最小时间 转移即可 #include<cstdio> #include<algorithm> using namespace s ...
- SSRS 制作报表时报错: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
转载注明出处,原文地址:http://www.cnblogs.com/zzry/p/5718739.html 在用ssrs 制作报表时报如下错误 错误信息截图: 看到如上错误第一个想到的解决方法就是 ...
- chrome浏览器设置自动切换代理上网的方法
利用shadowsocks代理软件实现FQ时,如果都走代理模式,流量肯定不够.可以利用chrome的SwitchyOmega插件实现自动根据URL来决定是否使用代理.设置如下: 1.安装Switchy ...
- windows下SecureCRT无法使用Backspace(删除键)和上下左右键
MongoDB Shell中退格键使用的问题. 利用SecureCRT工具访问linux的时候,在使用MongoDB的交互式shell的时候,退格键(Backspace)无法使用,导致无法修改输入的字 ...
- 【homework #1】第一次作业被虐感受
当大二暑假结束,我发现我还是没有熟练掌握一门编程语言时,我就知道苦日子要来了. 这不,才开学第三周,就已经被虐的体无完肤了.连编译课用C语言写的词法分析,都要写很久.Debug很久才能写出来,更别提大 ...
- 目标检测算法SSD在window环境下GPU配置训练自己的数据集
由于最近想试一下牛掰的目标检测算法SSD.于是乎,自己做了几千张数据(实际只有几百张,利用数据扩充算法比如镜像,噪声,切割,旋转等扩充到了几千张,其实还是很不够).于是在网上找了相关的介绍,自己处理数 ...
- Spread.js 上下级关系
- vmware安装centos7 安装redis windows7访问redis
1.在windows7中安装vmware 2.在vmware中安装centos7 3.禁用centos7自带的firewalld.service 4.安装iptables防火墙 5.安装Redis 3 ...
- activemq 简单聊天
有兴趣加群qq:200634530