二次联通门 : codevs 4244 平衡树练习

Splay实测指针占用空间大约是数组的3倍, 且时间上也慢了差不多1s

数组版评测记录如下

指针版评测记录如下

    以上数据仅限这一个题, 对于别的题,指针其实是比数组快的

  虽然这样。。。

  但是指针写起来就是看着顺眼。。。。

/*
指针版 */
#include <cstdio>
#include <cstdlib> 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;
} struct Splay_Tree_Data
{
Splay_Tree_Data *child[]; Splay_Tree_Data *father; int size, weigth; int key;
inline void Updata ()
{
this->size = this->weigth;
if (this->child[] != NULL)
this->size += this->child[]->size;
if (this->child[] != NULL)
this->size += this->child[]->size;
} inline int Get_Pos ()
{
return this->father->child[] == this;
} Splay_Tree_Data ()
{
child[] = NULL;
child[] = NULL;
size = weigth = ;
father = NULL;
key = ;
}
}; Splay_Tree_Data *Root; class Splay_Tree_Type
{
private : inline void Rotate (Splay_Tree_Data *now)
{
Splay_Tree_Data *Father = now->father;
if (now == NULL || Father == NULL)
return ;
int pos = now->Get_Pos () ^ ;
Father->child[pos ^ ] = now->child[pos];
if (now->child[pos])
now->child[pos]->father = Father;
if ((now->father = Father->father) != NULL)
now->father->child[now->father->child[] == Father] = now;
Father->father = now;
now->child[pos] = Father;
Father->Updata ();
now->Updata ();
if (now->father == NULL)
Root = now;
} inline void Splay (Splay_Tree_Data *now, Splay_Tree_Data *to = NULL)
{
for (; now->father != to; Rotate (now))
if (now->father->father != to)
Rotate (now->Get_Pos () == now->father->Get_Pos () ? now->father : now);
} public : void Insert (int x)
{
Splay_Tree_Data *now = Root;
if (Root == NULL)
{
Root = new Splay_Tree_Data;
Root->key = x;
Root->father = NULL;
return ;
}
int pos;
while (true)
{
if (now->key == x)
{
now->size ++;
now->weigth ++;
Splay (now);
return ;
}
pos = x > now->key ? : ;
if (now->child[pos] == NULL)
{
now->child[pos] = new Splay_Tree_Data;
now->child[pos]->father = now;
now->child[pos]->key = x;
Splay (now->child[pos]);
return ;
}
now = now->child[pos];
}
} bool Find (int x)
{
Splay_Tree_Data *now = Root;
while (true)
{
if (x == now->key)
return true;
else if (x < now->key && now->child[] != NULL)
now = now->child[];
else if (x > now->key && now->child[] != NULL)
now = now->child[];
else
return false;
}
}
}; Splay_Tree_Type Tree; int N, M; int main (int argc, char *argv[])
{
int x;
for (read (N), read (M); N--; )
{
read (x);
Tree.Insert (x);
}
for (; M--; )
{
read (x);
if (Tree.Find (x))
printf ("1 ");
else
printf ("0 ");
}
return ;
}
/*
数组版 */
#include <cstdio> #define Max 10000002 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 Splay_Tree_Date
{
int key;
int father;
int child[];
}
tree[Max]; int Count;
int Root; inline int Get_Son (int now)
{
return tree[tree[now].father].child[] == now;
} 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;
} 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;
} public : void Insert (int x)
{
if (!Root)
{
Count++;
tree[Count].key = x;
Root = Count;
return ;
}
int now = Root, father = ;
while (true)
{
if (tree[now].key == x)
return ;
father = now;
now = tree[now].child[x > tree[father].key];
if (!now)
{
Count++;
tree[Count].key = x;
tree[Count].father = father;
tree[father].child[x > tree[father].key] = Count;
Splay (Count);
return ;
}
}
} int Find (int x)
{
int now = Root;
while (true)
{
if (x == tree[now].key)
return ;
if (x < tree[now].key && tree[now].child[])
now = tree[now].child[];
else if (x > tree[now].key && tree[now].child[])
now = tree[now].child[];
else
return ;
if (!now)
return ;
}
}
}; Splay_Tree_Type Make; int main (int argc, char *argv[])
{
int N, M;
read (N);
read (M);
int x;
for (; N--; )
{
read (x);
Make.Insert (x);
}
for (; M--; )
{
read (x);
printf ("%d ", Make.Find (x));
}
return ;
}

codevs 4244 平衡树练习的更多相关文章

  1. AC日记——平衡树练习 codevs 4244

    4244 平衡树练习 思路: 有节操的人不用set也不用map: 代码: #include <cstdio> #include <cstring> #include <i ...

  2. [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转

    题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...

  3. treap codevs 4543普通平衡树

    #include<cstdio>#include<ctime>#include<cstdlib>struct shu{ int l,r,sum1,zhi,dui,s ...

  4. BZOJ_1208_&_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1208 (据说codevs要更新?就不放codevs的地址了吧...) 有宠物和人,每个单位都有 ...

  5. codevs 3289 花匠

    题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...

  6. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  7. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  8. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  9. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

随机推荐

  1. uwsgi flask gevent 测试代码覆盖率(coverage)

    目录 可能出现的问题 解决 可能出现的问题 多进程启动 gevent启动 运行的服务可能不会停止 解决 我先参考了一下这一篇文章使用Coverage分析WSGI项目的代码覆盖率,他基本能够解决掉1.2 ...

  2. 『Python基础练习题』day04

    # 1.写代码,有如下列表,按照要求实现每一个功能 # li = ['Conan', 'Kidd', 'blame', 'jimmy', 'RachelMoore'] # 计算列表的长度并输出 # 列 ...

  3. PB在已经存在的datawindow中添加数据列的方法

    打开数据窗口,点击Data按钮 进入到数据源定义画板,选择要新增的列后,不要保存文件,直接点关闭,在提示框选是就可以了 新增的列值存不到数据库中,如果需要更新数据库中的值则:需要在数据窗口画板下,点击 ...

  4. mysql全面整理(用于复习、查阅)--正在更新

    Mysql学习 1. 关键字与函数名称全部大写 2. 数据库名称.表名称.字段名称全部小写 3. SQL语句必须以分号结尾 一.数据库基本操作 1. 创建.查看数据库 CREATE {DATABASE ...

  5. Luogu5280 [ZJOI2019] 线段树 【线段树】

    题目分析: 这题除了分类讨论就没啥了... 容易发现问题实际就是所有操作选和不选按顺序执行的所有答案和.考虑每个点在多少种情况下会有tag. 那么,考虑新插入一个[l,r],所有有交集的点都会被清空, ...

  6. hdu 6661 Acesrc and String Theory (后缀数组)

    大意: 求重复$k$次的子串个数 枚举重复长度$i$, 把整个串分为$n/i$块, 如果每块可以$O(1)$计算, 那么最终复杂度就为$O(nlogn)$ 有个结论是: 以$j$开头的子串重复次数最大 ...

  7. 在论坛中出现的比较难的sql问题:16(取一个字段中的数字)

    原文:在论坛中出现的比较难的sql问题:16(取一个字段中的数字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 问题:取一个字段中的数字http://bbs.csdn ...

  8. 网络基础 URL

    一.用JAVA实现URL  在JAVA中,Java.net包里面的类是进行网络编程的,其中java.net.URL类和java.net.URLConection类使编程者方便地利用URL在Intern ...

  9. Hive笔记及配置

    Hive 基于Hadoop的数据仓库工具: 将结构化的数据文件,映射为一张表,并提供类SQL查询功能: 本质:将HQL转化为MapReduce程序: Hive处理的数据存储在HDFS: Hive分析数 ...

  10. 算法学习:我终于明白二分查找的时间复杂度为什么是O(logn)了

    最近发现了个好东西,就是一个学算法的好东西,是网易公开课的一个视频. 直通车 这是麻省理工学院的公开课,有中英字幕,感谢网易.. 也可以在App把视频缓存下来之后再放到电脑上面看,因为我这样可以倍速, ...