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 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
随机推荐
- [tomcat] 连接池参数maxActive、maxIdle 、maxWait 等
maxActive 连接池支持的最大连接数,这里取值为20,表示同时最多有20个数据库连接.设 0 为没有限制.maxIdle 连接池中最多可空闲maxIdle个连接 ,这里取值为20,表示即使没有数 ...
- Vue组件全局/局部注册
全局注册 main.js中创建 Vue.component('button-counter', { data: function () { return { count: 0 } }, templat ...
- 【转载】Intellij IDEA神器居然还有这些小技巧
概述Intellij IDEA真是越用越觉得它强大,它总是在我们写代码的时候,不时给我们来个小惊喜.出于对Intellij IDEA的喜爱,我决定写一个与其相关的专栏或者系列,把一些好用的Intell ...
- windows + Eclipse 汉化
https://www.eclipse.org/babel/downloads.php 下载Eclipse 对应版本 汉化包解压 复制文件夹里的内容到eclipse 文件夹下对应的文件里 重启ecli ...
- eigenface算法笔记
昨天看了PCA(PCA算法介绍见上一篇),今天继续看eigenface,在这里把eigenface的过程梳理下: EigenFace本质上讲,是把人脸从像素空间变换到另一个空间,在另一个空间中做相似性 ...
- JVM性能优化--类加载器,手动实现类的热加载
一.类加载的机制的层次结构 每个编写的".java"拓展名类文件都存储着需要执行的程序逻辑,这些".java"文件经过Java编译器编译成拓展名为". ...
- vscode教程(基础篇)
转载:https://segmentfault.com/a/1190000017949680 本文主要介绍vscode在工作中常用的快捷键及插件,目标在于提高工作效率 本文的快捷键是基于mac的,wi ...
- DBCP连接池原理分析(转载)
DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...
- Mycat学习-单独启动mycat
Mycat下载地址:http://mycat.io/ Mycat安装:解压缩即可. Mycat作为一个中间件,实现mysql协议,是可以不依赖数据库单独运行的. 对前端应用连接来说就是一个数据库, ...
- Python面向对象之多态、封装
一.多态 超过一个子类继承父类,出现了多种的形态. 例如,动物种类出现了多种形态,比如猫.狗.猪 class Animal:pass class Cat(Animal):pass class Dog( ...