codevs 4244 平衡树练习
二次联通门 : 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 平衡树练习的更多相关文章
- AC日记——平衡树练习 codevs 4244
4244 平衡树练习 思路: 有节操的人不用set也不用map: 代码: #include <cstdio> #include <cstring> #include <i ...
- [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- treap codevs 4543普通平衡树
#include<cstdio>#include<ctime>#include<cstdlib>struct shu{ int l,r,sum1,zhi,dui,s ...
- BZOJ_1208_&_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1208 (据说codevs要更新?就不放codevs的地址了吧...) 有宠物和人,每个单位都有 ...
- codevs 3289 花匠
题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- codevs 1080 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
随机推荐
- errgroup 分析
errgroup 在 WaitGroup 的基础上实现子协程错误传递, 同时使用 context 控制协程的生命周期. 使用 errgroup 的使用非常简单 package main import ...
- Arm-Linux 移植 FFMPEG库 + x264
背景: ffmpeg 中带有264的解码,没有编码,需要添加x264.libx264是一个自由的H.264编码库,是x264项目的一部分,使用广泛,ffmpeg的H.264实现就是用的libx26 ...
- spring中EL解析器的使用
SpEL对表达式语法解析过程进行了很高的抽象,抽象出解析器.表达式.解析上下文.估值(Evaluate)上下文等对象,非常优雅的表达了解析逻辑.主要的对象如下: 类名 说明 ExpressionPar ...
- Math.random()的加密安全替换方法window.crypto.getRandomValues
Math.random() 返回介于 0(包含) ~ 1(不包含) 之间的一个随机数. Math.random()函数不是加密安全的随机数生成器. window.crypto.getRandomVal ...
- windowsAPI创建句柄失败的返回值
创建句柄的api返回值 INVALID_HANDLE_VALUE CreateFile CreateNamedPipe CreateToolhelp32Snapshot FilterConnectCo ...
- 微信小程序通讯录字母排序
微信小程序通讯录 字母排序效果: demo地址:https://github.com/PeachCoder/wechat-contacts
- LunHui 的生命观
LunHui 的生命观 来源 https://www.zhihu.com/question/346510295 作者:齐天大圣链接:https://www.zhihu.com/question/346 ...
- maven - 多模块构建
使用idea创建maven项目 点击next输入GroupId和ArtifactId 点击next创建项目,新建项目结构如下 修改demo打包方式为pom 按层级拆分创建模块model,server, ...
- 利用 CAKeyframeAnimation实现任意轨迹移动
自定义 View,实现以下方法即可 - (void)drawRect:(CGRect)rect { // Drawing code // 初始化UIBezierPath UIBezierPath ...
- 修改tomcat使用的的编码方式
默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找到如下代码: < Connector port="8080 ...