题面

传送门

题解

鉴于最近的码力实在是弱到了一个境界……回来重新打一下Splay的板子……竟然整整调了一个上午……

//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
inline int getop(){R char ch;while((ch=getc())>'9'||ch<'0');return ch-'0';}
char sr[1<<21],z[20];int K=-1,Z=0;
inline void Ot(){fwrite(sr,1,K+1,stdout),K=-1;}
void print(R int x){
if(K>1<<20)Ot();if(x<0)sr[++K]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++K]=z[Z],--Z);sr[++K]='\n';
}
const int N=1e5+5,inf=0x3f3f3f3f;
struct node;typedef node* ptr;
struct node{
ptr fa,lc,rc;int sz,c,s;
inline node();
inline ptr upd(){sz=lc->sz+rc->sz+c;return this;}
inline ptr son(R int x){return x<s?lc:rc;}
}e[N],*rt;int tot;
inline node::node(){lc=rc=fa=e;}
void rotate(ptr &rt,ptr p){
ptr s=p->fa,t=s->fa;
s!=rt?(t->lc==s?t->lc:t->rc)=p:rt=p;
p->fa=t,s->fa=p;
if(s->lc==p)s->lc=p->rc,p->rc->fa=s,p->rc=s->upd();
else s->rc=p->lc,p->lc->fa=s,p->lc=s->upd();
}
ptr splay(ptr &rt,ptr p){
while(p!=rt){
if(p->fa!=rt)rotate(rt,p->fa->lc==p^p->fa->fa->lc==p->fa?p:p->fa);
rotate(rt,p);
}
return p->upd();
}
void push(int s){
ptr p=rt,las=e;
while(p!=e&&p->s!=s)las=p,p=(s<p->s?p->lc:p->rc);
if(p!=e)++p->c;
else{
p=e+(++tot),p->sz=p->c=1,p->fa=las,p->s=s;
if(las!=e)(s<las->s?las->lc:las->rc)=p;
}
splay(rt,p);
}
ptr find(int s){
ptr p=rt;
while(p->s!=s&&p->son(s)!=e)p=p->son(s);
return splay(rt,p);
}
ptr lst(int s){
ptr p=find(s);
if(p->s<s)return p;
p=p->lc;while(p->rc!=e)p=p->rc;return p;
}
ptr nxt(int s){
ptr p=find(s);
if(p->s>s)return p;
p=p->rc;while(p->lc!=e)p=p->lc;return p;
}
int qwq=0;
void pop(int x){
ptr s=lst(x),t=nxt(x);
splay(rt,s);ptr p=splay(s->rc,t)->lc;
p->c>1?(--p->sz,--p->c):(t->lc=e,0);
t->upd(),s->upd();
}
int Kth(int k){
ptr p=rt;if(k>p->sz)return false;
while(true){
if(p->lc->sz>=k)p=p->lc;
else if(p->lc->sz+p->c<k)k-=p->lc->sz+p->c,p=p->rc;
else return p->s;
}
}
int rk(int x){
ptr p=splay(rt,lst(x));
return p->lc->sz+p->c;
}
int m,op,x;
int main(){
// freopen("testdata.in","r",stdin);
// freopen("testdata.out","w",stdout);
m=read(),rt=e,push(inf),push(-inf);
while(m--){
op=getop(),x=read();
switch(op){
case 1:push(x);break;
case 2:pop(x);break;
case 3:print(rk(x));break;
case 4:print(Kth(x+1));break;
case 5:print(lst(x)->s);break;
case 6:print(nxt(x)->s);break;
}
}
return Ot(),0;
}

洛谷P3369 【模板】普通平衡树(Splay)的更多相关文章

  1. 【洛谷P3369】普通平衡树——Splay学习笔记(一)

    二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...

  2. 洛谷.3369.[模板]普通平衡树(Splay)

    题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...

  3. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  4. 【洛谷P3391】文艺平衡树——Splay学习笔记(二)

    题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...

  5. 洛谷.3369.[模板]普通平衡树(fhq Treap)

    题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...

  6. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  7. 【洛谷P3369】【模板】普通平衡树题解

    [洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...

  8. 洛谷P3369普通平衡树(Treap)

    题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...

  9. [洛谷日报第62期]Splay简易教程 (转载)

    本文发布于洛谷日报,特约作者:tiger0132 原地址 分割线下为copy的内容 [洛谷日报第62期]Splay简易教程 洛谷科技 18-10-0223:31 简介 二叉排序树(Binary Sor ...

  10. 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)

    To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...

随机推荐

  1. 演示Spring框架的JDBC模板的简单操作

    1. 步骤一:创建数据库的表结构 create database spring_day03; use spring_day03; create table t_account( id int prim ...

  2. 纯净版Windows7系统迅雷下载路径

    windows 7 旗舰版64位------------------- Windows 7 Ultimate (x64) - DVD (Chinese-Simplified) 详细信息 文件名     ...

  3. HDU_1022

    题目: As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want t ...

  4. DB2通用数据库性能调整的常用方法

    DB2通用数据库性能调整的常用方法 DB2通用数据库性能调整的常用方法 Agenda 统计值更新--runstats  调整Buffer pool  调整日志缓冲区大小  应用程序堆大小  排序堆大小 ...

  5. KOBAS

    1. What is KOBAS 3.0? KOBAS (KEGG Orthology Based Annotation System) is a web server for gene/protei ...

  6. JSONModel简便应用

    JSONModel, Mantle 这两个开源库都是用来进行封装JSON->Model的, 想想看, 直接向服务器发起一个请求,然后回来后,就是一个Model, 直接使用, 这是一个多么美好的事 ...

  7. 2018.10.18 bzoj4105: [Thu Summer Camp 2015]平方运算(线段树)

    传送门 线段树妙题. 显然平方几次就会循环(打表证明不解释). 然后所有环长度的lcmlcmlcm不大于70. 因此维护一下当前区间中的节点是否全部在环上. 不是直接暴力到叶子节点修改. 否则整体打标 ...

  8. struts2 的特征

    web.xml <filter> <filter-name>struts2</filter-name> <filter-class>org.apache ...

  9. java基础-day3

    第03天 java基础知识 今日内容介绍 u 选择流程控制语句 u 循环流程控制语句 u 控制循环语句 第1章   选择流程控制语句 1.1  顺序结构的基本使用 1.1.1 顺序结构概述 是程序中最 ...

  10. MFC OnOk(),OnCancel(),OnClose(),OnDestroy()的区别总结

    MFC OnOk(),OnCancel(),OnClose(),OnDestroy()的区别总结(转) 第一,OnOK()和OnCancel()是CDialog基类的成员函数,而OnClose()和O ...