Segment Tree Template

本文详细介绍了区间树状数组的数据结构实现,包括构建、更新单点及区间、查询区间求和或最大值等核心操作,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define lson l,m,n<<1
#define rson m+1,r,n<<1|1

int num[MAXN],sum[MAXN<<2],max[MAXN<<2],add[MAXN<<2];

void pushUp(int n)
{
	sum[n]=sum[n<<1]+sum[n<<1|1];
	max[n]=max(max[n<<1],max[n<<1|1]);
}

void build(int l,int r,int n)
{
	add[n]=0;
	if(l==r)
	{
		scanf("%d",&num[n]);
		sum[n]=max[n]=num[n];
		return;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	pushUp(n);
}

//update only one point
void update(int x,int v,int l,int r,int n)
{
	if(l==r)
	{
		//oprate here
		return;
	}
	int m=(l+r)>>1;
	if(x<=m) //Very Important
		update(x,v,lson);
	else update(x,v,rson);
	pushUp(n);
}

//get sum or max val of [L,R]
int query(int L,int R,int l,int r,int n)
{
	if(L<=l&&r<=R)
	{
		return sum[n];
		// return max[n];
	}
	int ans=0,m=(l+r)>>1;
	if(L<=m)
		ans+=query(L,R,lson);
		// ans=max(ans,query(L,R,lson));
	if(m<R)
		ans+=query(L,R,rson);
		// ans=max(ans,query(L,R,rson));
	return ans;
}

void pushDown(int n,int m)
{
	if(add[n])
	{
		add[n<<1]+=add[n];
		add[n<<1|1]+=add[n];
		sum[n<<1]+=add[n]*(m-(m>>1));
		sum[n<<1|1]+=add[n]*(m>>1);
		add[n]=0;
	}
}

//update [L,R]
void update(int L,int R,int v,int l,int r,int n)
{
	if(L<=l&&r<=R)
	{
		add[n]+=v;
		sum[n]+=v*(r-l+1);
		return;
	}
	pushDown(n,r-l+1);
	int m=(l+r)>>1;
	if(L<=m)
		update(L,R,v,lson);
	if(m<R)
		update(L,R,v,rson);
	pushUp(n);
}

int query(int L,int R,int l,int r,int n)
{
	if(L<=l&&r<=R)
	{
		return sum[n];
	}
	pushDown(n,r-l+1);
	int m=(l+r)>>1;
	int ans=0;
	if(L<=m)
		ans+=query(L,R,lson);
	if(m<R)
		ans+=query(L,R,rson);
	return ans;
}

帮我看一下这段代码为什么在洛谷P3919 RE了: #include <bits/stdc++.h> using namespace std; // Persistent Segment Tree template<class Value> class PersistentSGT { public: PersistentSGT(int _n) : n(_n) { root.push_back(0); build(root[0], 1, n, nullptr); } PersistentSGT(Value* begin, Value* end) { n = end - begin; root.push_back(0); build(root[0], 1, n, begin); } void modify(int ver, int idx, Value val) { root.push_back(root[ver]); modify(root.back(), 1, n, idx, val); } Value query(int ver, int idx) { root.push_back(root[ver]); return query(root.back(), 1, n, idx); } private: void build(int& cur, int l, int r, Value* arr) { tr.emplace_back(); cur = tr.size() - 1; if (l == r) tr[cur].val = arr ? arr[l - 1] : Value(); else { int mid = (l + r) / 2; build(tr[cur].ls, l, mid, arr); build(tr[cur].rs, mid + 1, r, arr); } } void modify(int& cur, int l, int r, int idx, Value val) { tr.push_back(tr[cur]); cur = tr.size() - 1; if (l == r) { tr[cur].val = val; return ; } int mid = (l + r) / 2; if (idx <= mid) modify(tr[cur].ls, l, mid, idx, val); else modify(tr[cur].rs, mid + 1, r, idx, val); } int query(int cur, int l, int r, int idx) { if (l == r) return tr[cur].val; int mid = (l + r) / 2; if (idx <= mid) return query(tr[cur].ls, l, mid, idx); else return query(tr[cur].rs, mid + 1, r, idx); } int n; struct Node { int ls, rs; Value val; }; vector<Node> tr; vector<int> root; }; const int MAX_N = 1e6 + 50; int n, m, a[MAX_N]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; PersistentSGT<int> t(a + 1, a + n + 1); for (int i = 1, ver, opt, idx, val; i <= m; i++) { cin >> ver >> opt >> idx; if (opt == 1) { cin >> val; t.modify(ver, idx, val); } else cout << t.query(ver, idx) << '\n'; } return 0; }
最新发布
03-22
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值