码迷,mamicode.com
首页 > 其他好文 > 详细

线段树(Segment Tree)

时间:2020-01-19 00:35:45      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:void   元素   递归   nbsp   cpp   amp   sum   tar   node   

建立线段树

void build_tree(int arr[],int tree[],int node,int start,int end){

	if(start==end){
		tree[node] = arr[start];//叶子节点
	}
	else{
		int mid = (start + end)/2;
		int left_node = 2*node + 1;
		int right_node = 2*node + 2;
		build_tree( arr, tree, left_node, start, mid);//递归建左子树
		build_tree( arr, tree, right_node, mid+1, end);//递归建右子树
		tree[node] = tree[left_node] + tree[right_node];//维护
	}
}
         

 

更新某元素

void update_tree(int arr[],int tree[],int node, int start, int end,int idx, int val){
	 
	 if(start == end){
	 	arr[idx] = val;
	 	tree[node] = val;
	 }
	 
	 else{
	 	int mid = (start + end)/2;
	 	int left_node = 2*node +1;
	 	int right_node = 2*node +2;
	 	if(idx >= start &&idx <= mid){
	 		update_tree( arr, tree, left_node, start, mid, idx, val);
	 	}
	 	else{
	 		update_tree( arr, tree, right_node, mid+1, end, idx, val);
	 	}
	 	tree[node] = tree[left_node]+tree[right_node];
	}
}

  

 

查询区间和

int query_tree(int arr[], int tree[], int node, int start, int end, int L, int R){
	if(R<start||L>end){
		return 0;
	}else if(start == end||start>=L&&end<=R){//这里如果没有后面的,则每次查询都会一直递归到叶子节点,降低效率
		return tree[node];
	}
	else{
	
	
		int mid = (start+end)/2;
		int left_node = 2*node + 1;
		int right_node = 2*node+2;
		int sum_left = query_tree( arr, tree, left_node, start, mid, L, R);
		int sum_right = query_tree( arr, tree, right_node, mid+1, end, L, R);
		return sum_left+sum_right;
	}

 

区间操作(加法)

void plus_tree(int arr[],int tree[],ll node,ll start,ll end,ll L,ll R,ll num){
	if(start == end){
		tree[node] += num;
	}
	else {
		int mid = (start+end)/2;
		int left_node = 2*node+1;
		int right_node = 2*node+2;
		if(L<=mid){
			plus_tree(arr, tree, left_node, start, mid, L, R, num);
		}
		if (R>=mid+1){
			plus_tree(arr, tree, right_node, mid+1, end, L, R, num);
		}
		
		tree[node] = tree[left_node]+tree[right_node];
	}
}

  

 

 

 

线段树(Segment Tree)

标签:void   元素   递归   nbsp   cpp   amp   sum   tar   node   

原文地址:https://www.cnblogs.com/ranbom/p/12209253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!