二次联通门 : luogu P3369 【模板】普通平衡树(Treap/SBT)

#include <cstdio>

#define Max 100005

#define Inline __attri\
bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now)
{
now = ;
register char word = getchar ();
bool temp = false;
while (word < '' || word > '')
{
if (word == '-')
temp = true;
word = getchar ();
}
while (word <= '' && word >= '')
{
now = now * + word - '';
word = getchar ();
}
if (temp)
now = -now;
} class Splay_Tree_Type
{
private: struct Tree_Date
{
int weigth;
int size;
int key;
int child[];
int father;
}
tree[Max]; Inline int Get_Son (int now)
{
return tree[tree[now].father].child[] == now;
} int Count;
int Root; public : Inline void Update (int now)
{
tree[now].size = tree[now].weigth;
if (tree[now].child[])
tree[now].size += tree[tree[now].child[]].size;
if (tree[now].child[])
tree[now].size += tree[tree[now].child[]].size;
} Inline void Rotate (int now)
{
int father = tree[now].father;
int Grand = tree[father].father;
int pos = Get_Son (now);
tree[father].child[pos] = tree[now].child[pos ^ ];
tree[tree[father].child[pos]].father = father;
tree[now].child[pos ^ ] = father;
tree[father].father = now;
tree[now].father = Grand;
if (Grand)
tree[Grand].child[tree[Grand].child[] == father] = now;
Update (father);
Update (now);
} Inline void Splay (int now)
{
for (int father; father = tree[now].father; Rotate (now))
if (tree[father].father)
Rotate (Get_Son (now) == Get_Son (father) ? father : now);
Root = now;
} Inline int Find_Prefix ()
{
int now = tree[Root].child[];
while (tree[now].child[])
now = tree[now].child[];
return now;
} Inline int Find_Suffix ()
{
int now = tree[Root].child[];
while (tree[now].child[])
now = tree[now].child[];
return now;
} Inline void Clear (int now)
{
tree[now].child[] = ;
tree[now].child[] = ;
tree[now].size = ;
tree[now].weigth = ;
tree[now].key = ;
tree[now].father = ;
} Inline int Find_x_rank (int x)
{
int now = Root;
int Answer = ;
while (true)
{
if (x < tree[now].key)
{
now = tree[now].child[];
continue;
}
Answer += tree[now].child[] ? tree[tree[now].child[]].size : ;
if (tree[now].key == x)
{
Splay (now);
return Answer + ;
}
Answer += tree[now].weigth;
now = tree[now].child[];
}
} Inline int Find_rank_x (int x)
{
int now = Root;
while (true)
{
if (tree[now].child[] && x <= tree[tree[now].child[]].size)
{
now = tree[now].child[];
continue;
}
int temp = (tree[now].child[] ? tree[tree[now].child[]].size : ) + tree[now].weigth;
if (x <= temp)
return tree[now].key;
x -= temp;
now = tree[now].child[];
}
} Inline void Insert (int x)
{
if (!Root)
{
Count++;
tree[Count].key = x;
tree[Count].size = ;
tree[Count].weigth = ;
Root = Count;
return;
}
int father = , now = Root;
while (true)
{
if (tree[now].key == x)
{
tree[now].size++;
tree[now].weigth++;
Splay (now);
return ;
}
father = now;
now = tree[now].child[x > tree[father].key];
if (!now)
{
Count++;
tree[father].child[x > tree[father].key] = Count;
tree[Count].father = father;
tree[Count].key = x;
tree[Count].size = ;
tree[Count].weigth = ;
Splay (Count);
return ;
}
}
} Inline void Delete (int x)
{
Find_x_rank (x);
if (tree[Root].weigth > )
{
tree[Root].weigth--;
tree[Root].size--;
return ;
}
if (!tree[Root].child[] && !tree[Root].child[])
{
Clear (Root);
Root = ;
return ;
}
if (!tree[Root].child[])
{
int temp = Root;
Root = tree[Root].child[];
tree[Root].father = ;
Clear (temp);
return ;
}
if (!tree[Root].child[])
{
int temp = Root;
Root = tree[Root].child[];
tree[Root].father = ;
Clear (temp);
return ;
}
int Prefix = Find_Prefix ();
int temp = Root;
Splay (Prefix);
tree[Root].child[] = tree[temp].child[];
tree[tree[temp].child[]].father = Root;
Clear (temp);
Update (Root);
} Inline int Get_tree_value (int now)
{
return tree[now].key;
}
}; Splay_Tree_Type Splay_Tree; int main (int argc, char *argv[])
{
int N; read (N);
int type, x; for (; N--; )
{
read (type);
read (x);
if (type == )
Splay_Tree.Insert (x);
else if (type == )
Splay_Tree.Delete (x);
else if (type == )
printf ("%d\n", Splay_Tree.Find_x_rank (x));
else if (type == )
printf ("%d\n", Splay_Tree.Find_rank_x (x));
else if (type == )
{
Splay_Tree.Insert (x);
printf ("%d\n", Splay_Tree.Get_tree_value (Splay_Tree.Find_Prefix ()));
Splay_Tree.Delete (x);
}
else
{
Splay_Tree.Insert (x);
printf ("%d\n", Splay_Tree.Get_tree_value (Splay_Tree.Find_Suffix ()));
Splay_Tree.Delete (x);
}
} return ;
}

数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)的更多相关文章

  1. luoguP3369[模板]普通平衡树(Treap/SBT) 题解

    链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...

  2. 【模板】平衡树——Treap和Splay

    二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...

  3. [luogu P3369]【模板】普通平衡树(Treap/SBT)

    [luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...

  4. 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...

  5. 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...

  6. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  7. AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369

    [模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...

  8. P3369 【模板】普通平衡树 Treap

    P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...

  9. luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

    luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...

随机推荐

  1. 集成maven和Spring boot的profile

    如果在配置中勾选了多套配置,则以pom.xml文件中 profiles中  配置 最后一个配置为准. maven中配置profile节点: <project> .... <profi ...

  2. [CF868E]Policeman and a Tree

    题目大意:有一棵$n$个点的带边权的树,上面有$m$个罪犯,速度为任意大,有一个警察在点$S$,速度为$1$.若警察和罪犯在同一个地方,罪犯就被干掉了,警察希望干掉所有罪犯时间最短,而罪犯希望最大化这 ...

  3. 解决跟Docker私有仓库登陆,推送,拉取镜像出现的报错

    出现问题:Error response from daemon: Get https://192.168.186.120/v1/users/: dial tcp 192.168.186.120:443 ...

  4. a 链接锚点

    建立锚点的元素必须要有 id 或 name 属性,最好两个都有. 锚点超链接一定包含井号 "#",锚点超链接都在链接的最末端,具体看后面例子: 同一个页面不同部分的跳转 目标元素: ...

  5. java并发编程之原子操作

    先来看一段简单的代码,稍微有点并发知识的都可以知道打印出结果必然是一个小于20000的值 package com.example.test.cas; import java.io.IOExceptio ...

  6. 虚拟机与宿主机可以互相ping通,但是外网不能

    http://rickcheung.blog.51cto.com/913220/354429 1.CentOS 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc/resolv.conf  ...

  7. GC是如何判断一个对象为"垃圾"的?被GC判断为"垃圾"的对象一定会被回收吗?

    一.GC如何判断一个对象为”垃圾”的java堆内存中存放着几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还“存活”着,哪些已经“死去”.那么GC具体通过什么手段来 ...

  8. [LeetCode] 448. 找到所有数组中消失的数字 ☆

    描述 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您 ...

  9. 快速为不同 Git 平台配置用户

    在 ~ 目录下创建 config 文件可以为项目配置默认的用户信息,但如果希望经常切换,那么最好就是通过命令为项目单独设置用户. 我使用的 shell 是 zsh, 所以我在 ~/.zshrc 文件中 ...

  10. "人工智能",你怕了吗?

    近期“人工智能+”,已经是市场上非常火的一个风口,人工智能已经渗透到人类生活的方方面面,服务于我们的生活.但是人工智能的迅速发展,也引起了我的担忧,一系列科技电影展示出来的人工智能奴役人类的场景,让人 ...