AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
思路:
劳资敲了一个多星期;
劳资终于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的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937
[USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...
- AC日记——[ZJOI2015]幻想乡战略游戏 洛谷 P3345
[ZJOI2015]幻想乡战略游戏 思路: 树剖暴力转移: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...
- AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203
[HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...
- AC日记——斐波那契数列 洛谷 P1962
斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——[JLOI2014]松鼠的新家 洛谷 P3258
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- 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 < ...
- AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- AC日记——让我们异或吧 洛谷 P2420
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
随机推荐
- Linux常见的系統进程
前言 在日常运维工作中,经常会看到一些奇怪的系统进程占用资源比较高.而且总是会听到业务线同学询问“xxx这个是啥进程啊?咋开启了这么多?” 而这些系统级的内核进程都是会用中括号括起来的,它们会执行一些 ...
- python-01 spider原理
用Python可以做什么?可以做日常任务,比如自动备份你的MP3:可以做网站,很多著名的网站包括YouTube就是Python写的:可以做网络游戏的后台,很多在线游戏的后台都是Python开发的.总之 ...
- UVA - 753 A Plug for UNIX(网络流)
题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...
- Codeforces:68A-Irrational problem(暴力大法好)
A- Irrational problem p Time Limit: 2000MS Memory Limit: 262144K 64bit IO Format: %I64d& %I64 De ...
- Gym 100829S_surf 动态规划的优化
题目大意是,非你若干个任务,任务分别对应开始时间.预期收益.持续时间三项指标,让你从中选择一个受益最大的方案(没有开始时间相同的任务). 于是,标准状态转移方程应当为,设DP[K]为选择了前K个任务的 ...
- Git-Git基本操作
先来合个影 马上就要和之前实践遗留的数据告别了,告别之前是不是要留个影呢?在Git里,"留影"用的命令叫做tag,更加专业的术语叫做"里程碑"(打tag,或打标 ...
- Android开发环境安装经验
前段时间在一个安装论坛上,下载了老罗的Android学习视频,看到第三节就卡住了。我这边Eclipse安装SDK总是不成功,报各种错误。断断续续好几天的摸索,终于弄明白了。 首先要安装ADT插件,也就 ...
- leetcode 【 Reverse Linked List II 】 python 实现
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- C 语言 习题 1-14
练习 1-14 编写一个程序,打印输入中各个字符出现频度的直方图. #include <stdio.h> /* count digits, white space, others */ i ...
- 如何将Linux rm命令删除的文件放入垃圾箱
因为rm命令删除的文件是不会放入垃圾箱的,所以无法恢复,下面小编就给大家介绍一种方法,通过替换Linux rm命令的方法,从而将rm命令删除的文件放入垃圾箱. 方法: 1. 在/home/userna ...