[bzoj 3224]手写treap
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224
bzoj不能用time(0),看到这个博客才知道,我也RE了好几发……
#include<bits/stdc++.h>
using namespace std; struct Node
{
int val,pri,sz,cnt;
Node* ch[];
int cmp(int x) const
{
if (x==val) return -;
return x<val?:;
}
void push_up()
{
sz=cnt+ch[]->sz+ch[]->sz;
}
}*tree,*null; void rotate(Node* &node,int d)
{
Node *k=node->ch[d];
node->ch[d]=k->ch[d^];
k->ch[d^]=node;
node=k;
} void insert(Node* &node,int x)
{
if (node==null)
{
node=new Node();
node->ch[]=null;
node->ch[]=null;
node->val=x;
node->cnt=;
node->pri=rand();
node->sz=;
}
else
{
int d=node->cmp(x);
if (d==-)
{
node->cnt++;
node->sz++;
}
else
{
insert(node->ch[d],x);
if (node->pri < node->ch[d]->pri)
{
rotate(node,d);
node->ch[d^]->push_up();
}
node->push_up();
}
}
} void remove(Node* &node,int x)
{
if (node==null) return;
int d=node->cmp(x);
if (d==-)
{
if (node->cnt>)
{
node->cnt--;
node->sz--;
}
else
{
if (node->ch[]==null)
{
Node *tmp=node;
node=node->ch[];
delete tmp;
}
else if (node->ch[]==null)
{
Node *tmp=node;
node=node->ch[];
delete tmp;
}
else
{
int dd=(node->ch[]->pri)<(node->ch[]->pri)?:;
rotate(node,dd);
remove(node->ch[dd^],x);
}
}
}
else remove(node->ch[d],x);
node->push_up();
} int rk(const Node* node,int x)
{
if (node==null) return ;
if (node->val<x) return node->cnt+node->ch[]->sz+rk(node->ch[],x);
else return rk(node->ch[],x);
} int kth(const Node* node,int k)
{
if (node->ch[]==null)
{
if (node->cnt>=k) return node->val;
else return kth(node->ch[],k-node->cnt);
}
else
{
if (node->ch[]->sz>=k) return kth(node->ch[],k);
if (node->ch[]->sz+node->cnt>=k) return node->val;
return kth(node->ch[],k-node->ch[]->sz-node->cnt);
}
} int pre(Node* node,int x)
{
Node *t=node;
int res=-0x3f3f3f3f;
while (t!=null)
{
if (t->val>=x) t=t->ch[];
else res=t->val,t=t->ch[];
}
return res;
} int suc(Node* node,int x)
{
Node *t=node;
int res=0x3f3f3f3f;
while (t!=null)
{
if (t->val<=x) t=t->ch[];
else res=t->val,t=t->ch[];
}
return res;
} int main()
{
//srand((unsigned)time(NULL));
null=new Node();
null->cnt=;
null->sz=;
null->pri=-;
null->ch[]=null;
null->ch[]=null;
tree=null;
int n;
scanf("%d",&n);
while (n--)
{
int op,x;
scanf("%d%d",&op,&x);
switch(op)
{
case : insert(tree,x); break;
case : remove(tree,x); break;
case : printf("%d\n",rk(tree,x)+); break;
case : printf("%d\n",kth(tree,x)); break;
case : printf("%d\n",pre(tree,x)); break;
case : printf("%d\n",suc(tree,x)); break;
}
}
return ;
}
[bzoj 3224]手写treap的更多相关文章
- BZOJ 3224 - 普通平衡树 - [Treap][Splay]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...
- BZOJ - 3224 可持久化Treap 树形操作
这个题目去年就做过了,这次稍微改了一下 都是基础操作 #include<iostream> #include<algorithm> #include<cstdio> ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- 使用AI算法进行手写数字识别
人工智能 人工智能(Artificial Intelligence,简称AI)一词最初是在1956年Dartmouth学会上提出的,从那以后,研究者们发展了众多理论和原理,人工智能的概念也随之扩展 ...
- 【Win 10 应用开发】手写识别
记得前面(忘了是哪天写的,反正是前些天,请用力点击这里观看)老周讲了一个14393新增的控件,可以很轻松地结合InkCanvas来完成涂鸦.其实,InkCanvas除了涂鸦外,另一个大用途是墨迹识别, ...
- JS / Egret 单笔手写识别、手势识别
UnistrokeRecognizer 单笔手写识别.手势识别 UnistrokeRecognizer : https://github.com/RichLiu1023/UnistrokeRecogn ...
- 如何用卷积神经网络CNN识别手写数字集?
前几天用CNN识别手写数字集,后来看到kaggle上有一个比赛是识别手写数字集的,已经进行了一年多了,目前有1179个有效提交,最高的是100%,我做了一下,用keras做的,一开始用最简单的MLP, ...
- 【转】机器学习教程 十四-利用tensorflow做手写数字识别
模式识别领域应用机器学习的场景非常多,手写识别就是其中一种,最简单的数字识别是一个多类分类问题,我们借这个多类分类问题来介绍一下google最新开源的tensorflow框架,后面深度学习的内容都会基 ...
随机推荐
- python爬虫 爬取steam热销游戏
好久没更新了啊...最近超忙 这学期学了学python 感觉很有趣 就写着玩~~~ 爬取的页面是:https://store.steampowered.com/search/?filter=globa ...
- node获取头信息数据
req.fresh req.stale var version = 100; app.get('/test',function(req,res){ res.set('etag',version); i ...
- Linux安装防火墙
1.安装防火墙 1)yum install iptables(centos) 安装IPtables服务 yum install iptables-services 2)清楚规则iptables -F ...
- How to find your web part
When we deploy a web part, we can find it on any pages through the follow steps: Firstly, ...
- gitk中文乱码问题处理
执行了 git config --global gui.encoding utf- 查看 %USERPROFILE%\.gitconfig 文件中也有 [gui] encoding = utf-8 在 ...
- Unity3d脚本生命周期
如图: 测试脚本: using UnityEngine; public class Test2 : MonoBehaviour { void Awake() { Debug.Log("Awa ...
- Tuxedo 介绍与安装
Tuxedo 介绍与安装(一) Tuxedo介绍 ...
- 1066 Root of AVL Tree (25 分)(平衡二叉树)
就是AVL的模板题了 注意细节 #include<bits/stdc++.h> using namespace std; typedef struct node; typedef node ...
- 教你如何用Docker快速搭建深度学习环境
本教程搭建集 Tensorflow.Keras.Coffe.PyTorch 等深度学习框架于一身的环境,及jupyter. 本教程使用nvidia-docker启动实例,通过本教程可以从一个全新的Ub ...
- restFul介绍及其使用规范
什么是REST和RESTful API? REST:(英文:Representational State Transfer,简称REST)表征性状态转移,是一种软件架构风格. RESTful : RE ...