【BZOJ】【P3685】【普通van Emde Boas树】【题解】【vEB树】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3685
出题又被D了唉……
好不容易卡掉了平衡树,然后线段树完虐……
Code:
#include<bits/stdc++.h>
#define lowsqrt(u) (1<<(M[u]>>1))
#define low(u,x) (x%lowsqrt(u))
#define high(u,x) (x/lowsqrt(u))
#define indx(u,x,y) (x*lowsqrt(u)+y)
using namespace std;
map<int,int>M;
struct vEB{struct node{int Max,Min,u;node *Summ;vector<node*>clus;void build(int Bitsize){if(Bitsize<=1){Max=Min=-1;u=2;Summ=NULL;return;}Max=Min=-1;u=1<<Bitsize;int Nsize=(Bitsize>>1)+(Bitsize&1);Summ=new node();Summ->build(Nsize);clus.resize(1<<Nsize);for(int i=0;i<(1<<Nsize);i++){clus[i]=new node();clus[i]->build(Bitsize>>1);} }}*root;void build(int size){ int L=0;for(size--;size;size>>=1,L++);root=new node();root->build(L);}int _Min(node *t){return t->Min;}int _Max(node *t){return t->Max;}void _insert(node *t,int x){if(t->Min==-1){t->Min=t->Max=x;return;} if(x<t->Min)swap(x,t->Min);int H=high(t->u,x),L=low(t->u,x);if(t->u>2){if(_Min(t->clus[H])==-1){_insert(t->Summ,H);t->clus[H]->Min=L;t->clus[H]->Max=L;}else _insert(t->clus[H],L); }if(x>t->Max)t->Max=x;}void _del(node *t,int x){if(t->Min==t->Max){t->Min=t->Max=-1;}elseif(t->u==2)t->Max=t->Min=!x; else{ if(x==t->Min){int fclus=_Min(t->Summ);x=indx(t->u,fclus,_Min(t->clus[fclus]));t->Min=x; }int H=high(t->u,x),L=low(t->u,x);_del(t->clus[H],L);if(_Min(t->clus[H])==-1){_del(t->Summ,H);if(x==t->Max){int Smax=_Max(t->Summ);if(Smax==-1)t->Max=t->Min;elset->Max=indx(t->u,Smax,_Max(t->clus[Smax]));} }else if(x==t->Max){t->Max=indx(t->u,H,_Max(t->clus[H]));}}}int _pred(node *t,int x){if(t->u==2){if(x==1&&t->Min==0)return 0;else return -1;}else if(t->Max!=-1&&x>t->Max)return t->Max;else{int H=high(t->u,x),L=low(t->u,x);int minhigh=_Min(t->clus[H]);if(minhigh!=-1&&L>minhigh){int offset=_pred(t->clus[H],L);return indx(t->u,H,offset);}else{int preclus=_pred(t->Summ,H);if(preclus==-1){if(t->Min!=-1&&x>t->Min)return t->Min;else return -1;}else{int offset=_Max(t->clus[preclus]);return indx(t->u,preclus,offset); }}}}int _succ(node *t,int x){if(t->u==2){if(x==0&&t->Max==1)return 1;else return -1;}else if(t->Min!=-1&&x<t->Min)return t->Min;else{int H=high(t->u,x),L=low(t->u,x);int maxlow=_Max(t->clus[H]);if(maxlow!=-1&&L<maxlow){int offset=_succ(t->clus[H],L);return indx(t->u,H,offset);}else{int suclus=_succ(t->Summ,H);if(suclus==-1)return -1;else{int offset=_Min(t->clus[suclus]);return indx(t->u,suclus,offset);}}}}bool _exist(node *t,int x){if(x==t->Min||x==t->Max)return 1;else if(t->u==2)return 0;else return _exist(t->clus[high(t->u,x)],low(t->u,x));}int Min(){return _Min(root);}int Max(){return _Max(root);}void insert(int x){_insert(root,x);}void del(int x){_del(root,x);}int pred(int x){return _pred(root,x);}int succ(int x){return _succ(root,x);}bool exist(int x){return _exist(root,x);}
}T;
int n,m;
int getint(){int res=0;char ch=getchar();while(!isdigit(ch))ch=getchar();while(isdigit(ch))res=(res<<3)+(res<<1)+ch-'0',ch=getchar();return res;
}
void putint(int x){if(x<0)putchar('-'),x=-x;if(x<10)putchar(x+'0');else{putint(x/10);putchar(x%10+'0');}
}
int main(){for(int i=0;i<32;i++)M[1<<i]=i;
// freopen("vEB.in","r",stdin);
// freopen("vEB.out","w",stdout);n=getint();m=getint();T.build(n);int x;
// cerr<<(double)clock()/CLOCKS_PER_SEC<<endl;while(m--){//if(m%1000==0)cerr<<m<<endl;int op=getint();switch(op){case 1:{x=getint();if(!T.exist(x))T.insert(x);break;}case 2:{x=getint();if(T.exist(x)) T.del(x);break;}case 3:{putint(T.Min());puts("");break;}case 4:{putint(T.Max());puts("");break;}case 5:{x=getint();putint(T.pred(x));puts("");break;}case 6:{x=getint();putint(T.succ(x));puts("");break;}case 7:{x=getint();if(T.exist(x))puts("1");else puts("-1");break;}}}
// cerr<<(double)clock()/CLOCKS_PER_SEC<<endl;return 0;
}
【BZOJ】【P3685】【普通van Emde Boas树】【题解】【vEB树】相关推荐
- bzoj 3685 普通van Emde Boas树(坑)
3685: 普通van Emde Boas树 Time Limit: 9 Sec Memory Limit: 128 MB Submit: 1758 Solved: 563 [Submit][Stat ...
- BZOJ 3685: 普通van Emde Boas树( 线段树 )
建颗权值线段树就行了...连离散化都不用... 没加读入优化就TLE, 加了就A掉了...而且还快了接近1/4.... ---------------------------------------- ...
- BZOJ 3685 普通van Emde Boas树 权值线段树(zkw)
第一眼看到这题,没错就拿他来做treap的练手了,然而我错了,卡treap,我哭了,写了两三次treap(),这两天几乎都在写数据结构了.后来我又可耻地看了题解,原来这道题已经给了数列中数的范围,可以 ...
- Van Emde Boas Trees
## 介绍 ## van Emde Boas trees 支持所有优先级优先级队列的操作,并且巧妙的是它对于SEARCH, INSERT,DELETE,MINIMUM,MAXMUN,SUCCESSOR ...
- 【adoo】Van Emde Boas trees
各种姿势补充中--转自http://www.roading.org/algorithm/introductiontoalgorithm/Van_Emde_Boas_trees.html van Emd ...
- 【bzoj3685】普通van Emde Boas树 线段树
普通van Emde Boas树 Time Limit: 9 Sec Memory Limit: 128 MB Submit: 1969 Solved: 639 [Submit][Status][ ...
- 算法导论读书笔记(20)van Emde Boas树
第五部分 高级数据结构 第20章 van Emde Boas树 van Emde Boas树支持优先队列操作以及一些其他操作,每个操作最坏情况运行时间为O(lglgn).而这种数据结构限制关键字必须为 ...
- van Emde Boas 树 数据结构说解
van Emde Boas 树的定义 直观上看,vEB 树保存了一个有序的集合,并支持以 O(lglgn) 的时间复杂度在 vEB 树上进行最小最大值查询.单值存在性查询.单值前驱后继查询.单值插入维 ...
- 算法导论-van Emde Boas树
van Emde Boas树 van Emde Boas树中文名不知道,所以暂且叫它v树吧.v树是一种数据结构,和二叉树.红黑树类似.一种数据结构被创建出来,肯定有其特别的优点,v树的优点就是实现数据 ...
- 《算法导论3rd第二十章》van Emde Boas树
前言 前面介绍的二叉堆,红黑树以及斐波那契堆,其重要的操作都要O(lgn).当特定条件下,能否够规避Ω(lglgn)下界的限制?在本章中,我们将看到:van Emde Boas树支持优先队列操作及一些 ...
最新文章
- Shell-bash
- 前端问题之body没设置高度背景色为什么会全屏显示?
- (九)数据结构之“图”
- linux下使用sed批量替换关键词(带斜杠处理+kaggle路径批量替换)
- Microsoft SQL Server 2005数据库安装
- lisp java_从Java调用的LISP代码
- jdbc dao 工具类mysql_Java基于JDBC实现事务,银行转账及货物进出库功能示例
- C++ STL容器vector篇(二) vector容器的构造函数与赋值操作
- MFC开发IM-第五篇、MFC中改变static字体颜色、大小、背景颜色(自定义类),及手动关联变量的方法
- 【重点!DP】LeetCode 97. Interleaving String
- 优秀软件测试工程师必读书籍推荐
- oeasy教您玩转vim - 39 - # 剪切粘贴
- SAP License Keys申请和导入
- 关于用python爬取自如网信息的价格问题(已解决)
- 如何制定人生目标体系
- 建木持续集成平台v2.2.1发布
- 迅雷再换赛道锁定云计算 盈利能力待考
- 170323 PyQt5 ListWidget的删除
- Discuz 会员头像调用方法之discuz_uc_avatar函数
- 大佬用python写了个豆瓣短评爬虫,有你喜欢的电视试试吗
热门文章
- Java小白读完这篇文章秒变大神,P8大师的赞叹不已
- js浮动广告框(可根据屏幕大小自动调整位置)
- 软件销售一个多月的感想
- 完美解决Ubuntu16.04下配置vs code编译环境
- 技术人文 | 米家皮皮灯:无障碍交互新入口
- 跟女友旅游三天,还好Python治好了我的内耗
- Eclipse Theia 揭秘之技术架构篇
- 宝能负债累累,观致“摇摇欲坠”,姚振华造车梦如何成真?
- 多对多查询:一个用户创建多个订单,一个订单包含多个订单明细,一个订单明细包含一个商品
- php短网址程序,php中生成短网址实现程序代码