线段树求助,这个复制有问题吗?
查看原帖
线段树求助,这个复制有问题吗?
800543
NirvanaCeleste楼主2024/7/18 11:38
#include <bits/stdc++.h>
using namespace std;
inline void read(int& rin){
	int rout = 0,w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9'){
		rout = rout * 10  + ch - '0';
		ch = getchar();
	}
	rin = rout * w;
}
void write(int x){
	if(x < 0) putchar('-'),x=-x;
	if(x > 9) write(x/10);
	putchar(x%10 +'0');
}
int n,m,tot = 1,ptot;
int a[1000010],root[1000010];
struct tree{
	int ls,rs,vlu;
}t[1000010 * 4 * 10];
void build(int p,int l,int r){
	if(l == r){
		t[p].vlu = a[l];
		return;
	}
	int mid = (l + r - 1) >> 1;
	if(l <= mid) t[p].ls = ++tot,build(t[p].ls,l,mid);
	if(mid < r) t[p].rs = ++tot,build(t[p].rs,mid+1,r);
}
int find(int p,int l,int r,int who){
	if(l == r) return t[p].vlu;
	int mid = (l + r - 1) >> 1;
	if(who <= mid) return find(t[p].ls,l,mid,who);
	else return find(t[p].rs,mid+1,r,who);
}
void add(int p,int l,int r,int who,int cnt){
	t[++tot] = t[p];
	if(l == r){
		t[tot].vlu = cnt;
		return;
	}
	int mid = (l + r - 1) >> 1;
	if(who <= mid) add(t[p].ls,l,mid,who,cnt);
	else add(t[p].rs,mid+1,r,who,cnt);
} 
int main(){
	root[0] = 1;
	cin>>n>>m;
	for(int i=1;i<=n;i++) read(a[i]);
	build(1,1,n);
	int v,k,who,value;
	for(int i=1;i<=m;i++){
		read(v),read(k),read(who);
		int p = root[v];
		if(k == 1){
			read(value);
			root[++ptot] = tot + 1;
			add(p,1,n,who,value);
		}else{
			root[++ptot] = p;
			write(find(p,1,n,who));
			putchar('\n');
		}
	}
	
	return 0;
}
2024/7/18 11:38
加载中...