【模板】splay维护序列
题目大意:维护一个长度为 N 的序列,支持单点插入,单点询问。
注意事项如下:
- build 函数中要记得初始化 fa。
- 插入两个端点值。
代码如下
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
inline int read(){
	int x=0,f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
	do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
	return f*x;
}
int n,m,a[maxn];
struct node{
	#define ls(x) t[x].ch[0]
	#define rs(x) t[x].ch[1]
	int ch[2],fa,val,size;
}t[maxn<<1];
int tot,root;
inline void pushup(int o){t[o].size=t[ls(o)].size+t[rs(o)].size+1;}
inline bool get(int o){return o==rs(t[o].fa);}
inline void rotate(int o){
	int fa=t[o].fa,gfa=t[fa].fa,d1=get(o),d2=get(fa);
	t[fa].ch[d1]=t[o].ch[d1^1],t[t[o].ch[d1^1]].fa=fa;
	t[fa].fa=o,t[o].ch[d1^1]=fa;
	t[o].fa=gfa,t[gfa].ch[d2]=o;
	pushup(fa),pushup(o);
}
inline void splay(int o,int goal){
	while(t[o].fa!=goal){
		int fa=t[o].fa,gfa=t[fa].fa;
		if(gfa!=goal)get(fa)==get(o)?rotate(fa):rotate(o);
		rotate(o);
	}
	if(!goal)root=o;
}
int find(int o,int k){
	if(k<=t[ls(o)].size)return find(ls(o),k);
	else if(k>t[ls(o)].size+1)return find(rs(o),k-t[ls(o)].size-1);
	else return o;
}
int build(int fa,int l,int r){
	if(l>r)return 0;
	int o=++tot;
	int mid=l+r>>1;
	t[o].val=a[mid],t[o].fa=fa;
	ls(o)=build(o,l,mid-1),rs(o)=build(o,mid+1,r);
	return pushup(o),o;
}
void insert(int pos,int val){
	int x=find(root,pos-1),y=find(root,pos);
	splay(x,0),splay(y,x);
	++tot,t[tot].val=val,t[tot].size=1,t[tot].fa=y,ls(y)=tot;
	pushup(y),pushup(x);
}
void read_and_parse(){
	n=m=read();
	for(int i=2;i<=n+1;i++)a[i]=read();
	root=build(0,1,n+2);
}
void solve(){
	while(m--){
		int opt=read(),l=read(),r=read(),c=read();
		if(opt==0)insert(l+1,r);
		else if(opt==1)printf("%d\n",t[find(root,r+1)].val);
	}
}
int main(){
	read_and_parse();
	solve();
	return 0;
}
【模板】splay维护序列的更多相关文章
- BZOJ 1251 Splay维护序列
		思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ... 
- BZOJ 3223 Tyvj 1729 文艺平衡树  | Splay 维护序列关系
		题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ... 
- BZOJ 3323 splay维护序列
		就第三个操作比较新颖 转化成 在l前插一个点 把r和r+1合并 //By SiriusRen #include <cstdio> #include <cstring> #inc ... 
- Letters Removing CodeForces - 899F (线段树维护序列)
		大意: 给定字符串, 每次删除一段区间的某种字符, 最后输出序列. 类似于splay维护序列. 每次删除都会影响到后面字符的位置 可以通过转化为查询前缀和=k来查找下标. #include <i ... 
- [AHOI 2009] 维护序列(线段树模板题)
		1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ... 
- 洛谷P3373 【模板】线段树 2 && P2023 [AHOI2009]维护序列——题解
		题目传送: P3373 [模板]线段树 2 P2023 [AHOI2009]维护序列 该题较传统线段树模板相比多了一个区间乘的操作.一提到线段树的区间维护问题,就自然想到了“懒标记”:为了降低时间复 ... 
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
		描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ... 
- [Luogu 2023] AHOI2009 维护序列
		[Luogu 2023] AHOI2009 维护序列 恕我冒昧这和线段树模板二有个琴梨区别? #include <cstdio> int n,m; long long p; class S ... 
- [BZOJ 1500] 维护序列
		Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ... 
随机推荐
- jQuery ajax解析xml文件demo
			解析xml文件,然后将城市列表还原到下拉列表框中:当选择下拉列表框时,在对应的文本框中显示该城市信息. 前端代码: <!doctype html> <html> <hea ... 
- MySQL客户端工具及SQL
			一.客户端命令介绍 mysql mysqladmin mysqldump mysql 1.用于数据库的连接管理 2. mysqladmin 1. 2. mysqldump 1. 2. 
- linux中的set -e 与set -o pipefail
			1.set -e "Exit immediately if a simple command exits with a non-zero status." 在“set -e”之后出 ... 
- sonar安装
			##jdk不要用yum下载的 一.下载sonar源码 cd /usr/local/src wget https://sonarsource.bintray.com/Distribution/sonar ... 
- WPF设置软件界面背景为MediaElement并播放视频
			在我们的常见的软件界面设计中我们经常会设置软件的背景为SolidColorBrush或者LinerColorBrush.RadialGradientBrush 等一系列的颜色画刷为背景,有时我们也会使 ... 
- 不幸,我的Ryzen 7 1700X中招了,也有segfault
			在历经了I7-5775C,I7-5820K之后,决定尝鲜用一下为AMD漂亮翻身的Ryzen 7,海淘了一颗Ryzen 7 1700X 最近听说在极重负载的情况下,CPU会出错,于是从网上找来Kill- ... 
- NetScope脱机(localhost)使用[转】
			https://blog.csdn.net/jiwu999/article/details/79626773 方法: step1:git clone https://github.com/ethere ... 
- codeforces263B
			Squares CodeForces - 263B Vasya has found a piece of paper with a coordinate system written on it. T ... 
- Nginx stream如何获取ssl信息并反向代理至上游服务器
			L:116 
- nginx POSTREAD阶段模块
			L:50 Realip模块 需要将--with-http_realip_model 编译进Nginx 因为nginx有可能有反向代理 获取到的客户端ip就不是原用户IP了 X-Forwarded-Fo ... 
