【codevs 1230】元素查找 splay……
2015-10-22 10:21
387 查看
高级数据结构——bool数组!
好吧标题党了
其实我打的是二叉搜索树(二分查找树?)
普通的二叉搜索树哦~~
[code]#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN = 100000 + 5; const int MAXM = 100000 + 5; bool hash[MAXN]; int n,m,k; int main() { scanf("%d %d",&n,&m); for(int i = 1;i <= n;i ++) { scanf("%d",&k); if(k <= 100000) hash[k] = true; } for(int i = 1;i <= m;i ++) { scanf("%d",&k); puts(hash[k] ? "YES" : "NO"); } return 0; }
好吧标题党了
其实我打的是二叉搜索树(二分查找树?)
普通的二叉搜索树哦~~
[code]#include <iostream> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int MAXN = 100000 + 5; int sz; struct treedot { int left,right,num; void clear() { this -> left = 0; this -> right = 0; this -> num = 0; return; } }t[MAXN]; bool find(int x) { int e = 1; while(true) { if(t[e].num == x) return true; if(t[e].num < x) { if(!t[e].left) return false; e = t[e].left; } else { if(!t[e].right) return false; e = t[e].right; } } return true; } void insert(int x) { int e = 1; while(true) { if(t[e].num == x) { //puts("呵呵呵"); return; } if(t[e].num < x) { if(!t[e].left) { t[e].left = ++ sz; t[sz].clear(); t[sz].num = x; return; } e = t[e].left; } else { if(!t[e].right) { t[e].right = ++ sz; t[sz].clear(); t[sz].num = x; return; } e = t[e].right; } } return; } void del(int x) { int e = 1,fa = 0; while(true) { if(t[e].num == x)//delete { if(!t[e].left) { if(t[fa].num > x) { t[fa].right = t[e].right; t[e].clear(); return; } else { t[fa].left = t[e].right; t[e].clear(); return; } } else if(!t[t[e].left].right) { if(t[fa].num > x) { t[fa].right = t[e].left; t[e].clear(); return; } else { t[fa].left = t[e].left; t[e].clear(); return; } } else { int d = e; while(t[t[d].right].right) { d = t[d].right; } if(t[fa].num > x) { t[fa].right = t[d].right; } else { t[fa].left = t[d].right; } int h = t[d].right; t[h].right = t[e].right; t[h].left = t[e].left; t[e].clear(); return; } } fa = e; if(t[e].num < x) { if(!t[e].left) { puts("Fuck You!!!"); return; } e = t[e].left; } else { if(!t[e].right) { puts("Fuck You!!!"); return; } e = t[e].right; } } return; } void clear() { memset(t,0,sizeof(struct treedot)*MAXN); } string s; int z; char hehe[MAXN]; int n,m; int x; int main() { scanf("%d %d",&n,&m); for(int i = 1;i <= n;i ++) { scanf("%d",&x); insert(x); } for(int i = 1;i <= m;i ++) { scanf("%d",&x); if(find(x)) puts("YES"); else puts("NO"); } }
相关文章推荐
- 使用Background类提取前景物体然后保存为图片
- python2.7安装requests步骤(接口测试)
- Android Gradle实用技巧——多渠道打包
- 《大道至简》第四章读后感
- 10月20日全球域名商解析量二十强:新网排名下降
- 使用正态分布变换进行配准
- django实现tab页思路!
- win7 + VS2013 出现Cannot open include file: 'SDKDDKVer.h'问题 附地址
- Centos tmux 安装 与 配置
- 数据库为何要有复合主键(多主键)
- Eclipse系列: Eclipse设置Tomcat启动超时时间
- webApi实践:开始WebApi 2
- Esper系列(四)Output
- test
- 如何在 Ubuntu 中设置 IonCube Loaders
- Linux学习16_linux上ln命令详细说明
- Flip Game【POJ--1753】【枚举】【高斯消元】
- 获取服务器信息
- HTTP协议 (七) Cookie(转)
- 有关数据库设计原则(转载)