社区讨论

第二个WA了

P3919【模板】可持久化线段树 1(可持久化数组)参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@m4ydygw5
此快照首次捕获于
2024/12/22 00:20
去年
此快照最后确认于
2025/11/04 12:30
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define mid ((l+r)>>1)
#define int long long
using namespace std;
int top,a[1000660],n,m,rot[1000660];
struct node{
	int l,r,v;
}tree[20006660];
inline int read() {
	int f = 1, k = 0;
	char c = getchar();
	while(c < '0' || c > '9'){
		if(c == '-') 	f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9')
		k=k * 10 + c - '0',	c = getchar();
	return f * k;
}
inline int clone(int root){
	tree[++top]=tree[root];
	return top;
}
int build(int root,int l,int r){
	root=(++top);
	if(l==r){
		tree[root].v=a[l];
		return top;
	}
	tree[root].l=build(tree[root].l,l,mid);
	tree[root].r=build(tree[root].r,mid+1,r);
	return root;
}
int modify(int root,int l,int r,int x,int val){
	root=clone(root);
	if(l==r) tree[root].v=val;
	else{
		if(x<=mid) tree[root].l=modify(tree[root].l,l,mid,x,val);
		else tree[root].r=modify(tree[root].r,mid+1,r,x,val);
	}
	return root;
}
int query(int root,int l,int r,int x){
	if(l==r) return tree[root].v;
	else{
		if(x<=mid) return query(tree[root].l,l,mid,x);
		else return query(tree[root].r,mid+1,r,x);
	}		
}
signed main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++) a[i]=read();
	rot[0]=build(0,1,n);
	for(int i=1;i<=m;i++){
		int rt,op,x;
		rt=read(),op=read(),x=read();
		if(op==1){
			int y;
			y=read();
			rot[i]=modify(rot[rt],1,n,x,y);
		}
		else{
			printf("%lld\n",query(rot[rt],1,n,x));
			rot[i]=rot[rt];
		}
	}
}

回复

1 条回复,欢迎继续交流。

正在加载回复...