集合(Set)是一种包含已排序对象的关联容器
返回指向第一个元素的迭代器 | |
清除所有元素 | |
返回某个值元素的个数 | |
如果集合为空,返回true | |
返回指向最后一个元素的迭代器 | |
返回集合中与给定值相等的上下限的两个迭代器 | |
删除集合中的元素 | |
返回一个指向被查找到元素的迭代器 //时间复杂度为O(n),找不到返回的迭代器为end() | |
返回集合的分配器 | |
在集合中插入元素 //如果存在插入的元素,则不改变集合里面的值,返回的是pair<iterator,bool> | |
返回指向大于(或等于)某值的第一个元素的迭代器//效率比find高 | |
返回一个用于元素间值比较的函数 | |
返回集合能容纳的元素的最大限值 | |
返回指向集合中最后一个元素的反向迭代器 | |
返回指向集合中第一个元素的反向迭代器 | |
集合中元素的数目 | |
交换两个集合变量 | |
返回大于某个值元素的迭代器 | |
返回一个用于比较元素间的值的函 |
set对于查询和插入的复杂度都是logn,而且每插入一个数,都保证那个数是有序的。他是用一种红黑树实现的动态平衡BST。由于set不能查询第k大的数和找到一个数的排名。
所以我们经常手写一种叫Treap的动态BST(排序二叉树)。简单来说,Treap是用有两个权值的树。一个是优先级,一个是价值。对于价值来数,它是个排序二叉树。对于优先级来说,他又是一个堆。(优先级是为了保证树的形态平衡,它是随机产生的,即在插入时每个点的优先级是随机确定的)由此我们可以得出它的插入、删除、查找的期望的logn的。
题目链接:http://www.oj.swust.edu.cn/problem/show/2268
有一个序列含有一定数量的元素,现在要求写一个程序,满足以下几个要求:
【A】支持插入操作(这个序列不允许有重复元素,即是说,如果待插入的元素已经出现在这个序列中,那么请忽略此操作)
【B】支持删除操作(如果此序列中不包含待删除的这个元素,则忽略此操作,否则删除这个元素)
【C】查找元素x的前继元素(前继元素是指:小于x且与x最接近的元素,当然,如果x已经是序列中的最小元素,则x没有前继元素)
【D】查找元素x的后继元素(后继元素是指:大于x且与x最接近的元素,当然,如果x已经是序列中的最大元素,则x没有后继元素)
【E】找第K小的元素
【F】求某个元素x的秩(即x的排名是多少,从小到大排序)
【A】支持插入操作(这个序列不允许有重复元素,即是说,如果待插入的元素已经出现在这个序列中,那么请忽略此操作)
【B】支持删除操作(如果此序列中不包含待删除的这个元素,则忽略此操作,否则删除这个元素)
【C】查找元素x的前继元素(前继元素是指:小于x且与x最接近的元素,当然,如果x已经是序列中的最小元素,则x没有前继元素)
【D】查找元素x的后继元素(后继元素是指:大于x且与x最接近的元素,当然,如果x已经是序列中的最大元素,则x没有后继元素)
【E】找第K小的元素
【F】求某个元素x的秩(即x的排名是多少,从小到大排序)
多组数据(整个文件以输入 -1 结束)
Input
对于每组数据,有若干行(最多100000行),表示的意义如下:
【A】 insert x
【B】 delete x
【C】 predecessor x
【D】 successor x
【E】 Kth x
【F】 rank x
这6种操作的意义与上面的定义相对应!
【G】 print 表示从小到大输出序列中的所有元素
【H】 end 表示结束本组数据
每组输入数据后有一空行!
对于每组数据,有若干行(最多100000行),表示的意义如下:
【A】 insert x
【B】 delete x
【C】 predecessor x
【D】 successor x
【E】 Kth x
【F】 rank x
这6种操作的意义与上面的定义相对应!
【G】 print 表示从小到大输出序列中的所有元素
【H】 end 表示结束本组数据
每组输入数据后有一空行!
output
对于以上8种操作,分别输出对应信息,如下:
【A】 insert x 不用输出任何信息
【B】 delete x 如果x存在,则删除x,否则输出 Input Error
【C】 predecessor x 如果x不存在,输出 Input Error;否则如果x是序列中的最小元素,输出对应信息(见样例),否则输出x的前继元素
【D】 successor x 如果x不存在,输出 Input Error;否则如果x是序列中的最大元素,输出对应信息(见样例),否则输出x的后继元素
【E】 Kth x 如果x不合法,输出 Input Error;否则输出第Kth小的元素(见样例)
【F】 rank x 如果x不存在,输出 Input Error;否则输出x的排名(见样例)
【G】 print 从小到大输出序列中的所有元素,每个元素后加一个逗号,并在最后加上 end of print(见样例)
【H】 end 输出 end of this test
【A】 insert x 不用输出任何信息
【B】 delete x 如果x存在,则删除x,否则输出 Input Error
【C】 predecessor x 如果x不存在,输出 Input Error;否则如果x是序列中的最小元素,输出对应信息(见样例),否则输出x的前继元素
【D】 successor x 如果x不存在,输出 Input Error;否则如果x是序列中的最大元素,输出对应信息(见样例),否则输出x的后继元素
【E】 Kth x 如果x不合法,输出 Input Error;否则输出第Kth小的元素(见样例)
【F】 rank x 如果x不存在,输出 Input Error;否则输出x的排名(见样例)
【G】 print 从小到大输出序列中的所有元素,每个元素后加一个逗号,并在最后加上 end of print(见样例)
【H】 end 输出 end of this test
实现代码:
#include <algorithm>
#include <string>
#include <iostream>
#include <string.h>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
#define ll long long int
#define maxn 100008
struct node
{
node *ch[2];
ll r,v,sz;
node(int val)
{
ch[1]=ch[0]=nullptr;
v=val;
r=(rand())<<15+rand();
sz=1;
}
ll cmp(ll x) const//排序二叉树比较函数
{
if(x==v) return -1;
else return x<v ? 0:1;
}
void supdate()size域更新,统计排名的
{
sz=1;
if(ch[0]!=nullptr) sz+=ch[0]->sz;
if(ch[1]!=nullptr) sz+=ch[1]->sz;
}
};
void rot(node * &o,ll d)//树的翻转,0为左转,1为右转
{
node *k=o->ch[d^1];
o->ch[d^1]=k->ch[d];
k->ch[d]=o;
o->supdate();//o必须先于k更新,因为k的更新需要用的o的size域
k->supdate();
o=k;
}
void insert_1(node * &o,ll x)//插入函数
{
if( o==nullptr){ o=new node(x); return ;}
else
{
int d=o->cmp(x);
if(d==-1) return ;
insert_1(o->ch[d],x);
if(o->ch[d]->r > o->r) rot(o,d^1);
}
o->supdate();
}
void remove_1(node* &o,ll x)//删除val为x的点
{
ll d=o->cmp(x);
if(d==-1)
{
node *u=o;
if(o->ch[0]!=nullptr&&o->ch[1]!=nullptr)
{
ll d2=( o->ch[0]->r > o->ch[1]->r ?1:0 );
rot(o,d2);
remove_1(o->ch[d2],x);
}
else
{
if(o->ch[0]==nullptr) o=o->ch[1];
else if(o->ch[1]==nullptr) o=o->ch[0];
delete u;
}
}
else remove_1(o->ch[d],x);
if(o!=nullptr) o->supdate();
}
int find_1(node *o,ll x)
{
while(o!=nullptr)
{
int d=o->cmp(x);
if(d==(ll)-1) return 1;
else o=o->ch[d];
}
return 0;
}
int find_p_s(node *o,ll x,int f)//1.查找前驱,2.找后继
{
int ff=0;
ll ans=-1;;
if(f==1)
{
while(o!=nullptr)
{
if(o->v==x)
{
ff=1;
o=o->ch[0];
}
else if(o->v>x)
{
o=o->ch[0];
}
else if(o->v<x)
{
ans=o->v;
o=o->ch[1];
}
}
if(!ff) printf("Input Error\n");
else if(ff&&ans==(ll)-1) printf("%lld is the minimum\n",x);
else
{
printf("The predecessor of %lld is %lld\n",x,ans);
}
}
else if(f==2)
{
while(o!=nullptr)
{
if(o->v==x)
{
ff=1;
o=o->ch[1];
}
else if(o->v<x)
{
o=o->ch[1];
}
else if(o->v>x)
{
ans=o->v;
o=o->ch[0];
}
}
if(!ff) printf("Input Error\n");
else if(ff&&ans==-1) printf("%lld is the maximum\n",x);
else
{
printf("The successor of %lld is %lld\n",x,ans);
}
}
}
void f_rank(node *o,ll x)//查找x的排名
{
ll ans=0,ff=0;
while(o!=nullptr)
{
if(o->v==x)
{
ff=1;
ans++;
o=o->ch[0];
}
else if(o->v<x)
{
ans++;
if(o->ch[0]!=nullptr) ans+=o->ch[0]->sz;
o=o->ch[1];
}
else if(o->v>x)
{
o=o->ch[0];
}
}
if(!ff) printf("Input Error\n");
else printf("The rank of %lld is %lld_th\n",x,ans);
}
void f_Kth(node *o,ll x)//查找排名为x的点
{
ll t=x;
while(o!=nullptr)
{
if(o->ch[0]!=nullptr)
{
if(o->ch[0]->sz<x)
{
x-=(o->ch[0]->sz+1);
if(x==0)
{
printf("The %lld_th element is %lld\n",t,o->v);
return ;
}
o=o->ch[1];
}
else o=o->ch[0];
}
else
{
x--;
if(x==0)
{
printf("The %lld_th element is %lld\n",t,o->v);
return ;
}
o=o->ch[1];
}
}
printf("Input Error\n");
}
void prin(node *o)//中序遍历树,显示有序列
{
if(o==nullptr) return ;
prin(o->ch[0]);
printf("%d,",o->v);
prin(o->ch[1]);
}
void clea(node *o)//清空
{
if(o==nullptr) return ;
clea(o->ch[0]);
clea(o->ch[1]);
delete o;
}
int main()
{
char s[30];
//freopen("in.txt","r",stdin);
ll d;
node *root=nullptr;
while(scanf("%s",s)!=EOF)
{
if(s[0]=='i')
{
scanf("%lld",&d);
insert_1(root,d);
}
else if(s[0]=='d')
{
scanf("%lld",&d);
if(find_1(root,d))
{
remove_1(root,d);
}
else printf("Input Error\n");
}
else if(s[0]=='p'&&s[2]=='e')
{
scanf("%lld",&d);
find_p_s(root,d,1);
}
else if(s[0]=='s')
{
scanf("%lld",&d);
find_p_s(root,d,2);
}
else if(s[0]=='p'&&s[2]=='i')
{
prin(root);
printf("end of print\n");
}
else if(s[0]=='e')
{
printf("end of this test\n");
clea(root);
root=nullptr;
}
else if(s[0]=='K')
{
scanf("%lld",&d);
f_Kth(root,d);
}
else if(s[0]=='r')
{
scanf("%lld",&d);
f_rank(root,d);
}
else if(s[0]=='-') break;
}
return 0;
}