Just a Hook(HDU-1698线段树代码讲解很详细)

Problem Description
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.

Just a Hook(HDU-1698线段树代码讲解很详细)_第1张图片

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.
题意分析:有n根金属棒,m次操作,每次操作将x到y这个区间内的所有值改为z,所有金属棒的初始值为1,问最后m次操作之后所有金属棒的总值。
解题思路:线段树中将一段区间的所有值改为同一个值,之后求给定区间的值的和(本题是求整个区间的所有值的和)模板题。
AC代码如下:

#include
#include
#include
using namespace std;
#define lson rt<<1//左子树
#define rson (rt<<1|1)//右子树
int sum[500000];//存储一段区间的和
int add[500000];
void pushup(int rt){//状态合并,用左右子区间所对应的值更新当前区间的值
	sum[rt]=sum[lson]+sum[rson];
}
void build(int l,int r,int rt){//初始化,建树
	int mid=(l+r)>>1;//计算左右子区间的边界
	if(l==r){//当l=r时,代表递归到了叶子结点
		sum[rt]=1;//将值初始化为1
		return;
	}
	build(l,mid,lson);//递归到左子区间
	build(mid+1,r,rson);//递归到右子区间
	pushup(rt); //状态合并,用左右子区间所对应的值更新当前区间的值
}
void push_down(int rt,int m){//下传操作,将父区间点的延迟标记,下传给子区间
	if(add[rt]){//判断子区间是否需要更新
		add[lson]=add[rt];//左区间延迟标记更新
		add[rson]=add[rt];//右区间延迟标记更新
		sum[lson]=(m-(m>>1))*add[rt];//左区间区间和更新
		sum[rson]=(m>>1)*add[rt];//右区间区间和更新
		add[rt]=0;//清空延迟标记
	}
}
/*//单点修改这个模板是将给定的x改为某一个y,本题是将一个区间内的所有值都改为相同的一个值z
//参数:当前结点的编号rt,需要修改的编号x,需要修改的数值num
void update(int rt,int x,int num,int l,int r)
{
    if(l==r && l==x)//判断是否为需要修改的节点
    {
        sum[rt]=num;//修改值
        return;
    }
    int mid=(l+r)>>1;//计算左右子区间的边界
    if(x<=mid) update(lson,x,num,l,mid);//递归到左子区间
    else update(rson,x,num,mid+1,r);//递归到右子区间
    push_up(rt);//状态合并,用左右子区间所对应的值更新当前区间的值
}
 */
 //区间值的修改
void update(int rt,int L,int R,int z,int l,int r){//判断该区间是否在[L,R]区间内
	if(L<=l&&R>=r){//判断该区间是否在[L,R]区间内
		sum[rt]=(r-l+1)*z;//更新区间和的值,加一的目的就是当为叶子节点也就是l与r相等的时候则将这个值改为z即可
		add[rt]=z;//更新延迟标记
		return;
	}
	push_down(rt,r-l+1);//下传操作,将父区间点的延迟标记,下传给子区间
	int mid=(l+r)>>1;
	if(L<=mid) update(lson,L,R,z,l,mid);
	if(R>mid) update(rson,L,R,z,mid+1,r);
	pushup(rt);
} 
long long query(int rt,int L,int R,int l,int r){
	if(L<=l && R>=r) return sum[rt];
	push_down(rt,r-l+1);
	int mid=(l+r)>>1;
	long long sum1=0;
	if(L<=mid) sum1+=query(lson,L,R,l,mid);//当查找区间在当前区间的左子区间时,递归到左子区间
	if(R>mid) sum1+=query(rson,L,R,mid+1,r);//当查找区间在当前区间的右子区间时,递归到右子区间
	return sum1;
}
int main(){
	int N,M,x,y,z,T,k=0;
	scanf("%d",&T);
	while(T--){//测试用例的个数
		memset(add,0,sizeof(add));
		memset(sum,0,sizeof(sum)); 
		scanf("%d%d",&N,&M);
		build(1,N,1);//建树
		for(int i=0;i<M;i++){
			scanf("%d%d%d",&x,&y,&z);
			update(1,x,y,z,1,N);
		}
		printf("Case %d: The total value of the hook is %lld.\n",++k,query(1,1,N,1,N));
	}
	
}

你可能感兴趣的:(线段树)