洛谷P3369 【模板】普通平衡树(Splay)
题面
题解
鉴于最近的码力实在是弱到了一个境界……回来重新打一下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)的更多相关文章
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- 洛谷P3369 【模板】普通平衡树(Treap/SBT)
洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...
- 【洛谷P3369】【模板】普通平衡树题解
[洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...
- 洛谷P3369普通平衡树(Treap)
题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...
- [洛谷日报第62期]Splay简易教程 (转载)
本文发布于洛谷日报,特约作者:tiger0132 原地址 分割线下为copy的内容 [洛谷日报第62期]Splay简易教程 洛谷科技 18-10-0223:31 简介 二叉排序树(Binary Sor ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
随机推荐
- ThinkPHP5路由图解
- 前后台交互(打开前端页面,不传递任何数据,发送ajax请求)
1.打开前端,不传递任何数据 <script src="./jquery.min.js"></script> <script> $(docume ...
- [SoapUI] 比较JSON Response
比较两个JSON, ID是数字时,处理成统一的格式:只保留小数点后5位 package direct; import org.json.JSONArray; import org.json.JSONE ...
- c++11 随机代码记录
// RadomTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #includ ...
- struts2 的 ServletActionContext 和 actionContext,服务器代码测试, redirect 、dispatcher、chain、redirectAction
一.ServletActionContext 和 actionContext HttpServletRequest request=ServletActionContext.getRequest() ...
- C山寨C++
#include <stdio.h> #include <string.h> #include <malloc.h> typedef struct Aclass_s ...
- Oracle 输出树形结构
Oracle 输出树形结构 树形结构,根 select connect_by_root(cat.parentid) root,cat.id,cat.parentid,cat.name,cat.code ...
- 利用ajaxSubmit()方法实现Form提交表单后回调
1. 背景 最近在工作中,需要实现网页端图片上传到FTP服务器的功能.上传文件是用Form表单提交数据的方法向后台传输文件流,在此遇到了一个问题:后台在处理完图片上传功能后,需要向前台回传是 ...
- 2018.09.21 atcoder An Invisible Hand(贪心)
传送门 简单贪心啊. 这题显然跟t并没有关系,取差量最大的几组买入卖出就行了. 于是我们统计一下有几组差量是最大的就行了. 代码: #include<bits/stdc++.h> #def ...
- 【Unity】2.0 第2章 Unity编辑器和基本操作
分类:Unity.C#.VS2015 创建日期:2016-03-26 本章要点: 1.掌握Unity 5.3.4编辑器视图和菜单项及其含义,这是入门的最基础部分,必须掌握. 2.了解最基本的操作,先学 ...