主席树求调(悬关)
查看原帖
主席树求调(悬关)
1328928
only_once楼主2024/7/26 19:27
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define L tree[root].lson
#define R tree[root].rson
#define LL tree[root].l
#define RR tree[root].r
const int N=1e6+5;
int id=1,n,m,a[N],rt[N],tot=0;
struct node{
	int l,r,root,sum,lson,rson;
}tree[N];
void update(int root){
	tree[root].sum=tree[L].sum+tree[R].sum;
}
void build(int l,int r,int root){
	tree[root].l=l;
	tree[root].r=r;
	if(l==r){
		tree[root].sum=a[l];
		return ;
	}
	int mid=(l+r)>>1;
	L=++id;
	build(l,mid,id);
	R=++id;
	build(mid+1,r,id);
	update(root);
}
void change(int root,int k,int x){
	if(LL==RR){
		tree[id].sum=x;
		return ;
	}
	int mid=(LL+RR)>>1;
	if(k<=mid){
		tree[id+1].l=LL;
		tree[id+1].r=mid; 
		tree[id].rson=R;
		tree[id].lson=++id;
		change(L,k,x);
	}
	else{
		tree[id+1].l=mid+1;
		tree[id+1].r=RR; 
		tree[id].rson=L;
		tree[id].lson=++id;
		change(R,k,x);
	}
	update(id);
}
int cheak(int root,int k){
	if(LL==RR){
		return tree[root].sum;
	}
	int mid=(LL+RR)>>1;
	if(k<=mid){
		return cheak(L,k);
	}
	else{
		return cheak(R,k);
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	build(1,n,1);
	rt[0]=1;
	while(m--){
		tot++;
		int b,op,l,v;
		cin>>b>>op>>l;
		if(op==1){
			cin>>v;
			id++;
			rt[tot]=id;
			change(rt[b],l,v);
		}
		else{
			rt[tot]=rt[tot-1];
			cout<<cheak(rt[b],l)<<endl;
		}
	}
	return 0;
}
2024/7/26 19:27
加载中...