平衡树模板 bzoj 3224
program t3224;
var
tr:array[-1..1000000,1..2] of int64;
num,fa,size,quan:array[-1..1000000] of int64;
i,n,sz,rt:longint;
x,y:int64;
function judge(a:boolean):longint;
begin
if a then exit(1) else exit(2);
end;
procedure rotate(k:longint);
var
f,ff,lr,szlr,szk,x:longint;
begin
f:=fa[k]; ff:=fa[f];
x:=judge(tr[f,1]=k); lr:=tr[k,3-x];
szlr:=size[lr]; szk:=size[k];
tr[ff,judge(tr[ff,1]=f)]:=k; fa[k]:=ff; size[k]:=size[f];
tr[k,3-x]:=f; fa[f]:=k;
tr[f,x]:=lr; fa[lr]:=f; size[f]:=size[f]-szk+szlr;
end;
procedure splay(k:longint);
var
f,ff,p,g1,g2:longint;
begin
if k=-1 then exit;
p:=k;
while fa[p]<>0 do
begin
f:=fa[p]; ff:=fa[f];
if ff=0 then begin rotate(p); break; end;
g1:=judge(tr[f,1]=p); g2:=judge(tr[ff,1]=f);
if (g1=g2)and(fa[ff]=0) then begin rotate(p); rotate(p); continue; end;
if g1=g2 then begin rotate(f); rotate(p); end;
if g1<>g2 then begin rotate(p); rotate(p); end;
end;
rt:=p;
end;
procedure inse(data:int64);
var
p,f:longint;
begin
p:=rt;
f:=0;
while p<>-1 do
begin
f:=p;
size[f]:=size[f]+1;
if data=num[p] then begin inc(quan[p]); splay(p); exit; end;
if data>num[p]
then begin p:=tr[p,2]; continue; end;
if data<num[p]
then p:=tr[p,1];
end;
inc(sz); num[sz]:=data; quan[sz]:=1; size[sz]:=1;
fa[sz]:=f; tr[sz,1]:=-1; tr[sz,2]:=-1;
if data>num[f] then tr[f,2]:=sz else tr[f,1]:=sz;
splay(sz);
end;
function findrank(data:int64):int64;
var
p:longint;
begin
p:=rt;
while true do
begin
if num[p]>data then p:=tr[p,1];
if num[p]<data then p:=tr[p,2];
if num[p]=data then break;
end;
splay(p);
exit(size[tr[p,1]]+1);
end;
procedure dele(data:int64);
var
p,p1,p2:longint;
begin
p:=rt;
while true do
begin
if num[p]>data then p:=tr[p,1];
if num[p]<data then p:=tr[p,2];
if num[p]=data then break;
end;
splay(p);
if quan[p]>1 then begin dec(quan[p]); dec(size[p]); exit; end;
if quan[p]=1
then begin
p1:=tr[rt,1];
while tr[p1,2]<>-1 do
p1:=tr[p1,2];
p2:=tr[rt,2];
while tr[p2,1]<>-1 do
p2:=tr[p2,1];
splay(p2); splay(p1);
dec(size[rt]);
dec(size[tr[rt,2]]);
tr[tr[rt,2],1]:=-1;
end;
end;
function pre(data:int64):int64;
var
p:longint;
begin
inse(data);
p:=tr[rt,1];
while tr[p,2]<>-1 do
p:=tr[p,2];
dele(data);
exit(num[p]);
end;
function suc(data:int64):int64;
var
p:longint;
begin
inse(data);
p:=tr[rt,2];
while tr[p,1]<>-1 do
p:=tr[p,1];
dele(data);
exit(num[p]);
end;
function finddata(rank:longint):int64;
var
p:longint;
begin
p:=rt;
inc(rank);
while not((size[tr[p,1]]<rank)and(size[tr[p,1]]+quan[p]>=rank)) do
begin
if size[tr[p,1]]>=rank then begin p:=tr[p,1]; continue; end;
if size[tr[p,1]]<rank then begin rank:=rank-size[tr[p,1]]-quan[p]; p:=tr[p,2]; end;
end;
exit(num[p]);
end;
begin
readln(n);
sz:=0;
rt:=-1;
size[0]:=0;
inse(maxlongint*1000);
inse(-maxlongint*1000);
for i:=1 to n do
begin
readln(x,y);
if x=1 then inse(y);
if x=2 then dele(y);
if x=3 then writeln(findrank(y)-1);
if x=4 then writeln(finddata(y));
if x=5 then writeln(pre(y));
if x=6 then writeln(suc(y));
end;
end.
平衡树模板 bzoj 3224的更多相关文章
- 普通平衡树(bzoj 3224)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 22483 Solved: 10130[Submit][S ...
- BZOJ 3224 Tyvj 1728 普通平衡树模板
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...
- fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树
题面:[模板]普通平衡树 代码: #include<cstdio> #include<cstring> #include<iostream> #include< ...
- BZOJ 3224 普通平衡树 | 平衡树模板
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
随机推荐
- winform 控件(1)
单词:controls(父类),所有的控件dataSource ,工具的数据源DisplayMember,属性,指定的值 <1>label--文本显示工具属性:1.text,是显示文字的n ...
- Variant OLE automation
The Variant data type is the data type for all variables. can contain any kind of data except fixed- ...
- 【原】JS正则表达式里的控制符
正则表达式易于使用而又让人费解,乍一看上去,就像是一行行的乱码,但是它的功能确实又不容小觑.今天整理正则时,纠正了自己的一个误解. 先缕一缕: 正则表达式的两种声明方式: 字面量.构造器 (RegEx ...
- 删除svn文件的批处理文件
@echo on color 2f mode con: cols=80 lines=25 @REM @echo 正在清理SVN文件,请稍候...... @rem 循环删除当前目录及子目录下所有的SVN ...
- json 判断字段
1方式一 !("key" in obj) 方式二 obj.hasOwnProperty("key") //obj为json对象. 2获取不确定键的值 for( ...
- Spring----->projects----->概述
概述: Spring旗下有若干子项目,整个spring工程中其实包含了若干个子项目,这些子项目种类丰富,分别适用于不同的应用领域.开发者可以根据自己的project的功能特色选取spring中某些特定 ...
- jQuery的append和appendTo
这两个关键词,Insus.NET刚开始学习jQuery时,也被它弄得不好理解.现用得多了,运行与理解也不难了. 查了英文词典append的意思是“添加,附加”: 而后者appendTo意思是“ 添加至 ...
- crontab这个坑,执行单个命令执行成功,但是写到crontab怎么也不行
昨天开始,一直在弄通过定时任务控制熄屏,在终端下的单个命令可以执行成功,写到脚本里,单个执行脚本能成功,但是写到crontab定时任务,怎么也不熄屏,但他确实执行了,看日志,是一直报错的:百度了很多很 ...
- Cannot assign to 'self' outside of a method in the init family
今天在重写父类的init方法时报错如下: error:Cannot assign to 'self' outside of a method in the init family 这种问题以前从来没有 ...
- map,list
---恢复内容开始--- Map<String, List> map=new HashMap<String,List>() HashMap可以理解成是一对对数据的集合我暂时把L ...