Splay 0分求助
查看原帖
Splay 0分求助
176360
honey_楼主2022/4/15 19:40

基本上是Splay模板代码全部复制过来的,断点调试本题,问题应该在S操作,但是找不出bug。
代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
const int N=1e5+5;
using namespace std;
int n,m,delta,num;
namespace Splay
{
    int ch[N][2],siz[N],val[N],par[N],cnt[N],rt,tot;
    int chk(int x)
	{
		return x==ch[par[x]][1];
	}
    void pushup(int x)
	{
		siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
	}
    void rotate(int x)
	{
		int y=par[x],z=par[y],k=chk(x),w=ch[x][k^1];
		ch[y][k]=w,par[w]=y;
		ch[z][chk(y)]=x,par[x]=z;
		ch[x][k^1]=y,par[y]=x;
		pushup(y);
		pushup(x);
	}
    void splay(int x,int goal=0)
	{
		while(par[x]!=goal)
		{
			int y=par[x],z=par[y];
			if(z!=goal)
			{
				if(chk(x)==chk(y))
				    rotate(y);
				else
				    rotate(x);
			}
			rotate(x);
		}
		if(!goal)
		    rt=x;
	}
    int insert(int x)
	{
		int cur=rt,p=0;
		while(cur && val[cur]!=x)
		{
			p=cur;
			cur=ch[cur][x>val[cur]];
		}
		if(cur)
		    cnt[cur]++;
		else
		{
			cur=++tot;
			if(p)
			    ch[p][x>val[p]]=cur;
			ch[cur][0]=ch[cur][1]=0;
			val[cur]=x,par[cur]=p;
			siz[cur]=cnt[cur]=1;
		}
		splay(cur);
		return cur;
	}
    int kth(int k)
	{
		int cur=rt;
		while(1)
		{
			if(ch[cur][0] && k<=siz[ch[cur][0]])
			    cur=ch[cur][0];
			else if(k>siz[ch[cur][0]]+cnt[cur])
			    k-=siz[ch[cur][0]]+cnt[cur],cur=ch[cur][1];
			else
			    return val[cur];
		}
		return -1;
	}
	void find(int x)
	{
		if(!rt)
		    return ;
		int cur=rt;
		while(ch[cur][x>val[cur]] && x!=val[cur])
		    cur=ch[cur][x>val[cur]];
		splay(cur);
	}
    int rk(int x)
	{
		find(x);
		return siz[ch[rt][0]];
	}
}
using namespace Splay;
int main()
{
    scanf("%d %d",&n,&m);
    int L=insert(-1e9),R=insert(1e9);
    while(n--)
    {
        char opt[2];
        int k;
        scanf("%s %d",opt,&k);
        if(*opt=='I')
        {
            if(k>=m)
            {
                k-=delta;
                insert(k);
                ++num;
            }
        }
        else if(*opt=='A')
            delta+=k;
        else if(*opt=='S')
        {
            delta-=k;
            R=rk(m-delta);
            splay(R);splay(L,R);
            ch[L][1]=0;
            pushup(L),pushup(R);
        }
        else
        {
            if(siz[rt]-2<k)
                puts("-1");
            else
                printf("%d\n",kth(siz[rt]-k)+delta);
        }
    }
    printf("%d\n",num-(siz[rt]-2));
    return 0;
}

2022/4/15 19:40
加载中...