Dynamic prescription segment tree (tree segment comprising common operation)

Dynamic open spot on a blog, it is not repeated here.

Dynamic prescription segment tree is a tree line, the query may be common interval to complete segment tree, lazy indicia may also be employed to modify the method section. It should be noted that, in a query or modify the function of, the interval represented by the root node needs to write parameter. Subinterval determined according to the root zone parameters.

Although it is sad to learn a little more a little.

#include <bits/stdc++.h>
#define lson(k) c[k].lson
#define rson(k) c[k].rson
#define sum(a, b) a + b
using namespace std;
const int N = 310;
struct p{
	int lson, rson, sum, lazy;
};
int cnt;
p c[N * 4];
void init(){
	cnt = 0;
}
int build(){
	++cnt;
	c[cnt].lson = -1;
	c[cnt].rson = -1;
	c[cnt].lazy = 0;
	c[cnt].sum = 0;
	return cnt;
}
void update(int ind, int k, int l, int r, int val){
	if (l == r){
		c[k].sum = val;
		return;
	}
	int mid = (l + r) >> 1;
	if (ind <= mid){
		if (c[k].lson == -1)c[k].lson = build();
		update(ind, c[k].lson, l, mid, val);
	}
	else{
		if (c[k].rson == -1)c[k].rson = build();
		update(ind, c[k].rson, mid + 1, r, val);
	}
	c[k].sum = sum(c[k].lson == -1 ? -1 : c[c[k].lson].sum, c[k].rson == -1 ? -1 : c[c[k].rson].sum);
}
void down(int k){
	if (c[k].lazy){
		if (lson(k) == -1){
			lson(k) = build();
		}
		if (rson(k) == -1){
			rson(k) = build();
		}
		c[lson(k)].lazy += c[k].lazy;
		c[rson(k)].lazy += c[k].lazy;
	}
}
int query(int ll, int rr, int l, int r, int k){
	if (ll <= l && rr >= r){
		return c[k].sum;
	}
	int res = 0;
	int mid = (l + r) >> 1;
	if (c[k].lazy){
		down(k);
		c[lson(k)].sum += c[k].lazy * (mid - l + 1);
		c[rson(k)].sum += c[k].lazy * (r - mid);
		c[k].lazy = 0;
	}
	if (ll <= mid){
		if (c[k].lson == -1)c[k].lson = build();
		res = sum(res, query(ll, rr, l, mid, c[k].lson));
	}
	if (rr > mid){
		if (c[k].rson == -1)c[k].rson = build();
		res = sum(res, query(ll, rr, mid + 1, r, c[k].rson));
	}
	return res;
}
void update(int ll, int rr, int l, int r, int k, int d){
	if (ll <= l && rr >= r){
		c[k].sum += d * (r - l + 1);
		c[k].lazy += d;
		return;
	}
	int mid = (l + r) >> 1;
	if (c[k].lazy){
		down(k);
		c[lson(k)].sum += c[k].lazy * (mid - l + 1);
		c[rson(k)].sum += c[k].lazy * (r - mid);
		c[k].lazy = 0;
	}
	if (ll <= mid){
		if (lson(k) == -1){
			lson(k) = build();
		}
		update(ll, rr, l, mid, lson(k), d);
	}
	if (rr > mid){
		if (rson(l) == -1){
			rson(k) = build();
		}
		update(ll, rr, mid + 1, r, rson(k), d);
	}
	c[k].sum = sum(c[lson(k)].sum, c[rson(k)].sum);
}
int main(){
	int root = build();
	update(1, 5, 1, 10, root, 1);
	update(6, 10, 1, 10, root, 2);
	update(3, 7, 1, 10, root, -1);
	int res = query(1, 10, 1, 10, root);
	printf("%d\n", res);
	return 0;
}

 

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

Guess you like

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