线段树(segment tree)

线段树(segment tree)

一:基本操作

1:建树

线段树的每个父节点均为一个范围,故称之为线段树

struct node{
	int l,r,weight,mark;     //左孩子,右孩子,区间和,延时标记  
}tree[maxn*4+10];             //开四倍空间

在这里插入图片描述

思路:1: 遇到叶子节点,直接赋值
2:否则分治,左右节点
3:最后归并,父节点的值 = 两子节点的值
void build(int l, int r, int root){
	tree[root].l = l;
	tree[root].r = r;
	if(l == r){
		scanf("%d", &tree[root].w);  //叶子节点的值 
		return; 
	}
	
	int mid = l+(r-l)/2;
	build( l ,mid,  root*2);
	build(mid+1,r, root*2+1);
    
	tree[root].w = tree[root*2].w + tree[root*2+1].w;      //类似归并  
}

2:向下修改

思路:(六行代码)
1:左右儿子分别加上根树的mark
2:左右儿子的值分别加上 mark*各自区间节点个数 (乘的是父节点mark
3:父根节点 mark 归 0
void push_down(int root){
	if(tree[root].mark){
		int mark = tree[root].mark;
		tree[root*2].mark += mark;
		tree[root*2+1].mark += mark;
		tree[root*2].w += mark * (tree[root*2].r-tree[root*2].l+1);
		tree[root*2+1].w += mark * (tree[root*2+1].r-tree[root*2+1].l+1);
		tree[root].mark = 0;
	}
} 

3:区间修改

思路:1:父节点区间若 完全包含于 需要修改的区间中,
标记 += k,值 += 儿子个数 * k
2:若之前有标记过mark,立即做向下调整
3:如果左儿子区间 与 需修改区间 有交集,往左走
如果右儿子区间 与 需修改区间 有交集,往右走
4:归并,父节点值 = 子节点值相加
void modify_section(int root, int l, int r, int k){
	if(tree[root].l >= l && tree[root].r <= r){
		tree[root].w += k*(tree[root].r-tree[root].l+1); //值 = 子个数 * k  
		tree[root].mark += k;
		return;
	}
	
	if(tree[root].mark)
		push_down(root);
	
	int mid = (tree[root].l + tree[root].r)/2;			//mid是root的中间
	if(l<=mid) modify_section(root*2, l, r, k);           //注意:l,r不变
	if(mid<r) modify_section(root*2+1, l, r, k);
	
	tree[root].w = tree[root*2].w + tree[root*2+1].w;
}

4:单点修改

​ 位置为 x, 值加 k

void change_point(int root,int x, int k){
	if(tree[root].l == tree[root].r){
		tree[root].w += k;
		return ;
	}
	if(tree[root].mark) push_down(root);
	
	int mid = (tree[root].l + tree[root].r)/2;
	if(x<=mid)
		change_point(root*2, x, k);
	else
		change_point(root*2+1, x, k);
	
	tree[root].w = tree[root*2].w + tree[root*2+1].w;
} 

5:线段树的区间查询

查询 l 到 r 的区间和 :

1、如果这个区间被完全包括在目标区间里面,直接返回这个区间的值
2、如果这个区间的左儿子和目标区间有交集,那么搜索左儿子
3、如果这个区间的右儿子和目标区间有交集,那么搜索右儿子
void check_section(int root,int l,int r, int &ans){
	if(tree[root].l >= l && tree[root].r <= r){
		ans += tree[root].w;
		return;
	}
	if(tree[root].mark) 
		push_down(root);
	
	int mid = (tree[root].l + tree[root].r)/2;
	if(l<=mid) check_section(root*2, l, r, ans);
	if(mid<r) check_section(root*2+1, l, r, ans);
} 

6:单点查询

查询 x 的位置

void check_point(int root, int &ans, int x){
	if(tree[root].l == tree[root].r){
		ans = tree[root].w;
		return ;
	}
	if(tree[root].mark) push_down(root);
	int mid = (tree[root].l + tree[root].r)/2;
	if(x<=mid)
		check_point(root*2, ans, x);
	else
		check_point(root*2+1, ans, x);
}

二:经典例题

https://vjudge.net/article/752

例1:HDU 1698 (模板题):区间修改

​ Just a Hook

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

img

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

Output

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.

AC代码:

#include <iostream>
#define maxn 110000
using namespace std;

int n,m,t,cnt;
struct node{
	int l,r,mark,weight;
}tree[maxn*4+10];

void init(){
	for(int i=0;i<maxn*4+5;i++){
		tree[i].l = tree[i].r = tree[i].mark = tree[i].weight = 0;
	}
}

void build(int root, int l, int r){
	tree[root].l = l;
	tree[root].r = r;
	if(l==r){
		tree[root].weight = 1;
		return;
	}
	int mid = (l+r)/2;
	build(root*2, l, mid);
	build(root*2+1, mid+1, r);
	tree[root].weight = tree[root*2].weight + tree[root*2+1].weight;
}

void push_down(int root){
	int marks = tree[root].mark;
	tree[root*2].mark = marks;
	tree[root*2+1].mark = marks;
	tree[root*2].weight = marks * (tree[root*2].r - tree[root*2].l +1);
	tree[root*2+1].weight = marks * (tree[root*2+1].r - tree[root*2+1].l +1);
	tree[root].mark = 0;
}

void change(int root, int l, int r, int x){
	if(tree[root].l >= l && tree[root].r <= r){
		tree[root].mark = x;
		tree[root].weight = x * (tree[root].r - tree[root].l + 1);
		return ;
	}
	
	if(tree[root].mark)
		push_down(root);
	int mid = (tree[root].l + tree[root].r)/2;
	if(l <= mid) change(root*2, l, r, x);
	if(mid < r) change(root*2+1, l, r, x);
	tree[root].weight = tree[root*2].weight + tree[root*2+1].weight;
}

int main(){
	scanf("%d",&t);
	while(t--){
		init();
		scanf("%d%d",&n,&m);
		build(1,1,n);
		for(int i=0;i<m;i++){
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			change(1,a,b,c);
		}
		printf("Case %d: The total value of the hook is %d.\n",++cnt,tree[1].weight);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值