#2 WA #12 RE 主席树求助
查看原帖
#2 WA #12 RE 主席树求助
791331
tjr0513楼主2023/6/6 20:09
#include<bits/stdc++.h>
//#define int long long
using namespace std;
void read(int &x) {
	x = 0;
	char ch = getchar();
	bool y = false;
	while (ch > '9' || ch < '0') {
		y |= ch == '-';
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	if (y) x = -x;
}
const int N=1e6;
int n,m,cnt;
int dat[N];
int root[N],ver;
struct Tree{
	int l,r,n;
}tree[N<<5];
int build_tree(int u,int l,int r){
	u=++cnt;
	if(l==r){
		tree[u].n=dat[l];
		return u;
	}
	int mid=(l+r)>>1;
	tree[u].l=build_tree(tree[u].l,l,mid);
	tree[u].r=build_tree(tree[u].r,mid+1,r);
	return u;
}
int clone(int u){
	tree[++cnt]=tree[u];
	return cnt;
}
int modify(int u,int l,int r,int to,int k){
	u=clone(u);
	if(l==r){
		tree[u].n=k;
		return u;
	}
	int mid=(l+r)>>1;
	if(to<=mid){
		tree[u].l=modify(tree[u].l,l,mid,to,k);
	}
	else{
		tree[u].r=modify(tree[u].r,mid+1,r,to,k);
	}
	return u;
}
int ask(int u,int l,int r,int x){
	if(l==r){
		return tree[u].n;
	}
	int mid=(l+r)>>1;
	if(x<=mid){
		return ask(tree[u].l,l,mid,x);
	}
	else{
		return ask(tree[u].r,mid+1,r,x);
	}
}
int main(){
	read(n),read(m);
	for(int i=1;i<=n;i++){
		read(dat[i]);
	}
	root[0]=build_tree(root[0],1,n);
	for(int i=1;i<=m;i++){
		int vi,ch;
		read(vi),read(ch);
		if(ch==1){
			int loc,val;
			read(loc),read(val);
			root[i]=modify(root[vi],1,n,loc,val);
		}
		if(ch==2){
			int loc;
			read(loc);
			printf("%d\n",ask(root[vi],1,n,loc));
			root[i]=root[vi];
		}
	}
	return 0;
}

rt,如题

2023/6/6 20:09
加载中...