ACM练级日志:Treap个人用模板

原来是照着动态的模板写的,有几个地方还真不太好改,在这儿记录一下免得下次又纠结半天……

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<time.h>
using namespace std;
  
int t;
int n;
  
struct ntype
{
    int num; //值
    int fix;//修正值
    int left, right;//左右指针
};
ntype node[500010];
int p_node=0;
int ans=0;
int root=0; //root这么开
  
void debug(int p) //这个debug函数似乎还挺好用的
{
    if(p==0)
        return;
    cout<<node[p].num<<" left: "<<node[node[p].left].num<<" right: "<<node[node[p].right].num<<endl;
    debug(node[p].left);
    debug(node[p].right);
}
  
void init()
{
    memset(node,0,sizeof(ntype)*500010);
    p_node=0;
    ans=0;
    root=0;
    return;
}
  
void l_rotate(int &p)
{
    int y=node[p].right;
    node[p].right= node[y].left;
    node[y].left=p;
    p=y;
}
  
void r_rotate(int &p)
{
    int y=node[p].left;
    node[p].left=node[y].right;
    node[y].right=p;
    p=y;
}
  
void add(int &p, int x)
{
    if(p==0)
    {
        p_node++;
        p=p_node;
        node[p].num=x;
        node[p].left = node[p].right = 0;
        node[p].fix=rand()*rand();
    }
      
    else if(x < node[p].num)
    {
        add(node[p].left, x);
        if(node [ node[p].left ] . fix < node[p]. fix)
            r_rotate(p);
    }
      
    else
    {
        add(node[p].right, x);
        if(node [ node[p].right ] . fix < node[p]. fix )
            l_rotate(p);
    }
}
  
void del(int &p, int x) //连查带删,必须,不能指定一个删,否则它爸爸会不知道它已经被删了
{
    if(p==0)
        return;
    if(x<node[p].num)
        del(node[p].left, x);
    else if(x>node[p].num)
        del(node[p].right, x);
      
    else if(node[p].left==0 || node[p].right==0) // 一定要有else,或者在上面就return,不然一高兴整棵树就没了
    {
        int y=p;
        p= (node[p].left !=0)? node[p].left : node[p].right;
        node[y].num= node[y].left=node[y].right=0;
    }
    else
    {
        if(node [ node[p].left ].fix < node[node[p].right] .fix)
        {
            r_rotate(p);
            del(node[p].right, x);
        }
        else
        {
            l_rotate(p);
            del(node[p].left, x);
        }
    }
}
  
int srch(int p, int nowbest, int x) //这是用于找 最接近x但是不比x大的数 用的
{
    if(p==0)
    {
        if(nowbest!=0)
        {
            del(root, node[nowbest].num);
        }
        return nowbest;
    }
    if(node[p].num<=x)
        return srch(node[p].right, p , x);
    else
        return srch(node[p].left, nowbest, x);
}
  
  
int main()
{
    scanf("%d", &t);
    srand(unsigned(time(0)));
      
    //新建:add(root,x);
        //找前驱:srch(root,0,x);
         
    system("pause");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值