【模板】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.维护序列时始终记得 ...
随机推荐
- win10查看无线密码
- java 中Excel的导入导出
部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字 的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...
- Alertmanager 安装(k8s报警)
一.下载Alertmanager https://prometheus.io/download/ wget https://github.com/prometheus/alertmanager/rel ...
- IntelliJ IDEA详情
详情请参考http://www.phperz.com/article/15/0923/159043.html
- vue組件
組件有局部組件和全局組件,全局組件,其它的元素能夠調用. Prop父組件子組件看不大明白.
- How to enable flash on Chromium
sudo apt install chromium-browser pepperflashplugin-nonfree
- codeforces509B
Painting Pebbles CodeForces - 509B There are n piles of pebbles on the table, the i-th pile contains ...
- TLS/SSL
- MT【267】第一次很重要
\begin{equation*}\textbf{已知}x_1,x_2<\pi,x_{n+1}=x_n+\left\{ \begin{aligned} sin x_n &,x_n> ...
- kubernetes 基础命令及操作
获取集群的基本信息kubectl cluster-infokubectl get nodeskubectl get namespaceskubectl get deployment --all-nam ...