可持久化线段树样例没过求调
查看原帖
可持久化线段树样例没过求调
661534
Dehydration楼主2023/8/9 14:36
#include<bits/stdc++.h>
using namespace std;
int n,m,v[1000005],root[1000005];
int tot=0;
struct dian
{
	int l,r,sum;
};
dian a[1000005];
int twice(int id)
{
	tot++;
	a[tot]=a[id];
	return tot;
}
int make_tree(int L,int R)
{
	++tot;
	if(L==R)
	{
		a[tot].sum=v[L];
		return tot;
	}
	int mid=(L+R)/2;
	a[tot].l=make_tree(L,mid);a[tot].r=make_tree(mid+1,R);
	return tot;
}
int add(int x,int L,int R,int sid,int s)
{
	x=twice(x);
	if(L==R)
	{
		a[x].sum=s;
		return x;
	}
	int mid=(L+R)/2;
	if(sid<=mid)a[x].l=add(a[x].l,L,mid,sid,s);else a[x].r=add(a[x].r,mid+1,R,sid,s);
	return x;
}
int query(int x,int L,int R,int id)
{
	if(L==R)return a[x].sum;
	int mid=(L+R)/2;
	if(id<=mid)return query(a[x].l,L,mid,id);else return query(a[x].r,mid+1,R,id);	
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>v[i];
	root[0]=make_tree(1,n);
	for(int i=1;i<=m;i++)
	{
		int k,op,id,Do;
		cin>>k>>op>>id;
		if(op==1)
		{
			cin>>Do;
			root[i]=add(root[k],1,n,id,Do);
		}else
		{
			cout<<query(root[k],1,n,id)<<'\n';
			root[i]=root[k];
		}
	}
	return 0;
}    
2023/8/9 14:36
加载中...