首页 技术 正文
技术 2022年11月7日
0 收藏 513 点赞 824 浏览 2252 个字
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000000
#define INF 2147483647
int n,fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn],cnt[maxn];
void Maintain(int x)
{
siz[x]=siz[c[x][0]]+siz[c[x][1]]+cnt[x];
}
void NewNode(int &x,int Fa,int key)
{
x=++tot;
fa[x]=Fa;
c[x][0]=c[x][1]=0;
val[x]=key;
siz[x]=cnt[x]=1;
}
void Rotate(int x,bool flag)
{
int y=fa[x];
c[y][!flag]=c[x][flag];
fa[c[x][flag]]=y;
if(fa[y]){
c[fa[y]][c[fa[y]][1]==y]=x;
}
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
Maintain(y);
Maintain(x);
}
void Splay(int x,int goal)
{
if(!x){
return;
}
int y;
while((y=fa[x])!=goal){
if(fa[y]==goal){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
if(!goal){
root=x;
}
}
int Find(int key,int x=root)
{
while(c[x][val[x]<key]){
if(val[x]==key){
return x;
}
x=c[x][val[x]<key];
}
return x;
}
void Insert(int key)
{
if(!root){
NewNode(root,0,key);
return;
}
int x=Find(key);
if(val[x]==key){
++cnt[x];
Splay(x,0);
return;
}
NewNode(c[x][val[x]<key],x,key);
Splay(c[x][val[x]<key],0);
}
int Findmax(int x=root)
{
while(c[x][1]){
x=c[x][1];
}
return x;
}
int Findmin(int x=root)
{
while(c[x][0]){
x=c[x][0];
}
return x;
}
void Delete(int key)
{
int x=Find(key);
Splay(x,0);
if(val[x]==key){
--cnt[x];
if(!cnt[x]){
if(!c[x][0]&&!c[x][1]){
root=0;
}
else if(!c[x][0]||!c[x][1]){
fa[c[x][c[x][0]==0]]=0;
root=c[x][c[x][0]==0];
}
else{
int y=Findmax(c[x][0]);
Splay(y,root);
c[y][1]=c[root][1];
fa[c[root][1]]=y;
root=y;
fa[root]=0;
Maintain(root);
}
}
else{
Maintain(x);
}
}
}
int Rank(int key)
{
int x=Find(key);
Splay(x,0);
return siz[c[x][0]]+1;
}
int Kth(int K,int x=root)
{
while(1){
int Siz0=siz[c[x][0]];
if(K<=Siz0){
x=c[x][0];
}
else if(K<=Siz0+cnt[x]){
return val[x];
}
else{
K-=(Siz0+cnt[x]);
x=c[x][1];
}
}
}
int GetPre(int key)
{
int x=Find(key);
if(val[x]==key){
Splay(x,0);
return val[Findmax(c[x][0])];
}
while(val[x]>key&&fa[x]){
x=fa[x];
}
return val[x];
}
int GetNex(int key)
{
int x=Find(key);
if(val[x]==key){
Splay(x,0);
return val[Findmin(c[x][1])];
}
while(val[x]<key&&fa[x]){
x=fa[x];
}
return val[x];
}
int main(){
int op, x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&op,&x);
if(op==1){
Insert(x);
}
else if(op==2){
Delete(x);
}
else if(op==3){
printf("%d\n",Rank(x));
}
else if(op==4){
printf("%d\n",Kth(x));
}
else if(op==5){
printf("%d\n",GetPre(x));
}
else{
printf("%d\n",GetNex(x));
}
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902