萌新刚学OI,求助可持久化线段树
查看原帖
萌新刚学OI,求助可持久化线段树
217323
ZnH2楼主2021/9/3 16:57

RT ,WA了后七个点,似乎只有个别的几个询问会出问题。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>

#define ll int
#define F(i, a, n) for (ll i = (a); i <= (n); ++i)
using std::max;
using std::min;
const ll S1 = 1e6+3;
const ll S2 = 23000005;
const ll inf = 0x3f3f3f3f;
const ll p = 1e9 + 7;
ll rd();
ll n, m;
ll a[S1];

struct T {
	ll d,l,r;
	ll ls,rs;
	T() {
		l=r=0;
	}
} node[S2];
ll list[S1],his;
ll mun;

void build(T &fa) {
	if(fa.l==fa.r) {
		fa.d=a[fa.l];
		return;
	}

	ll m=(fa.l+fa.r)>>1;
	node[++mun].l=fa.l;
	fa.ls=mun;
	node[mun].r=m;
	build(node[mun]);

	fa.d+=node[fa.ls].d;

	node[++mun].l=m+1;
	fa.rs=mun;
	node[mun].r=fa.r;
	build(node[mun]);

	fa.d+=node[fa.rs].d;
}

void change(ll loc,ll value,T &fa,T hisfa) {
	if(fa.l==fa.r) {
		fa.d=value;
		return;
	}
	ll m=(fa.l+fa.r)>>1;
	if(loc<=m) {
		mun++;
		node[mun].l=fa.l;
		node[mun].r=m;
		fa.ls=mun;
		fa.rs=hisfa.rs;
//		printf("%lld\n",hisfa.rs);
		change(loc,value,node[mun],node[hisfa.ls]);
		fa.d=node[fa.ls].d+node[fa.rs].d;
	} else {
		mun++;
		node[mun].l=m+1;
		node[mun].r=fa.r;
		fa.ls=hisfa.ls;
		fa.rs=mun;
		change(loc,value,node[mun],node[hisfa.rs]);
		fa.d=node[fa.ls].d+node[fa.rs].d;
	}
}

ll get_mun(T now,ll loc) {
	if(now.l==now.r) {
		return now.d;
	}
	ll m=(now.l+now.r)>>1;
	//printf("%lld %lld %lld %lld \n",now.ls,now.rs,now.l,now.r);
	if(loc<=m) return get_mun(node[now.ls],loc);
	else return get_mun(node[now.rs],loc);
}

int main() {
//	freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
	n=rd();
	m=rd();
	F(i,1,n) a[i]=rd();
	node[++mun].l=1;
	node[mun].r=n;
	build(node[mun]);
	list[his]=1;
//	F(i,1,mun) printf("%lld %lld %lld %lld \n",node[i].ls,node[i].rs,node[i].l,node[i].r);
	F(i,1,m) {
		ll v=rd();
		ll t=rd();
		ll loc=rd();
		if(t==1) {
			ll value=rd();
			++his;
			++mun;
			list[his]=mun;
			node[mun].l=1;
			node[mun].r=n;
			change(loc,value,node[mun],node[list[v]]);
			//printf("%lld\n",node[list[his]].rs);
		} else {
			list[his+1]=list[his];
			his++;
			printf("%d\n",get_mun(node[list[v]],loc));//
		}
	}
	return 0;
}

ll rd() {
	ll s = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') {
			f = -1;
		}
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		s = s * 10 + ch - '0';
		ch = getchar();
	}
	return s * f;
}

救救孩子吧/kk

2021/9/3 16:57
加载中...