题目描述#
如题,你需要维护这样的一个长度为
-
在某个历史版本上修改某一个位置上的值
-
访问某个历史版本上的某一位置的值
此外,每进行一次操作(对于操作2,即为生成一个完全一样的版本,不作任何改动),就会生成一个新的版本。版本编号即为当前操作的编号(从1开始编号,版本0表示初始状态数组)
输入格式#
输入的第一行包含两个正整数
第二行包含
接下来
输出格式#
输出包含若干行,依次为每个操作2的结果。
样例 #1#
样例输入 #1#
5 10
59 46 14 87 41
0 2 1
0 1 1 14
0 1 1 57
0 1 1 88
4 2 4
0 2 5
0 2 4
4 2 1
2 2 2
1 1 5 91plaintext样例输出 #1#
59
87
41
87
88
46plaintext提示#
数据规模:
对于30%的数据:
对于50%的数据:
对于70%的数据:
对于100%的数据:
询问生成的版本是指你访问的那个版本的复制
样例说明:
一共11个版本,编号从0-10,依次为:
* 0 : 59 46 14 87 41
* 1 : 59 46 14 87 41
* 2 : 14 46 14 87 41
* 3 : 57 46 14 87 41
* 4 : 88 46 14 87 41
* 5 : 88 46 14 87 41
* 6 : 59 46 14 87 41
* 7 : 59 46 14 87 41
* 8 : 88 46 14 87 41
* 9 : 14 46 14 87 41
* 10 : 59 46 14 87 91
AC Code#
#include<iostream>
using namespace std;
inline int read(){int x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);return x*f;}
int n,m,idx,root[1000005];
struct node{
int lc;
int rc;
int val;
}tr[1000001*30];
void build(int &pos,int l,int r){
pos=++idx;
if(l==r){
return tr[pos].val=read(),void();
}
int mid = (l+r)>>1;
build(tr[pos].lc,l,mid);
build(tr[pos].rc,mid+1,r);
}
void update(int pre,int &pos,int l,int r,int x,int val){
pos=++idx;tr[pos]=tr[pre];
if(l==r){
return tr[pos].val=val,void();
}
int mid = (l+r)>>1;
if(mid>=x)update(tr[pre].lc,tr[pos].lc,l,mid,x,val);
else update(tr[pre].rc,tr[pos].rc,mid+1,r,x,val);
}
int query(int pos,int l,int r,int x){
if(l==r){
return tr[pos].val;
}
int mid = (l+r)>>1;
if(mid>=x)return query(tr[pos].lc,l,mid,x);
else return query(tr[pos].rc,mid+1,r,x);
}
int main(){
n=read(),m=read();
build(root[0],1,n);
for(int i = 1,ver,loc,val,opt;i<=m;i++){
ver=read(),opt=read(),loc=read();
if(opt&1){
val=read();
update(root[ver],root[i],1,n,loc,val);
}else{
cout<<query(root[ver],1,n,loc)<<endl;
root[i]=root[ver];
}
}
return 0;
}cpp伪代码#
idx:节点编号
mid (左边界+右边界)>>1
上传:(当前节点)
合并 左儿子 右儿子 的信息
建树:(当前节点,左边界,右边界)
当前节点 <- ++idx
if 左界=右界 :
维护一些值
返回
递归建树(当前节点左儿子)
递归建树(当前节点右儿子)
上传(当前节点)
区间查询:(当前节点,左边界,右边界,查询的左边界,查询的右边界)
if 查询的左边界<=左边界 && 右边界<=查询的右边界:
返回当前节点信息
if 查询的左边界<=mid 返回 区间查询(当前节点左儿子)
if 查询的右边界>mid 返回 区间查询(当前节点右儿子)
更新:(上个版本的当前节点,当前版本的当前节点,左边界,右边界,改变的位置,更改的值)
当前节点 <- ++idx
if 左界=右界 :
当前版本的当前节点的值 <- 更改的值
返回
if 改变的位置 <= mid 更新(当前节点左儿子)
else 更新(当前节点右儿子)
上传(当前节点)plaintext
Comments
View on GitHub