Dynamic prescription segment tree

Prerequisite dynamic open point is Chairman of the tree, is considered to be a Keqianyuxi Chairman tree learning it.

Personal understanding of the dynamics of a binary tree similar to the prescription list to achieve, there are two pointers to the left and right son. Dynamic open point roughly the same way. In order to maximum range as an example, the segment tree maintains three information:

  1. Current son left node number of the node
  2. Right Son current node node number
  3. Interval maximum value

 First contribution, contribution as a function of

int build(){
	++cnt;
	c[cnt].l = 0;
	c[cnt].r = 0;
	c[cnt].max = -inf;
	return cnt;
}

Represent the addition of a new node, about the son of the newly added nodes are 0 (empty), the current node number returns out. If the new node increases, it must be from the time of the visit found no parent node down, this is the need to create a new son node, then the parent node number left or right son node is the current number of the new node.

The initial time, the need for a root, root=build()create a new point, this point as the root of it.

When performing single-point update, for example, at an index indexof the position change, an update function and the segment tree is similar to the following

void insert(int l, int r, int k, int ind, int d){
	if (l == r){
		c[k].max += d;
		return;
	}
	int mid = (l + r) >> 1;
	if (ind <= mid){
		if (c[k].l == 0)c[k].l = build();
		insert(l, mid, c[k].l, ind, d);
	}
	else{
		if (c[k].r == 0)c[k].r = build();
		insert(mid + 1, r, c[k].r, ind, d);
	}
	c[k].max = max(c[c[k].l].max, c[c[k].r].max);
}

If you find this point to update and return, otherwise, keep looking to find the time or the interval into two, look at this point is which section, for just a walk around to the son of the current node, take the time to judge , assuming that the son is taking the left node, if left son exist, continue to walk, does not exist, then you need to create a node son left to continue walking. And the rest is the same as the segment tree.

Query is similar, not repeat them.

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof a)
using namespace std;
const int N = 310;
const int inf = 0x7fffffff;
struct p{
	int l, r, max;
};
p c[N * 4];
int root, cnt;
int build(){
	++cnt;
	c[cnt].l = 0;
	c[cnt].r = 0;
	c[cnt].max = -inf;
	return cnt;
}
void insert(int l, int r, int k, int ind, int d){
	if (l == r){
		c[k].max = d;
		return;
	}
	int mid = (l + r) >> 1;
	if (ind <= mid){
		if (c[k].l == 0)c[k].l = build();
		insert(l, mid, c[k].l, ind, d);
	}
	else{
		if (c[k].r == 0)c[k].r = build();
		insert(mid + 1, r, c[k].r, ind, d);
	}
	c[k].max = max(c[c[k].l].max, c[c[k].r].max);
}
int query(int ind, int k, int l, int r){
	if (l == r){
		return c[k].max;
	}
	int mid = (l + r) >> 1;
	if (ind <= mid)return query(ind, c[k].l, l, mid);
	else return query(ind, c[k].r, mid + 1, r);
}
int main()
{
	root = build();
	insert(1, 5, root, 2, 1);
	insert(1, 5, root, 1, 2);
	insert(1, 5, root, 3, 4);
	insert(1, 5, root, 4, 3);
	insert(1, 5, root, 5, 5);
	for (int i = 1; i <= 5; i++){
		printf("%d : %d\n",i, query(i, root, 1, 5));
	}
	return 0;
}

 

Published 204 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/104427667