题面

传送门

题解

鉴于最近的码力实在是弱到了一个境界……回来重新打一下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. 淘宝星星的思路c#

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  2. Android界面设计

    从继承关系来看,所有组件继承自View.容器也是继承自View,它能容纳别的View. 所有容器继承自ViewGroup.包括 FrameLayout, LinearLayout, RelativeL ...

  3. action spring 注入错误,如果检查各项注入都没有错误时,考虑struts 是否配置了namespace(如果你有多个namespace="/")

    [ERROR] 2015-01-04 09:42:35,180 (CommonsLogger.java:38) - Exception occurred during processing reque ...

  4. part1:13-linux编译器GCC

    Gcc特点 Gcc基本用法 1.gcc的概念 GCC(GNU Compiler Collection,GNU编译器套装),是一款由GNU开发的编程语言编译器.GCC原名为GNU C 语言编译器,因为它 ...

  5. JavaScript修改注册表

    JavaScript修改注册表 2009-04-14 11:22:13|  分类: JS相关 |  标签: |字号大中小 订阅     注册表有关安全设置项的说明: 注册表路径: HKEY_CURRE ...

  6. 2018.07.03 POJ 2653 Pick-up sticks(简单计算几何)

    Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Description Stan has n sticks of various leng ...

  7. android apk签名原理

    //这个md5跟腾讯的对应 public Signature getPackageSignature( ){ Context context=getContext(); String packageN ...

  8. 类内初始值(c++11)

    1.概念 1)可以为数据成员提供一个类内初始值,创建对象时,类内初始值用于初始化数据成员,没有初始值的成员将被默认初始化 2)类内初始值和赋值类似,或者放在花括号里(如数组),或者放在等号右边,不能使 ...

  9. (最小生成树) Building a Space Station -- POJ -- 2031

    链接: http://poj.org/problem?id=2031 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6011 ...

  10. Hdu2612 Find a way 2017-01-18 14:52 59人阅读 评论(0) 收藏

    Find a way Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...