【模板】普通平衡树(Treap/SBT)

思路:

  劳资敲了一个多星期;

  劳资终于a了;

  劳资一直不a是因为一个小错误;

  劳资最后看的模板;

  劳资现在很愤怒;

  劳资不想谈思路!!!

来,上代码:

#include <cstdio>

using namespace std;

#define maxn 1000005

struct SplayTreeNodeType {
int w,key,opi,size,ch[];
};
struct SplayTreeNodeType tree[maxn]; int n,root,tot; inline void updata(int now)
{
tree[now].size=tree[now].w;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
} inline int pre()
{
int now=tree[root].ch[];
while(tree[now].ch[]) now=tree[now].ch[];
return now;
} inline int suc()
{
int now=tree[root].ch[];
while(tree[now].ch[]) now=tree[now].ch[];
return now;
} inline int getson(int now)
{
return tree[tree[now].opi].ch[]==now;
} void rotate(int now)
{
int opi=tree[now].opi,fopi=tree[opi].opi,pos=getson(now);
tree[opi].ch[pos]=tree[now].ch[pos^];
tree[tree[opi].ch[pos]].opi=opi;
tree[now].ch[pos^]=opi;tree[opi].opi=now;
tree[now].opi=fopi;
if(fopi) tree[fopi].ch[tree[fopi].ch[]==opi]=now;
updata(opi),updata(now);
} void splay(int now)
{
for(int opi;opi=tree[now].opi;rotate(now))
{
if(tree[opi].opi) rotate(getson(now)==getson(opi)?opi:now);
}
root=now;
} int rank(int x)
{
int now=root,ans=;
while()
{
if(x<tree[now].key) now=tree[now].ch[];
else
{
ans+=tree[now].ch[]?tree[tree[now].ch[]].size:;
if(x==tree[now].key)
{
splay(now);
return ans+;
}
ans+=tree[now].w;
now=tree[now].ch[];
}
}
} int rank_(int x)
{
int now=root;
while()
{
if(tree[now].ch[]&&x<=tree[tree[now].ch[]].size) now=tree[now].ch[];
else
{
int tmp=(tree[now].ch[]?tree[tree[now].ch[]].size:)+tree[now].w;
if(x<=tmp) return tree[now].key;
x-=tmp;now=tree[now].ch[];
}
}
} inline void clear(int now)
{
tree[now].ch[]=tree[now].ch[]=tree[now].w=tree[now].size=tree[now].key=tree[now].opi=;
} inline void create(int x)
{
tree[++tot].key=x;
tree[tot].w=tree[tot].size=;
tree[tot].ch[]=tree[tot].ch[]=tree[tot].opi=;
} void insert(int x)
{
if(!root) create(x),root=tot;
else
{
int now=root,opi=;
while()
{
if(tree[now].key==x)
{
tree[now].w++;
tree[now].size++;
splay(now);
break;
}
opi=now;
now=tree[now].ch[x>tree[opi].key];
if(!now)
{
create(x);
tree[tot].opi=opi;
tree[opi].ch[x>tree[opi].key]=tot;
splay(tot);
break;
}
}
}
} void del(int x)
{
int t=rank(x);
if(tree[root].w>)
{
tree[root].w--;
tree[root].size--;
return ;
}
if(!tree[root].ch[]&&!tree[root].ch[])
{
clear(root);
root=;
return ;
}
if(!tree[root].ch[])
{
int tmp=root;
root=tree[root].ch[];
tree[root].opi=;
clear(tmp);
return ;
}
if(!tree[root].ch[])
{
int tmp=root;
root=tree[root].ch[];
tree[root].opi=;
clear(tmp);
return ;
}
int pre1=pre(),tmp=root;
splay(pre1);
tree[root].ch[]=tree[tmp].ch[];
tree[tree[tmp].ch[]].opi=root;
clear(tmp);updata(root);
} inline void in(int &now)
{
register int if_z=;now=;
register char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} int main()
{
in(n);int ty,x;
while(n--)
{
in(ty),in(x);
if(ty==) insert(x);
if(ty==) del(x);
if(ty==) printf("%d\n",rank(x));
if(ty==) printf("%d\n",rank_(x));
if(ty==) insert(x),printf("%d\n",tree[pre()].key),del(x);
if(ty==) insert(x),printf("%d\n",tree[suc()].key),del(x);
}
return ;
}

AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369的更多相关文章

  1. luoguP3369[模板]普通平衡树(Treap/SBT) 题解

    链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...

  2. AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  3. AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937

    [USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...

  4. AC日记——[ZJOI2015]幻想乡战略游戏 洛谷 P3345

    [ZJOI2015]幻想乡战略游戏 思路: 树剖暴力转移: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...

  5. AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203

    [HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  6. AC日记——斐波那契数列 洛谷 P1962

    斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...

  7. AC日记——[JLOI2014]松鼠的新家 洛谷 P3258

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  8. AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  9. AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  10. AC日记——让我们异或吧 洛谷 P2420

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...

随机推荐

  1. 源码级强力分析hadoop的RPC机制

    分析对象: hadoop版本:hadoop 0.20.203.0 必备技术点: 1. 动态代理(参考 :http://weixiaolu.iteye.com/blog/1477774 )2. Java ...

  2. NMF分解(二)

    应用: 一.图像分析 NMF最成功的一类应用是在图像的分析和处理领域.图像本身包含大量的数据,计算机一般将图像的信息按照矩阵的形式进行存放,针对图像的识别.分析和处理也是在矩阵的基础上进行的.这些特点 ...

  3. python3与django中@property详解

    django提供了内置装饰器 @staticmethod\@classmethod\property 在OSQA中,@property的使用频率是非常高的.下面就是它的使用方法: @property ...

  4. Migrate a Domain-based Namespace to Windows Server 2008 Mode

    TechNet Library Scripting with Windows PowerShell Windows and Windows Server Automation with Windows ...

  5. Careercup - Microsoft面试题 - 24313662

    2014-05-12 07:27 题目链接 原题: Convert a number to a number 题目:把二进制数转化成四进制数. 解法:四是二的倍数,所以两位变一位就可以了. 代码: / ...

  6. win7装python3.6提示api-ms-win-runtime-1-1-0.dll丢失

    win7为MSDN下的旗舰版,没有servicepack1那个,刚开始安装python3.6提示必须得安装servicepack1,于是乎到微软官网下了个900mb大小的安装包. https://ww ...

  7. python-day5-格式化输入

    python格式化输入包含'%'调用,及format方法 使用‘%’进行格式化输出 #最简单的字符串传参 tpl='i am %s '%'alex' >>>i am alex #字符 ...

  8. 用nc+简单bat/vbs脚本+winrar制作迷你远控后门

    前言 某大佬某天和我聊起了nc,并且提到了nc正反向shell这个概念. 我对nc之前的了解程度仅局限于:可以侦听TCP/UDP端口,发起对应的连接. 真正的远控还没实践过,所以决定写个小后门试一试. ...

  9. poj2914 Minimum Cut 全局最小割模板题

    Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 8324   Accepted: 3488 Case ...

  10. knockout,change事件

    knockout,change事件 不知道为啥公司要用ko,就这样吧 大概需求,动态绑定的预算类别,然后预算类别切换时候,根据预算类别中的value去找上级编号.