萌新刚学主席树WA16求调
查看原帖
萌新刚学主席树WA16求调
755789
Misty_Post楼主2025/8/5 09:04
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define ls tr[now].lso
#define rs tr[now].rso
struct nood{
	ll z,lso,rso;
}tr[40000000];
ll n,m,cnt,v,op,p,c,a[3000000],rt[3000000];
void bui(ll l,ll r,ll &now){
//	cout<<l<<" "<<r<<":"<<now<<endl;
	if(!now){
		now=++cnt;
	}
	if(l==r){
		tr[now].z=a[l];
		return;
	}
	ll mid=(l+r)>>1;
	bui(l,mid,ls);
	bui(mid+1,r,rs);
	return;
}
void update(ll &now,ll lst,ll l,ll r,ll pos,ll val){
	//当前节点 now,上一个版本对应节点 lst,左端点 l,右端点 r,修改的位置 pos 和修改的值 val
	if(!now){
		now=++cnt;
	}
	tr[now]=tr[lst];
	if(l==r){
		tr[now].z=val;
		return;
	}
	ll mid=(l+r)>>1;
	if(pos<=mid){
		update(ls,tr[lst].lso,l,mid,pos,val);
	}
	else{
		update(rs,tr[lst].rso,mid+1,r,pos,val);
	}
	return;
}
ll que(ll &now,ll lst,ll l,ll r,ll pos){
	//当前节点 now,上一个版本对应节点 lst,左端点 l,右端点 r,修改的位置 pos
	if(!now){
		now=++cnt;
	}
	tr[now]=tr[lst];
	if(l==r){
		return tr[now].z;
	}
	ll mid=(l+r)>>1;
	if(pos<=mid){
		return que(ls,tr[lst].lso,l,mid,pos);
	}
	else{
		return que(rs,tr[lst].rso,mid+1,r,pos);
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	bui(1,n,rt[0]);
	for(int i=1;i<=m;i++){
		cin>>v>>op>>p;
		if(op==1){
			cin>>c;
			update(rt[i],rt[v],1,n,p,c);
		}
		else{
			cout<<que(rt[i],rt[v],1,n,p)<<"\n";
		}
	}
} 
2025/8/5 09:04
加载中...