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是否能 ...
随机推荐
- (转)iOS静态库与动态库的区别
一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用 ...
- Leetcode 96. 不同的二叉搜索树
题目链接 https://leetcode.com/problems/unique-binary-search-trees/description/ 题目描述 给定一个整数 n,求以 1 ... n ...
- TI C6000优化手册——让代码看起来像钉子
DSP芯片的出现,是为了解决大量的数字运算问题.通过集成专用的加法器.乘法器.地址产生器.复杂逻辑等硬件单元,DSP能实现比普通单片机更快速的数字运算,使处理器更适用于实时性高.复杂度强的处理场合.也 ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
- Kali 安装VMtools(最新)
老方法安装的VMtools不能进行主宿切换,下面是kali最新版安装VMtools的方法 一.换国内源&更新源 参考 Kali 2017更新源 二.安装VMtools apt-get inst ...
- Spring---环境搭建与包介绍
jar包下载路径 首先需要下载Spring框架 spring-framework-5.0.0.RELEASE-dist,官方地址为https://repo.spring.io/release/org/ ...
- Linux基本命令运行
文件基本操作: 增删查改: 创建文件:touch(创建文件和修改文件或者目录的时间戳),vim.vi(编辑/创建文件),mkdir(创建文件目录) 移动和修改文件名:mv 删除文件:rm –rf(强制 ...
- Python框架之Django学习笔记(七)
标签 eif/else {% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的 ...
- 51、如何提取android代码中的字符串为系统资源文件 (I18N)
工具:android studio 步骤1:找到要转为资源文件的字符串并选中,同时按下option+enter,弹出菜单,我们选中extract string resource 步骤2:在弹窗中输入你 ...
- python - 接口自动化测试 - GetLog - 日志类封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: get_logger.py @ide: PyCharm C ...