萌新刚刚入谷,求助五彩斑斓28pts
  • 板块学术版
  • 楼主ybchenyuyang
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/8/28 20:21
  • 上次更新2023/11/3 00:38:58
查看原帖
萌新刚刚入谷,求助五彩斑斓28pts
665688
ybchenyuyang楼主2023/8/28 20:21

qwq,我怕我把这个求助帖放在题目板块没人回我

题目点这里

代码如下

#include<bits/stdc++.h>
using namespace std;
namespace fastrw{
template<typename tn>void read(tn& a){
    tn x=0,f=1;
    char c=' ';
    for(;!isdigit(c);c=getchar()){
        if(c=='-'){
            f=-1;
        }
    }
    for(;isdigit(c);c=getchar()){
        x=x*10+c-'0';
    }
    a=x*f;
}
template<typename tn>void print(tn a){
    if(a<0){
        putchar('-');
        a=-a;
    }
    if(a>9){
        print(a/10);
    }
    putchar(a%10+'0');
}
};
using namespace fastrw;
const double alpha=0.75;
int st[1000005],top,q,op,x;
struct tree{
	int lef,righ,v,tot,len,_del;
}t[1000005];
int ord[1000005],cnt,root;
void inord(int x);
void init(int x);
void update(int x);
void build(int l,int r,int &x);
void rebuild(int &x);
bool balance(int x);
void insert(int &x,int y);
int rankk(int x,int y);
int kth(int k);
void delk(int &x,int k);
void del(int x);
int main(){
	ios::sync_with_stdio(false);
	for(int i=1000004;i>=1;i--){
		st[++top]=i;
	}
	cin>>q;
	while(q--){
		cin>>op>>x;
		if(op==1){
			insert(root,x);
		}else if(op==2){
			del(x);
		}else if(op==3){
			cout<<rankk(root,x)+1<<"\n";
		}else if(op==4){
			cout<<kth(x)<<"\n";
		}else if(op==5){
			cout<<kth(rankk(root,x))<<"\n";
		}else if(op==6){
			cout<<kth(rankk(root,x+1)+1)<<"\n";
		}
	}
	return 0;
}
void inord(int x){
	if(x==0){
		return;
	}
	inord(t[x].lef);
	if(t[x]._del){
		ord[++cnt]=x;
	}else{
		st[++top]=x;
	}
	inord(t[x].righ);
}
void init(int x){
	t[x].lef=t[x].righ=0;
	t[x].len=t[x].tot=t[x]._del=1;
}
void update(int x){
	t[x].len=t[t[x].lef].len+t[t[x].righ].len+1;
	t[x].tot=t[t[x].lef].tot+t[t[x].righ].tot+1;
}
void build(int l,int r,int &x){
	int mid=(l+r)>>1;
	x=ord[mid];
	if(l==r){
		init(x);
		return;
	}
	if(l<mid){
		build(l,mid-1,t[x].lef);
	}
	if(l==mid){
		t[x].lef=0;
	}
	build(mid+1,r,t[x].righ);
}
void rebuild(int &x){
	cnt=0;
	inord(x);
	if(cnt){
		build(1,cnt,x);
	}else{
		x=0;
	}
}
bool balance(int x){
	double maxn=(double)max(t[t[x].lef].len,t[t[x].righ].len);
	if((double)t[x].len*alpha<=maxn){
		return true;
	}
	return false;
}
void insert(int &x,int y){
	if(x==0){
		x=st[top--];
		t[x].v=y;
		init(x);
		return;
	}
	t[x].len++;
	t[x].tot++;
	if(t[x].v>=y){
		insert(t[x].lef,y);
	}else{
		insert(t[x].righ,y);
	}
	if(balance(x)){
		rebuild(x);
	}
}
int rankk(int x,int y){
	if(x==0){
		return 0;
	}
	if(y>t[x].v){
		return t[t[x].lef].len+t[x]._del+rankk(t[x].righ,y);
	}
	return rankk(t[x].lef,y);
}
int kth(int k){
	int x=root;
	while(x){
		if(t[x]._del&&t[t[x].lef].len+1==k){
			return t[x].v;
		}else if(t[t[x].lef].len>=k){
			x=t[x].lef;
		}else{
			k-=t[t[x].lef].len+t[x]._del;
			x=t[x].righ;
		}
	}
	return t[x].v;
}
void delk(int &x,int k){
	t[x].len--;
	if(t[x]._del&&t[t[x].lef].len+1==k){
		t[x]._del=0;
		return;
	}
	if(t[t[x].lef].len+t[x]._del>=k){
		delk(t[x].lef,k);
	}else{
		delk(t[x].righ,k-t[t[x].lef].len-t[x]._del);
	}
}
void del(int x){
	delk(root,rankk(root,x)+1);
	if(t[root].tot*alpha>=t[root].len){
		rebuild(root);
	}
}

本萌新刚刚学会替罪羊树,想逝一下

2023/8/28 20:21
加载中...