非旋treap

#include<bits/stdc++.h>
#define N 100005
using namespace std;
int rt,val[N],lch[N],rch[N],pro[N],sz[N],cnt;
int rnd()
{
	static int sd=4234;
	return sd*=3423;
}
int node(int v)
{
	val[++cnt]=v;
	pro[cnt]=rnd();
	sz[cnt]=1;
	return cnt;
}
void update(int t)
{
	sz[t]=sz[lch[t]]+sz[rch[t]]+1;
}
void split(int t,int v,int & a,int & b)
{
	if(!t)
	{
		a=b=0;
		return;
	}
	if(v<=val[t])
	{
		b=t;
		split(lch[t],v,a,lch[b]);
	}
	else
	{
		a=t;
		split(rch[t],v,rch[a],b);
	}
	update(t);
}
int merge(int a,int b)
{
	if(!(a&&b))
	{
		return a+b;
	}
	if(pro[a]<pro[b])
	{
		lch[b]=merge(a,lch[b]);
		update(b);
		return b;
	}
	else
	{
		rch[a]=merge(rch[a],b);//
		update(a);
		return a;
	}
}
int find(int k)
{
	int t=rt;
	while(k!=sz[rch[t]]+1)
	{
		if(k<=sz[rch[t]]) t=rch[t];
		else k-=sz[rch[t]]+1, t=lch[t];
	}
	return val[t];
}
int main()
{
	int n,mn,x,y,z,o=0,ans=0;
	char s[10];
	scanf("%d%d",&n,&mn);
	while(n--)
	{
		scanf("%s%d",s,&x);
		if(s[0]=='A') o-=x;
		else if(s[0]=='S') o+=x, split(rt,o+mn,y,rt), ans+=sz[y];
		else if(s[0]=='I') if(x>=mn) split(rt,x+o,y,z), rt=merge(merge(y,node(x+o)),z); else;
		else if(s[0]=='F') printf("%d\n",sz[rt]>=x ? find(x)-o : -1);
	}
	printf("%d\n",ans);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值