题目大意:维护一个长度为 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维护序列的更多相关文章

  1. BZOJ 1251 Splay维护序列

    思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ...

  2. BZOJ 3223 Tyvj 1729 文艺平衡树 | Splay 维护序列关系

    题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...

  3. BZOJ 3323 splay维护序列

    就第三个操作比较新颖 转化成 在l前插一个点 把r和r+1合并 //By SiriusRen #include <cstdio> #include <cstring> #inc ...

  4. Letters Removing CodeForces - 899F (线段树维护序列)

    大意: 给定字符串, 每次删除一段区间的某种字符, 最后输出序列. 类似于splay维护序列. 每次删除都会影响到后面字符的位置 可以通过转化为查询前缀和=k来查找下标. #include <i ...

  5. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  6. 洛谷P3373 【模板】线段树 2 && P2023 [AHOI2009]维护序列——题解

    题目传送: P3373 [模板]线段树 2  P2023 [AHOI2009]维护序列 该题较传统线段树模板相比多了一个区间乘的操作.一提到线段树的区间维护问题,就自然想到了“懒标记”:为了降低时间复 ...

  7. HNOI2004宠物收养所(splay维护二叉搜索树模板题)

    描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  8. [Luogu 2023] AHOI2009 维护序列

    [Luogu 2023] AHOI2009 维护序列 恕我冒昧这和线段树模板二有个琴梨区别? #include <cstdio> int n,m; long long p; class S ...

  9. [BZOJ 1500] 维护序列

    Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ...

随机推荐

  1. C# Note8: 设计模式全解

    前言——资源说明 目前网上设计模式的介绍可谓非常之多(各种编程语言的版本),其中不乏精细之作,本文的目的在于搜集和整理C#或C++的设计模式,毕竟思想还是共通的! 设计模式的分类 创建型模式,共五种: ...

  2. 运行pip报错:Fatal error in launcher: Unable to create process using '"'

    参考: https://blog.csdn.net/cjeric/article/details/73518782

  3. Kettle 变量(arg位置参数)

    1.表输入中使用?占位作为kettle转换变量 数据预览: 获取变量数据: 使用?传入变量 需要勾选替换sql语句中的变量,并选则从步骤插入数据中所在步骤 数据预览

  4. Swagger2常用注解及其说明 (转)

    Api 用在Controller中,标记一个Controller作为swagger的文档资源 属性名称 说明 value Controller的注解 description 对api资源的描述 hid ...

  5. Netty ByteBuf 和 String 转换

    参考https://blog.csdn.net/o1101574955/article/details/81024102 参考http://youyu4.iteye.com/blog/2361959 ...

  6. WPF实现滚动显示的TextBlock

    在我们使用TextBlock进行数据显示时,经常会遇到这样一种情况就是TextBlock的文字内容太多,如果全部显示的话会占据大量的界面,这是我们就会只让其显示一部分,另外的一部分就让其随着时间的推移 ...

  7. 老男孩python学习自修【第一天】文件IO用法

    第一天   文件IO处理 1.读文件实例 file_split.python f = file('myFile.txt', 'r') for line in f.readlines(): line = ...

  8. qtp 自动货测试桌面程序-笔记(使用参数 parameters)

    dtGlobalSheet:运行整个test时候使用的参数(心得:可以将公共使用的测试数据放于全局表格中,所有action脚本都可以使用同一个数据,如供应商.客户.商品) dtActionSheet: ...

  9. npm火速上手

    npm,即node  package manager,翻译过来就是“node包管理工具”.“node包”是啥呢?它就是jquery啦.bootstrap啦之类的各种版本. 1.npm的安装 第一步,下 ...

  10. Play framework框架中通过post方式发送请求

    搞了好久这个最终还是在play官方文档中看见的发送请求的方式,国内好像很少有使用这个框架的,加之自己不是太愿意宣传,好东西总归是好东西,不说废话了. 在play中发送请求有两种常用的方式,一种get, ...