题面

传送门

题解

鉴于最近的码力实在是弱到了一个境界……回来重新打一下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. SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  2. Golang之并发篇

    进程和线程 A.进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位. B.线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位. C.一 ...

  3. Django之ORM数据库

    5.1 数据库的配置 1    django默认支持sqlite,mysql, oracle,postgresql数据库.  <1> sqlite django默认使用sqlite的数据库 ...

  4. BZOJ 1430 小猴打架 - prufer数列

    描述 一开始森林里面有N只互不相识的小猴子,它们经常打架,但打架的双方都必须不是好朋友.每次打完架后,打架的双方以及它们的好朋友就会互相认识,成为好朋友.经过$N-1$次打架之后,整个森林的小猴都会成 ...

  5. [Groovy] 学习Groovy的好网站(内容全面)

    https://www.tutorialspoint.com/groovy/index.htm

  6. 一次HBase问题的解决过程(Status: INCONSISTENT)

    ==版本信息== HBase:2.7.1 Storm:1.0.1 RocketMQ:3.4.6(阿里版) ==问题描述== 2018年9月3号晚上23点左右,例行巡检系统运行状况时, 发现Storm消 ...

  7. /etc/inittab加入自动启动格式

    R01:35:respawn:/usr/bin/exe_program 说明 R01:标识,每一行必须唯一(R01并无特殊含义,可自定义). 35:有效模式,3字符界面启动,5图形界面启动 respa ...

  8. phoenix错误

    spark集群与phoenix进行数据请求时报错: 2018-06-15 17:13:30,158 INFO - Starting task 15.3 in stage 116.0 (TID 6832 ...

  9. 2018.08.22 NOIP模拟 string(模拟)

    string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...

  10. MUI框架开发HTML5手机APP(一)--搭建第一个手机APP(转)

    出处:http://www.cnblogs.com/jerehedu/p/7832808.html  前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...