poj3481 splaytree模板题
找不到错在哪里,先留着吧
/*
splay是以键值排序的!
三个操作:1 a b,z增加键值为b的点,值为a
2,查询最大值
3,查询最小值
需要的操作:rotate,splay,insert,findx,delete
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000005
int pre[maxn],ch[maxn][],size[maxn],key[maxn],num[maxn],root,tot; void newnode(int &r,int fa,int k,int val){
r=++tot;
pre[r]=fa;
ch[r][]=ch[r][]=;
key[r]=k;
num[r]=val;
}
void pushup(int r){
size[r]=;
if(ch[r][]) size[r]+=size[ch[r][]];
if(ch[r][]) size[r]+=size[ch[r][]];
size[r]+=;
}
void rotate(int x,int kind){//0左旋,1右旋
int fa=pre[x];
ch[fa][!kind]=ch[x][kind];
pre[ch[x][kind]]=fa;
if(pre[fa])
ch[pre[fa]][ch[pre[fa]][]==fa]=x;
pre[x]=pre[fa];
pre[fa]=x;
ch[x][kind]=fa;
pushup(fa);
pushup(x);
}
void splay(int r,int goal){
while(pre[r]!=goal){
if(pre[pre[r]]==goal) rotate(r,ch[pre[r]][]==r);
else {
int fa=pre[r];
int kind=ch[pre[fa]][]==fa;//fa的旋转方向,与fa所在子树相反
if(ch[fa][kind]==r){rotate(r,!kind);rotate(r,kind);}
else{rotate(fa,kind);rotate(r,kind);}
}
}
if(goal==) root=r;
pushup(r);
}
void init(){
root=tot=;
ch[root][]=ch[root][]=pre[root]=size[root]=num[root]=key[root]=;
}
void insert(int k,int val){//插入键值为k的结点
int r=root;
if(r==) {newnode(root,,k,val);pushup(root);return;}
while(ch[r][key[r]<k]) r=ch[r][key[r]<k];
newnode(ch[r][key[r]<k],r,k,val);
splay(ch[r][key[r]<k],);//把k提上去作为根
pushup(r);
}
int getth(int r,int pos){//返回第pos个结点的
int t=size[ch[r][]]+;
if(pos==t) return r;
else if(pos<t) getth(ch[r][],pos);
else getth(ch[r][],pos-t);
}
void remove(){//删除根节点
if(ch[root][]==){root=ch[root][];pre[root]=;}//只有右子树
else {
int tmp=getth(ch[root][],size[ch[root][]]);
splay(tmp,root);
ch[tmp][]=ch[root][];
pre[ch[root][]]=tmp;
root=tmp;
pre[root]=;
}
pushup(root);
}
int main(){
int op,k,p;
init();//建立一颗空树
while(scanf("%d",&op)&&op){
if(op==){//最大值
if(root==) {
puts("");
continue;
}
int ans=getth(root,size[root]);
splay(ans,);
printf("%d\n",num[ans]);
remove();
//debug();
}
else if(op==){
if(root==){
puts("");
continue;
}
int ans=getth(root,);
splay(ans,);
printf("%d\n",num[ans]);
remove();
//debug();
}
else {
scanf("%d%d",&k,&p);
insert(p,k);//p是关键字,k是值
//debug();
}
}
return ;
}
终于过了:和上面做法有些不同,找到最大或最小的点,将其作为根,同时将前驱后缀提取出来当做子树
/* */
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
int pre[maxn],ch[maxn][],nums[maxn],keys[maxn],size[maxn],tot,root; inline void newnode(int &r,int fa,int k,int val){
r=++tot;
ch[r][]=ch[r][]=;
pre[r]=fa;
size[r]=;
nums[r]=val;
keys[r]=k;
}
void init(){
tot=root=;
pre[root]=ch[root][]=ch[root][]=size[root]=;
}
inline void pushup(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+;
}
inline void rotate(int x,int kind){
int fa=pre[x];
ch[fa][!kind]=ch[x][kind];
pre[ch[x][kind]]=fa;
pre[x]=pre[fa];
if(pre[fa])
ch[pre[fa]][ch[pre[fa]][]==fa]=x;
ch[x][kind]=fa;
pre[fa]=x;
pushup(fa);
pushup(x);
}
inline void splay(int x,int goal){
while(pre[x]!=goal){
if(pre[pre[x]]==goal) rotate(x,ch[pre[x]][]==x);
else {
int fa=pre[x];
int kind=ch[pre[fa]][]==fa;
if(ch[fa][kind]==x){
rotate(x,!kind);
rotate(x,kind);
}
else {
rotate(fa,kind);
rotate(x,kind);
}
}
}
pushup(x);
if(goal==) root=x;
}
int find(int key){
if(root==) return ;
int x=root;
while(x && keys[x]!=key)
x=ch[x][key>keys[x]];
if(x) splay(x,);
return x;
}
int prev(){
int x=ch[root][];
if(x==) return ;//ÎÞǰÇý
while(ch[x][])
x=ch[x][];
return x;
}
int succ(){
int x=ch[root][];
if(x==) return ;//ÎÞºó¼Ì
while(ch[x][])
x=ch[x][];
return x;
}
void insert(int key,int num){
if(root==) {newnode(root,,key,num);return;}
int x=root,lastx=;
while(x){
lastx=x;
x=ch[x][key>keys[x]];
}
newnode(x,lastx,key,num);
ch[lastx][key>keys[lastx]]=x;
pre[x]=lastx;
splay(x,);
} void del(int key){
int x=find(key);
if(x==) return;
int tmp1=prev(),tmp2=succ();
if(!tmp1 && !tmp2){root=;return;}
if(!tmp1){//Ö»ÓÐÓÒ×ÓÊ÷
splay(tmp2,);
ch[tmp2][]=;
pushup(tmp2);
return;
}
if(!tmp2){//Ö»ÓÐ×ó×ÓÊ÷
splay(tmp1,);
ch[tmp1][]=;
pushup(tmp1);
return;
}
splay(tmp1,);
splay(tmp2,tmp1);
ch[tmp2][]=;
pushup(tmp2);pushup(tmp1);
}
int getth(int x,int pos){
if(root==) return ;
int t=size[ch[x][]]+;
if(pos==t) return x;
else if(pos<t) return getth(ch[x][],pos);
else return getth(ch[x][],pos-t);
}
int main(){
int p,key,num,x;
while(scanf("%d",&p)&&p){
switch(p){
case :
scanf("%d%d",&num,&key);
insert(key,num);
break;
case :
x=getth(root,size[root]);
if(x){
printf("%d\n",nums[x]);
del(keys[x]);
}
else printf("0\n");
break;
case :
x=getth(root,);
if(x){
printf("%d\n",nums[x]);
del(keys[x]);
}
else printf("0\n");
}
}
return ;
}
poj3481 splaytree模板题的更多相关文章
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- POJ2774 & 后缀数组模板题
题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- Simple Question
一.你会在时间序列数据集上使用什么交叉验证技术?是用k倍? 答:都不是.对于时间序列问题,k倍可能会很麻烦,因为第4年或第5年的一些模式有可能跟第3年的不同,而我们最终可能只是需要对过去几年的进行验证 ...
- php-fpm sock文件权限设置
在编译php-fpm时,若没有指定fpm用户,在配置文件中也没有指定用户,则sock文件会由root(启动php-fpm的用户)创建,其权限是srw-rw----. 而nginx一般由nginx用户启 ...
- 启动eclipse弹出提示Version 1.7.0_79 of the JVM is not suitable for this product. Version: 1.8 or greater is required怎样解决
启动eclipse时弹出如下弹出框: 解决办法: 在eclipse安装目录下找到eclipse.ini文件,并在 -vmargs-Dosgi.requiredJavaVersion=1.8 前面加上 ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c
tourselect.c 文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...
- JSTL中forEach标签应用示例【转】【补】
forEach样例 <%@ page language="java" import="java.util.*" pageEncoding="ut ...
- CodeBblock 常用快捷键 (最常用)
==日常编辑== • 按住Ctrl滚滚轮,代码的字体会随你心意变大变小. • 在编辑区按住右键可拖动代码,省去拉(尤其是横向)滚动条之麻烦:相关设置:Mouse Drag Scrolling. • C ...
- 匿名内部类中使用的外部局部变量为什么只能是final变量
被匿名内部类引用的变量会被拷贝一份到内部类的环境中 但其后,在外部,该变量如果被修改,则内部外部不一致 Java为了避免数据不同步的问题,做出了匿名内部类只可以访问final的局部变量的限制. 究其原 ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
- python -- 题目不看别人的自己写然后比较
题目一 ''' 编写Python脚本,分析xx.log文件,按域名统计访问次数倒序输出 xx.log文件内容如下: https://www.sogo.com/ale.html https://www. ...
- JS中的call()方法和apply()方法用法总结
原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...