lyw的三行式splay

#include <cstdio>
#include<cstring>
#include <algorithm>
#define maxn 33333
using namespace std;
const int inf=~0u>>2;
#define lc(x) ch[(x)][0]
#define min(x,y) (x)>(y)?(y):(x)
int fa[maxn],ch[maxn][2],root,k[maxn],ind=1;
 
inline void rotate(int p)
{
    int q=fa[p],y=fa[q],x=ch[q][1]==p;
    ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
    ch[p][x^1]=q;fa[q]=p;
    fa[p]=y;
    if(y)
    {
        if(ch[y][0]==q)ch[y][0]=p;
        else if(ch[y][1]==q)ch[y][1]=p;
    }
}
inline void splay(int x)
{
    for(int y;y=fa[x];rotate(x))
        if(fa[y])
            rotate((x==lc(y))==(y==lc(fa[y]))?y:x);
    root=x;
}
inline void insert(int x,int v)
{
    int y;
    while(true)
    {
        y=ch[x][k[x]<v];
        if(y==0)
        {
            y=++ind;
            k[y]=v;
            ch[y][0]=ch[y][1]=0;
            fa[y]=x;
            ch[x][k[x]<v]=y;
            break;
        }
        x=y;
    }
    splay(y);
}
inline int pre(int x)
{
    int tmp=ch[x][0];
    while(ch[tmp][1])tmp=ch[tmp][1];
    return k[tmp];
}
inline int suc(int x)
{
    int tmp=ch[x][1];
    while(ch[tmp][0])tmp=ch[tmp][0];
    return k[tmp];
}
int main()
{
    int n,t,ans=0;
    scanf("%d",&n);
     
    if(scanf("%d",&t)==-1)t=0;
    root=1;k[root]=t;
    ch[root][0]=ch[root][1]=0;
    fa[root]=0;
    ans=t;
    insert(root,inf);insert(root,-inf);
         
    for(int i=2;i<=n;i++)
    {
        if(scanf("%d",&t)==-1)t=0;
        insert(root,t);
        int a=pre(root),b=suc(root);
        ans+=min(t-a,b-t);
    }
    printf("%d\n",ans);
    return 0;
}

从今天下午开始学习splay。。。话说从昨天晚上到今天上午一直在调treap版的郁闷的出纳员但是还是一直都是90分各种超时啊。。

昨天晚上还是一个人在机房写treap然后lyw又跟基友来了机房转悠,意外收获lyw自己写的超简化版的splay我觉得如果能理解十分钟能写完。。

但是就我现在这水平。。还是先写正常版的无指针的splay吧。。

先把lyw的代码贴过来

题目是bzoj的1588.。也就是hnoi2002营业额统计


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值