#include <iostream>
using namespace std;
typedef long long ll;
ll opt,asks,num,pos=0,ovrmin=0x7fffffff,ovrmax=-10000001;
ll nums[100001];
struct Node{
ll num,height,size,count;
Node *Left,*Right;
Node(ll x,ll y,ll z):num(x),height(y),size(z),count(1),Left(NULL),Right(NULL){}
}*Root;
ll getBalance(Node* x){return (x->Left==NULL?0:x->Left->height)-(x->Right==NULL?0:x->Right->height);}
Node* RR(Node* x){
Node* y=x->Left;
x->Left=(y->Right==NULL?NULL:y->Right);
y->Right=x;
x->height=max((x->Left==NULL?0:x->Left->height),(x->Right==NULL?0:x->Right->height))+1;
x->size=(x->Left==NULL?0:x->Left->size)+(x->Right==NULL?0:x->Right->size)+x->count;
y->height=max((y->Left==NULL?0:y->Left->height),(y->Right==NULL?0:y->Right->height))+1;
y->size=(y->Left==NULL?0:y->Left->size)+(y->Right==NULL?0:y->Right->size)+y->count;
return y;
}
Node* LL(Node* x){
Node* y=x->Right;
x->Right=(y->Left==NULL?NULL:y->Left);
y->Left=x;
x->height=max((x->Left==NULL?0:x->Left->height),(x->Right==NULL?0:x->Right->height))+1;
x->size=(x->Left==NULL?0:x->Left->size)+(x->Right==NULL?0:x->Right->size)+x->count;
y->height=max((y->Left==NULL?0:y->Left->height),(y->Right==NULL?0:y->Right->height))+1;
y->size=(y->Left==NULL?0:y->Left->size)+(y->Right==NULL?0:y->Right->size)+y->count;
return y;
}
Node* Avalon(Node* x){
ll fh=getBalance(x),lh=0,rh=0;
if(x->Left!=NULL) lh=getBalance(x->Left);
if(x->Right!=NULL) rh=getBalance(x->Right);
if(fh>=-1&&fh<=1) return x;
if(fh>1){
if(lh>0) x=RR(x);
else if(lh<0){x->Left=LL(x->Left);x=RR(x);}
}
else if(fh<-1){
if(rh>0){x->Right=RR(x->Right);x=LL(x);}
else if(rh<0) x=LL(x);
}
return x;
}
Node* add(ll x,Node* y){
ovrmin=min(ovrmin,x);
ovrmax=max(ovrmax,x);
if(y==NULL) return new Node(x,1,1);
if(y->num==x){
y->count++;
return y;
}
if(x<y->num){
if(y->Left==NULL) y->Left=new Node(x,1,1);
else y->Left=add(x,y->Left);
}
else{
if(y->Right==NULL) y->Right=new Node(x,1,1);
else y->Right=add(x,y->Right);
}
y->size++;
y->height++;
return Avalon(y);
}
Node* remove(ll x,Node* y){
if(y->num==x){
if(y->Left==NULL&&y->Right==NULL){
y->count--;
y->size--;
if(y->count) return y;
return NULL;
}
else if(!(y->Left!=NULL&&y->Right!=NULL)){
y->count--;
y->size--;
if(y->count) return y;
return y->Left==NULL?y->Right:y->Left;
}
else{
y->count--;
y->size--;
if(y->count) return y;
Node *interN=y->Right,*interfN;
while(interN->Left!=NULL){
interfN=interN;
interN=interN->Left;
}
Node* R=interN->Right==NULL?NULL:interN->Right;
int interi=interN->num;
delete interN;
interfN->Left=R;
y->num=interi;
return y;
}
}
else if(x<y->num) y->Left=remove(x,y->Left);
else y->Right=remove(x,y->Right);
y->size--;
return Avalon(y);
}
ll SearchRank(ll x,Node* y){
if(y->num==x) return (y->Left==NULL?1:y->Left->size+1);
if(y->Left==NULL&&y->Right==NULL) return 1;
if(x<y->num) return (y->Left==NULL)?1:SearchRank(x,y->Left);
return (y->Right==NULL?1:SearchRank(x,y->Right))+(y->Left==NULL?0:y->Left->size)+y->count;
}
ll SearchNumber(ll x,Node* y){
int leftsize=(y->Left==NULL?0:y->Left->size);
if(x<=leftsize) return SearchNumber(x,y->Left);
if(x<=leftsize+y->count) return y->num;
return SearchNumber(x-leftsize-y->count,y->Right);
}
ll FindPre(ll x,Node* y){
ll Min=ovrmin;
Node* interN=y;
while(interN!=NULL){
if(x<interN->num) interN=interN->Left;
else if(x>interN->num){
Min=max(Min,interN->num);
interN=interN->Right;
}
else{
interN=interN->Left;
while(interN!=NULL){
if(interN->num<x) Min=max(Min,interN->num);
interN=interN->Right;
}
}
}
return Min;
}
ll FindSuc(ll x,Node* y){
ll Max=ovrmax;
Node* interN=y;
while(interN!=NULL){
if(x<interN->num){
Max=min(Max,interN->num);
interN=interN->Left;
}
else if(x>interN->num) interN=interN->Right;
else{
interN=interN->Right;
while(interN!=NULL){
if(interN->num>x) Max=min(Max,interN->num);
interN=interN->Left;
}
}
}
return Max;
}
void operate(){
cin>>opt>>num;
switch(opt){
case 1:
Root=add(num,Root);
break;
case 2:
Root=remove(num,Root);
break;
case 3:
cout<<SearchRank(num,Root)<<endl;
break;
case 4:
cout<<SearchNumber(num,Root)<<endl;
break;
case 5:
cout<<FindPre(num,Root)<<endl;
break;
case 6:
cout<</*"6 "<<num<<endl<<"结果"<<*/FindSuc(num,Root)<<endl;
break;
default:
break;
}
}
int main(int argc, char** argv) {
cin>>asks;
while(asks--) operate();
return 0;
}
写的AVL树,用的Win10系统。在本地结果是对的,在洛谷上非说有错。
救救萌新吧,第二天碰电脑QAQ