splay70pts求条
查看原帖
splay70pts求条
1200842
hypixel_zhb666楼主2024/7/11 16:30
#include<bits/stdc++.h>
#define lcs tree[rt].l
#define rcs tree[rt].r
using namespace std;
const int maxn=1e5+9;
int n,mn,cnt,RT,k,ans;
struct tree{
	int val,l,r,sz,tot,die;
}tree[maxn];
inline void pushup(int rt){tree[rt].sz=tree[lcs].sz+tree[rcs].sz+tree[rt].tot;}
void insert(int &rt,int fa,int val){
	if(!rt){
		rt=++cnt;
		tree[rt].val=val;
		tree[rt].tot=tree[rt].sz=1;
		tree[rt].die=fa;
		return;
	}
	if(val<tree[rt].val) insert(lcs,rt,val);
	else if(val>tree[rt].val) insert(rcs,rt,val);
	else tree[rt].tot++;
	pushup(rt);
}
void rotate(int x){
	int y=tree[x].die,z=tree[y].die;
	if(tree[z].l==y) tree[z].l=x;
	else tree[z].r=x;
	tree[x].die=z;tree[y].die=x;
	if(x==tree[y].l){
		tree[tree[x].r].die=y;
		tree[y].l=tree[x].r;
		tree[x].r=y;
	}
	else{
		tree[tree[x].l].die=y;
		tree[y].r=tree[x].l;
		tree[x].l=y;
	}
	pushup(y),pushup(x);
}
void splay(int x,int goal){
	while(tree[x].die^goal){
		int y=tree[x].die,z=tree[y].die;
		if(z^goal) (tree[y].l==x)==(tree[z].l==y)?rotate(y):rotate(x);
		rotate(x);
	}
	if(!goal) RT=x;
}
int query(int rt,int rnk){
	if(rnk>tree[RT].sz-1) return -1;
	if(rnk<=tree[rcs].sz) return query(rcs,rnk);
	else if(rnk>tree[rcs].sz+tree[rt].tot) return query(lcs,rnk-tree[rcs].sz-tree[rt].tot);
	else return tree[rt].val;
}
void find(int rt,int fa,int val){
	if(!rt){splay(fa,0);return;}
	if(val==tree[rt].val) splay(rt,0);
	else if(val<tree[rt].val) find(lcs,rt,val);
	else find(rcs,rt,val);
}
int pre(int val){
	find(RT,0,val);int rt=RT;
	if(tree[rt].val<val) return rt;
	for(rt=lcs;rcs;rt=rcs);return rt;
}
int nxt(int val){
	find(RT,0,val);int rt=RT;
	if(tree[rt].val>val) return rt;
	for(rt=rcs;lcs;rt=lcs);return rt;
}
void remove(){
	for(int i=3;i<=cnt;i++) tree[i].val-=k;
	int rt=nxt(mn-1);splay(rt,0),splay(1,rt);
	ans+=tree[tree[1].r].sz,tree[1].r=0;
	pushup(1),pushup(rt);
}
int main(){
	scanf("%d%d",&n,&mn);char op;
	insert(RT,0,-0x3f3f3f3f),insert(RT,0,0x3f3f3f3f);
	while(n--){
		scanf("\n%c%d",&op,&k);
		if(op=='I'){
			if(k<mn) continue;
			insert(RT,0,k),splay(cnt,0);
		}
		if(op=='A') for(int i=3;i<=cnt;i++) tree[i].val+=k;
		if(op=='S') remove();
		if(op=='F') printf("%d\n",query(RT,k+1));
	}
	return printf("%d\n",ans),0;
}
2024/7/11 16:30
加载中...