代码

#include<bits/stdc++.h>
using namespace std;
namespace hdk{
namespace balanced_tree{
const int N=2000001,inf=114514191;
class splay{
private:
int root,tot;
struct tree{
int w;
int cnt,size;
int fa,son[2];
}t[N];
public:
tree *operator [](int x){
return &t[x];
}
inline int getroot(){
return root;
}
inline void clear(int x){
t[x]={0,0,0,0,{0,0}};
}
inline bool judgeson(int x){
return t[t[x].fa].son[1]==x;
}
inline void update(int x){
if(x){
t[x].size=t[x].cnt;
if(t[x].son[0]) t[x].size+=t[t[x].son[0]].size;
if(t[x].son[1]) t[x].size+=t[t[x].son[1]].size;
}
}
inline void rotate(int x){
int f=t[x].fa,gf=t[f].fa;
int k=judgeson(x);
t[f].son[k]=t[x].son[k^1];
t[t[f].son[k]].fa=f;
t[x].son[k^1]=f;
t[f].fa=x;
t[x].fa=gf;
if(gf){
t[gf].son[t[gf].son[1]==f]=x;
}
update(f);update(x);
}
inline void sply(int x){
for(int f;(f=t[x].fa);rotate(x)){
if(t[f].fa){
if(judgeson(x)==judgeson(f)){
rotate(f);
}
else rotate(x);
}
}
root=x;
}
inline void insert(int x){
if(!root){
tot++;
t[tot]={x,1,1,0,{0,0}};
root=tot;
return;
}
int now=root,fa=0;
while(1){
if(x==t[now].w){
t[now].cnt++;
update(now);update(fa);
sply(now);
break;
}
fa=now;
now=t[now].son[t[now].w<x];
if(!now){
tot++;
t[tot]={x,1,1,fa,{0,0}};
t[fa].son[t[fa].w<x]=tot;
update(fa);
sply(tot);
break;
}
}
}
inline int findnum(int x){
int now=root;
while(now){
if(t[now].son[0] and x<=t[t[now].son[0]].size){
now=t[now].son[0];
}
else{
int temp=t[now].cnt;
if(t[now].son[0]){
temp+=t[t[now].son[0]].size;
}
if(x<=temp) return t[now].w;
x-=temp;
now=t[now].son[1];
}
}
return t[now].w;
}
inline int findrank(int x){
int now=root,ans=0;
while(now){
if(x<t[now].w){
now=t[now].son[0];
}
else{
if(t[now].son[0]){
ans+=t[t[now].son[0]].size;
}
if(x==t[now].w){
sply(now);
return ans+1;
}
ans+=t[now].cnt;
now=t[now].son[1];
}
}
return ans+1;
}
inline int findpre(){
int now=t[root].son[0];
while(t[now].son[1]) now=t[now].son[1];
return now;
}
inline int findnext(){
int now=t[root].son[1];
while(t[now].son[0]) now=t[now].son[0];
return now;
}
inline void remove(int x){
findrank(x);
if(t[root].cnt>1){
t[root].cnt--;
update(root);
return;
}
if(!t[root].son[0] and !t[root].son[1]){
clear(root);
root=0;
return;
}
if(!t[root].son[0]){
int oroot=root;
root=t[root].son[1];
t[root].fa=0;
clear(oroot);
return;
}
else if(!t[root].son[1]){
int oroot=root;
root=t[root].son[0];
t[root].fa=0;
clear(oroot);
return;
}
int left=findpre(),oroot=root;
sply(left);
t[root].son[1]=t[oroot].son[1];
t[t[oroot].son[1]].fa=root;
clear(oroot);
update(root);
}
inline int findpre(int x){
insert(x);
int ans=t[findpre()].w;
remove(x);
return ans;
}
inline int findnext(int x){
insert(x);
int ans=t[findnext()].w;
remove(x);
return ans;
}
inline tree* get(int id){
return &t[id];
}
};
class treap{
private:
int tot;
struct tree{
int w,data,size,cnt,son[2];
}t[N];
int root;
public:
tree *operator [](int x){
return &t[x];
}
inline int getroot(){
return root;
}
inline int size(){
return tot;
}
inline void update(int x){
t[x].size=t[t[x].son[0]].size+t[t[x].son[1]].size+t[x].cnt;
}
inline int newnode(int val){
t[++tot]={val,rand(),1,1,{0,0}};
return tot;
}
inline tree* get(int id){
return &t[id];
}
inline void rotate(int &id,int isrignt){
bool k=isrignt;
int temp=t[id].son[k^1];
t[id].son[k^1]=t[temp].son[k];
t[temp].son[k]=id;
id=temp;
update(t[id].son[k]);
update(id);
}
inline void insert(int &id,int x){
if(!id){
id=newnode(x);
return;
}
if(x==t[id].w) t[id].cnt++;
else{
bool k=(x>=t[id].w);
insert(t[id].son[k],x);
if(t[id].data<t[t[id].son[k]].data){
rotate(id,k^1);
}
}
update(id);
}
inline void remove(int &id,int x){
if(!id) return;
if(t[id].w==x){
if(t[id].cnt>1){
t[id].cnt--;
update(id);
return;
}
if(t[id].son[0] or t[id].son[1]){
if(!t[id].son[1] or t[t[id].son[0]].data>t[t[id].son[1]].data){
rotate(id,1);
remove(t[id].son[1],x);
}
else{
rotate(id,0);
remove(t[id].son[0],x);
}
update(id);
}
else{
id=0;
}
return;
}
(x<t[id].w)?remove(t[id].son[0],x):remove(t[id].son[1],x);
update(id);
}
inline int getrank(int id,int x){
if(!id){
return 1;
}
if(x==t[id].w){
return t[t[id].son[0]].size+1;
}
else if(x<t[id].w){
return getrank(t[id].son[0],x);
}
else{
return t[t[id].son[0]].size+t[id].cnt+getrank(t[id].son[1],x);
}
}
inline int getval(int id,int rank){
if(!id) return inf;
if(rank<=t[t[id].son[0]].size){
return getval(t[id].son[0],rank);
}
else if(rank<=t[t[id].son[0]].size+t[id].cnt){
return t[id].w;
}
else{
return getval(t[id].son[1],rank-t[t[id].son[0]].size-t[id].cnt);
}
}
inline int findpre(int x){
int id=root,pre=0;
while(id){
if(t[id].w<x){
pre=t[id].w;
id=t[id].son[1];
}
else{
id=t[id].son[0];
}
}
return pre;
}
inline int findnext(int x){
int id=root,next=0;
while(id){
if(t[id].w>x){
next=t[id].w;
id=t[id].son[0];
}
else{
id=t[id].son[1];
}
}
return next;
}
inline void insert(int x){
insert(root,x);
}
inline void remove(int x){
remove(root,x);
}
inline int findrank(int x){
return getrank(root,x);
}
inline int findnum(int rank){
return getval(root,rank);
}
};
class fhq_treap{
private:
int tot,removed;
struct tree{
int w,data,size,cnt,son[2];
}t[N];
int root;
public:
tree *operator [](int x){
return &t[x];
}
inline int getroot(){
return root;
}
inline int size(){
return tot;
}
tree *get(int id){
return &t[id];
}
inline void update(int x){
t[x].size=1+t[t[x].son[0]].size+t[t[x].son[1]].size;
}
inline int newnode(int val){
t[++tot]={val,rand(),1,1,{0,0}};
return tot;
}
inline int merge(int x,int y){
if(!x or !y) return x+y;
if(t[x].data<t[y].data){
t[x].son[1]=merge(t[x].son[1],y);
update(x);
return x;
}
else{
t[y].son[0]=merge(x,t[y].son[0]);
update(y);
return y;
}
}
inline void split(int now,int k,int &x,int &y){
if(!now) x=y=0;
else{
if(t[now].w<=k){
x=now;
split(t[now].son[1],k,t[now].son[1],y);
}
else{
y=now;
split(t[now].son[0],k,x,t[now].son[0]);
}
update(now);
}
}
inline int find(int now,int rank){
while(1){
if(rank<=t[t[now].son[0]].size){
now=t[now].son[0];
}
else if(rank==t[t[now].son[0]].size+1){
return now;
}
else{
rank-=t[t[now].son[0]].size+1;
now=t[now].son[1];
}
}
}
inline void insert(int x){
int a,b;
split(root,x,a,b);
root=merge(merge(a,newnode(x)),b);
}
inline void remove(int a){
int x,y,z;
split(root,a,x,z);
split(x,a-1,x,y);
y=merge(t[y].son[0],t[y].son[1]);
root=merge(merge(x,y),z);
}
inline int findrank(int a){
int x,y;
split(root,a-1,x,y);
int ans=t[x].size+1;
root=merge(x,y);
return ans;
}
inline int findnum(int rank){
return t[find(root,rank)].w;
}
inline int findpre(int a){
int x,y;
split(root,a-1,x,y);
int ans=t[find(x,t[x].size)].w;
root=merge(x,y);
return ans;
}
inline int findnext(int a){
int x,y;
split(root,a,x,y);
int ans=t[find(y,1)].w;
root=merge(x,y);
return ans;
}
};
}
}

P6136 使用例

#include<bits/stdc++.h>
using namespace std;
using namespace hdk::balanced_tree;
using namespace hdk::fastio; //详见demap合集
#define int long long
fhq_treap s;//可直接更改树名:treap / splay
int n,m,op,x,last,ans;
signed main(){
srand(time(0));
n=read(),m=read();
for(int i=1;i<=n;++i){
x=read();
s.insert(x);
}
while(m--){
op=read(),x=read();
x^=last;
if(op==1){
s.insert(x);
}
if(op==2){
s.remove(x);
}
if(op==3){
last=s.findrank(x);
ans^=last;
}
if(op==4){
last=s.findnum(x);
ans^=last;
}
if(op==5){
last=s.findpre(x);
ans^=last;
}
if(op==6){
last=s.findnext(x);
ans^=last;
}
}
w(ans);
}

[namespace hdk] Balanced_tree 整合的更多相关文章

  1. springcloud必知功能使用教程

    springcloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路 ...

  2. spring 与 CXF 整合 webservice 出现error “Unable to locate Spring NamespaceHandler for XML schema namespace” 总结

    我试了多个版本的spring 发现 出现error : Unable to locate Spring NamespaceHandler for XML schema namespace 并非都是sp ...

  3. SSH整合:Unable to instantiate Action, employeeAction, defined for 'emp-list' in namespace '/'employeeAction - action

    SSH整合,照着视频敲的,不知为何会报错,经历了快两周的折磨给解决了.记录下来给后面需要帮助的人,也算极好的了. Struts Problem Report Struts has detected a ...

  4. SSH整合报错:Unable to instantiate Action, testAction, defined for 'test' in namespace '/'testAction

    报错如下: Struts Problem Report Struts has detected an unhandled exception: Messages: testAction Unable ...

  5. 整合mybatis时报错:Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  6. spring源码分析之freemarker整合

    FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个Java类库,是一款程 ...

  7. SSH框架整合(代码加文字解释)

    一.创建数据库并设置编码. A) create database oa default character set utf8. 二.MyEclipse工程 A) 在Myeclipse里创建web工程, ...

  8. Eclipse Meaven Spring SpringMVC Mybaits整合

    本示例是在:Ubuntu15上实现的:Windows上安装Maven将不太相同. Maven Install Run command sudo apt-get install maven, to in ...

  9. struts2+hibernate整合-实现登录功能

    最近一直学习struts2+hibernate框架,于是想把两个框架整合到一起,做一个小的登录项目.其他不多说,直接看例子. 1).Struts2 和hibernate的环境配置 包括jar包.web ...

  10. Spring,Mybatis 整合Memcache

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

随机推荐

  1. 单细胞测序最好的教程(十六):关于RNA速率你想知道的都在这

    作者按 本章节详细讲解了基于RNA速率的三种拟时序模型,包括稳态模型,EM模型和深度学习模型,并对比了不同模型的适用场景与计算特点.本教程首发于单细胞最好的中文教程,未经授权许可,禁止转载. 全文字数 ...

  2. windows生成苹果私钥证书p12证书和profile文件的方法

    hbuilderx出现已经有差不多10年时间了,现在越来越多的企业,开始使用跨平台性更优秀的uniapp来开发ios app. 开发ios app的时候,打包需要苹果的私钥证书和证书profile文件 ...

  3. RHCA cl210 016 流表 overlay

    Overlay网络是建立在Underlay网络上的逻辑网络 underlay br-int 之间建立隧道 数据流量还是从eth1出去 只有vlan20 是geneve隧道.只有租户网络有子网,子网需要 ...

  4. axios传递参数的使用

    今天在学习elasticsearch时,遇到一个问题:项目中前端采用的是Vue2+axios,后端的接口采用Restful风格来接收: 关于Resultful风格: 1. GET(SELECT):从服 ...

  5. 对比python学julia(第四章:人工智能)--(第二节)人脸识别

    2.1.  项目简介 人脸识别是基于人的脸部特征信息进行身份识别的一种图像识别技术.使用0PenCV 进行人脸识别的过程如下. (1)  针对每个识别对象收集大量的人脸图傣作为样本. (2)  将样本 ...

  6. 【Spring Data JPA】04 JPQL和原生SQL

    @Transactional注解 让Spring处理事务 不需要自己每次都手动开启提交回滚 FINDONE & GETONE的区别? findone是立即加载 getone是延迟加载,配合事务 ...

  7. 【转载】 NVIDIA RTX2080ti不支持P2P Access,这是真的么?

    原文地址: http://www.gpus.cn/gpus_list_page_techno_support_content?id=30 ------------------------------- ...

  8. 强化学习框架chainerrl的安装

    源码地址: https://gitee.com/mirrors_chainer/chainerrl PS: 需要注意的是本文的安装方式并不能支持CUDA,也就是无法使用GPU进行计算:年久失修的计算框 ...

  9. IDEA-实时显示当前所在类及方

    有时候用GIT对比文件修改过大,没法同步修改的时候,这个操作方式就变得极为有用.接下来就是是实操. 1 view 2 Active Editor 3 勾上show Breadcrumbs 4 看IDE ...

  10. [SHOI2009] 会场预约 题解

    LG2161 显然: 任意时刻每个点最多被一条线段覆盖 暴力删每条线段的复杂度是对的 插入 \([l,r]\) 时需要删除的线段要么被 \([l,r]\) 包含,要么覆盖 \(l\) 或 \(r\) ...