codevs 4543 treap 模板
type rec=record
lc,rc,v,rnd,size,w,fa:longint;
end; var
n,root,tot,ans,opt,x,i,po:longint;
tr:array[..] of rec; procedure rotl(po:longint);
var
y:longint;
begin
y:=tr[po].rc;
tr[po].rc:=tr[y].lc;
if tr[y].lc> then tr[tr[y].lc].fa:=po;
tr[y].fa:=tr[po].fa;
if tr[po].fa= then root:=y else
if po=tr[tr[po].fa].lc then
tr[tr[po].fa].lc:=y else tr[tr[po].fa].rc:=y;
tr[y].lc:=po;
tr[po].fa:=y;
tr[y].size:=tr[po].size;
tr[po].size:=tr[tr[po].lc].size+tr[tr[po].rc].size+tr[po].w;
end; procedure rotr(po:longint);
var
y:longint;
begin
y:=tr[po].lc;
tr[po].lc:=tr[y].rc;
if tr[y].rc> then tr[tr[y].rc].fa:=po;
tr[y].fa:=tr[po].fa;
if tr[po].fa= then root:=y else
if po=tr[tr[po].fa].lc then
tr[tr[po].fa].lc:=y else tr[tr[po].fa].rc:=y;
tr[y].rc:=po;
tr[po].fa:=y;
tr[y].size:=tr[po].size;
tr[po].size:=tr[tr[po].lc].size+tr[tr[po].rc].size+tr[po].w;
end; procedure ins(x,po:longint);
begin
inc(tr[po].size);
if x=tr[po].v then
begin
inc(tr[po].w);
exit;
end; if x<tr[po].v then
begin
if tr[po].lc= then
begin
inc(tot);
tr[tot].fa:=po;
tr[tot].v:=x;
tr[tot].size:=;
tr[tot].w:=;
tr[tot].rnd:=random()+;
tr[po].lc:=tot;
if tr[tot].rnd<tr[po].rnd then rotr(po);
end else
begin
ins(x,tr[po].lc);
if tr[tr[po].lc].rnd<tr[po].rnd then rotr(po);
end;
end; if x>tr[po].v then
begin
if tr[po].rc= then
begin
inc(tot);
tr[tot].fa:=po;
tr[tot].v:=x;
tr[tot].size:=;
tr[tot].w:=;
tr[tot].rnd:=random()+;
tr[po].rc:=tot;
if tr[tot].rnd<tr[po].rnd then rotl(po);
end else
begin
ins(x,tr[po].rc);
if tr[tr[po].rc].rnd<tr[po].rnd then rotl(po);
end;
end;
end; function find(x,po:longint):longint;
begin
dec(tr[po].size);
if x<tr[po].v then exit(find(x,tr[po].lc));
if x>tr[po].v then exit(find(x,tr[po].rc));
if x=tr[po].v then exit(po);
end; procedure del(po:longint);
begin
while(tr[po].lc>) or (tr[po].rc>) do
begin
if tr[po].lc= then
begin
rotl(po);
tr[tr[po].fa].size:=tr[tr[tr[po].fa].rc].size+tr[tr[po].lc].size+tr[tr[po].rc].size+tr[tr[po].fa].w;
continue;
end; if tr[po].rc= then
begin
rotr(po);
tr[tr[po].fa].size:=tr[tr[tr[po].fa].lc].size+tr[tr[po].lc].size+tr[tr[po].rc].size+tr[tr[po].fa].w;
continue;
end; if tr[tr[po].lc].rnd>tr[tr[po].rc].rnd then
begin
rotl(po);
tr[tr[po].fa].size:=tr[tr[tr[po].fa].rc].size+tr[tr[po].lc].size+tr[tr[po].rc].size+tr[tr[po].fa].w;
end
else
begin
rotr(po);
tr[tr[po].fa].size:=tr[tr[tr[po].fa].lc].size+tr[tr[po].lc].size+tr[tr[po].rc].size+tr[tr[po].fa].w;
end;
end; if tr[tr[po].fa].lc=po then
begin
tr[tr[po].fa].lc:=;
tr[tr[po].fa].size:=tr[tr[tr[po].fa].rc].size+tr[tr[po].fa].w;
end
if tr[tr[po].fa].rc=po then
begin
tr[tr[po].fa].rc:=;
tr[tr[po].fa].size:=tr[tr[tr[po].fa].lc].size+tr[tr[po].fa].w;
end; tr[po].size:=;tr[po].w:=; end; procedure num(x,po:longint);
begin
if x<=tr[tr[po].lc].size then
begin
num(x,tr[po].lc);
exit;
end; if x>tr[tr[po].lc].size+tr[po].w then
begin
num(x-tr[tr[po].lc].size-tr[po].w,tr[po].rc);
exit;
end; writeln(tr[po].v);
end; procedure rank(x,po:longint);
begin
if x<tr[po].v then
begin
rank(x,tr[po].lc);
exit;
end; if x>tr[po].v then
begin
ans:=ans+tr[tr[po].lc].size+tr[po].w;
rank(x,tr[po].rc);
exit;
end; ans:=ans+tr[tr[po].lc].size+;
end; procedure pre(x,po:longint);
begin
if po= then exit;
if (tr[po].v<x) then
begin
ans:=tr[po].v;
pre(x,tr[po].rc);
end else pre(x,tr[po].lc);
end; procedure suc(x,po:longint);
begin
if po= then exit;
if (tr[po].v>x) then
begin
ans:=tr[po].v;suc(x,tr[po].lc);
end else suc(x,tr[po].rc);
end; begin randomize;
read(n); for i:= to n do
begin
read(opt,x);
if opt= then
begin
if tr[root].w= then
begin
inc(tot);
tr[tot].v:=x;tr[tot].size:=;tr[tot].w:=;
tr[tot].rnd:=random()+;
root:=tot;
end else ins(x,root);
end;
if opt= then begin po:=find(x,root);if tr[po].w> then begin dec(tr[po].w);continue;end;del(po); end;
if opt= then begin ans:=;rank(x,root); writeln(ans);end;
if opt= then num(x,root);
if opt= then begin pre(x,root); writeln(ans); end;
if opt= then begin suc(x,root); writeln(ans); end;
end; end.
------------------------------------------------------
非旋treap
CODECHEF LTIME16 CHEFC
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std; int a[],T,n,root,q; struct treenode{
int lc,rc,lv,rv,v,ans,size;
}tr[]; void update(int po){
if (!tr[po].lc) tr[po].lv=tr[po].v;else tr[po].lv=tr[tr[po].lc].lv;
if (!tr[po].rc) tr[po].rv=tr[po].v;else tr[po].rv=tr[tr[po].rc].rv; tr[po].ans=tr[tr[po].lc].ans+tr[tr[po].rc].ans;
if (tr[po].lc) tr[po].ans+=tr[po].v!=tr[tr[po].lc].rv;
if (tr[po].rc) tr[po].ans+=tr[po].v!=tr[tr[po].rc].lv; tr[po].size=tr[tr[po].lc].size+tr[tr[po].rc].size+;
} int build(int l,int r){
int mid=(l+r)>>;
tr[mid].v=a[mid];tr[mid].size=tr[mid].ans=;tr[mid].lc=tr[mid].rc=;
if (l==r){
tr[mid].lv=tr[mid].rv=tr[mid].v;tr[mid].size=;
return(mid);
} if (l!=mid) tr[mid].lc=build(l,mid-);
if (r!=mid) tr[mid].rc=build(mid+,r);
update(mid);
return(mid);
} void split(int po,int &root1,int &root2,int k){
if (tr[tr[po].lc].size==k){
root1=tr[po].lc;root2=po;
tr[po].lc=;
update(po);
return;
}
if (tr[tr[po].lc].size==k-){
root1=po;root2=tr[po].rc;
tr[po].rc=;
update(po);
return;
} if (k<=tr[tr[po].lc].size){
split(tr[po].lc,root1,root2,k);
tr[po].lc=root2;
update(po);
root2=po;
}else{
split(tr[po].rc,root1,root2,k-tr[tr[po].lc].size-);
tr[po].rc=root1;
update(po);
root1=po;
}
} int merge(int po1,int po2){
if (po1==||po2==) return(po1|po2); if ((LL)rand()*tr[po1].size>(LL)rand()*tr[po2].size){
tr[po1].rc=merge(tr[po1].rc,po2);
update(po1);
return(po1);
}else{
tr[po2].lc=merge(po1,tr[po2].lc);
update(po2);
return(po2);
}
} void dfs_show(int po){
if (tr[po].lc) dfs_show(tr[po].lc);
printf("%d ",tr[po].v);
if (tr[po].rc) dfs_show(tr[po].rc);
} int main(){
scanf("%d",&T);
while (T--){
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
root=build(,n); scanf("%d",&q);
for (int i=;i<=q;i++){
int opt,l,r;
scanf("%d%d%d",&opt,&l,&r);
int root1,root2,root3;
split(root,root1,root2,l-); split(root2,root2,root3,r-l+); if (opt==){
root1=merge(root1,root3);
root=merge(root2,root1);
}else{
printf("%d\n",tr[root2].ans+);
root2=merge(root2,root3);
root=merge(root1,root2);
}
}
}
}
codevs 4543 treap 模板的更多相关文章
- BZOJ 1588: Treap 模板
		
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
 - [luogu3369]普通平衡树(treap模板)
		
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
 - treap codevs 4543普通平衡树
		
#include<cstdio>#include<ctime>#include<cstdlib>struct shu{ int l,r,sum1,zhi,dui,s ...
 - Treap 模板 poj1442&hdu4557
		
原理可以看hihocoder上面的讲解,很清楚,不多说了. 模板抄lrj训练指南上面的. /** Treap 实现 名次树 功能: 1.找到排名为k的元素 2.值为x的元素的名次 初始化:Node* ...
 - 平衡树Treap模板与原理
		
这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...
 - POJ1442-查询第K大-Treap模板题
		
模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...
 - Treap 模板
		
感觉平衡树也没有以前想的那么玄乎,(其实set超好用的),非旋式Treap挺好理解,和可并堆,二叉搜索树有很大联系 推荐博客:http://memphis.is-programmer.com/post ...
 - 【Treap模板详细注释】BZOJ3224-普通平衡树
		
模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
 - 非旋treap模板
		
bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...
 
随机推荐
- Java设计模式 - 代理模式
			
1.什么是代理模式: 为另一个对象提供一个替身或占位符以访问这个对象. 2.代理模式有什么好处: (1)延迟加载 当你需要从网络上面查看一张很大的图片时,你可以使用代理模式先查看它的缩略图看是否是自己 ...
 - Windows Azure CN 超业余性能测试
			
先来说说为什么会有这篇文章吧.从朋友那里搞来个Windows Azure CN的测试帐号,在公司的时候领导的朋友有一个阿里云的服务器,平时部署小东西都往上面丢,不过那是人家的东西,还有其他的应用跑在上 ...
 - selenium结合sikuliX操作Flash网页
			
sikuli的官网地址:http://www.sikuli.org 首先下载sikuliX的jar包:https://launchpad.net/sikuli/sikulix/1.1.0 java-d ...
 - 【CSS】理解CSS
			
CSS(Cascading Style Sheet,层叠样式表),及其精巧且富有表达力,开发者可以用最为高效的方式高度掌控网页内容的表示. 1. 盒模型 CSS中的一个基本概念是盒模型(box mod ...
 - 洛谷10月月赛Round.1| P3399 丝绸之路 [DP]
			
题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...
 - Nginx反向代理的配置
			
Chapter: Nginx基本操作释疑 1. Nginx的端口修改问题 2. Nginx 301重定向的配置 3. Windows下配置Nginx使之支持PHP 4. Linux下配置Nginx使之 ...
 - maven总结5
			
上篇文章中项目最终发布的release仓库和快照仓库都是nexus的默认仓库,若所有的本地开发项目版本都发布到同一个仓库,可能会造成冲突.因此,我们可以为每一个项目创建一组仓库(快照版本和releas ...
 - androidSDK无法更新的解决方法之一
			
方法来源于: http://www.eoeandroid.com/thread-281075-1-1.html 试试这个,能解决国内访问Google服务器的困难启动 Android SDK Manag ...
 - 深入了解Windows
			
1.1.什么是WindowMicrosoft Windows,是美国微软公司研发的一套操作系统,它问世于1985年,起初仅仅是Microsoft-DOS模拟环境,后续的系统版本由于微软不断的更新升级, ...
 - 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)
			
1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...