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. 



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.
题意
给一组棍子染色,不同的颜色有不同的值,
执行一系列的区间染色后,问这组棍子的总值是多少。

C++版本一

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
const int N = 1e5+10;
using namespace std;
int t,n,q;
long long tree[N<<2];
long long lazy[N<<2];
void PushUp(int rt){
    tree[rt] = tree[rt*2] + tree[rt*2+1]; ///区间和的更新操作
}
void PushDown(int rt, int llen, int rlen){
    if(lazy[rt]){
        lazy[rt*2+1] =lazy[rt*2] = lazy[rt];
        tree[rt*2] = lazy[rt] *llen ;
        tree[rt*2+1] = lazy[rt] * rlen;
        lazy[rt] = 0;
    }
}
void Build(int l,int r,int rt){
    // l,r 代表的是这个区间内的左端点 和 右端点, rt代表的是 [l,r] 这个区间内的值是存在哪一个位置的。
    if(l==r){
         tree[rt] = 1;
        return;
    }
    int m=(l+r)/2;// 对于区间区分,我们一般将m点划入左半边区间
    Build(l,m,rt*2);
    Build(m+1,r,rt*2+1);
    PushUp(rt); // PushUp 函数是通过2个子节点来更新现在这个节点的状态, 对于不同的要求需要不同的写法。

}


void Updata(int l,int r,int rt,int L,int R,int C){// l,r,rt 与前面的定义一样, L代表的是要更新区间的位置,C代表的是修改后的值
    if(L <= l && r <= R){// 这里不能写成 if(l == L) 因为有可能左端点恰好是要更新的位置, 但是还有右端点, 我们直接更新的只有区间 [L,L]。
        tree[rt] = (long long)C*(r-l+1);
        lazy[rt] = C;

        return ;
    }
    int m=(l+r)/2;
    PushDown(rt, m-l+1, r-m);
    if(L<=m) Updata(l,m,rt*2,L,R,C);//要更新的区间在左边部分,所以往左边走,更新左边
    if(m < R) Updata(m+1,r,rt*2+1,L,R,C);//要更新的区间在右边部分, 往右边走,更新右边
    PushUp(rt);//更新完子节点之后需要更新现在的位置, 需要保证线段树的性质。
}
int main()
{
    int cnt=0;
    scanf("%d",&t);
    while(t--){
        memset(tree,0,sizeof(tree));
        memset(lazy,0,sizeof(lazy));
        scanf("%d",&n);
        Build(1,n,1);
        int x,y,z;
        scanf("%d",&q);
        while(q--){

            scanf("%d%d%d",&x,&y,&z);
            Updata(1,n,1,x,y,z);

        }
        printf("Case %d: The total value of the hook is %lld.\n",++cnt,tree[1]);

    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

题意:有t组测试数据,n为钩子长度(1<=n<=100000),m为操作的次数。初始时,每个钩子的价值为1,操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1代表铜,2银,3金)。

       使用了延迟标记type,表示当前区间的左右子儿子的价值为type,在更新到当前区间时,同时更新当前区间的valu,即type*当前区间的长度。
 

/*代码风格更新后*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
 
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=100005;
 
struct node
{
	int lft,rht;
	int valu,type;
	int mid(){return MID(lft,rht);}
	void fun(int tmp)
	{
		type=tmp;
		valu=(rht-lft+1)*type;
	}
};
struct Segtree
{
	node tree[N*4];
	void build(int lft,int rht,int ind)
	{
		tree[ind].lft=lft;	tree[ind].rht=rht;
		tree[ind].valu=rht-lft+1;
		tree[ind].type=1;
		if(lft!=rht)
		{
			int mid=tree[ind].mid();
			build(lft,mid,LL(ind));
			build(mid+1,rht,RR(ind));
		}
	}
	void updata(int st,int ed,int ind,int type)
	{
		int lft=tree[ind].lft,rht=tree[ind].rht;
		if(st<=lft&&rht<=ed) tree[ind].fun(type);
		else 
		{
			if(tree[ind].type)
			{
				tree[LL(ind)].fun(tree[ind].type);
				tree[RR(ind)].fun(tree[ind].type);
				tree[ind].type=0;
			}
			int mid=tree[ind].mid();
			if(st<=mid) updata(st,ed,LL(ind),type);
			if(ed>mid)  updata(st,ed,RR(ind),type);
			tree[ind].valu=tree[LL(ind)].valu+tree[RR(ind)].valu;
		}
	}
}seg;
int main()
{
	int t,t_cnt=0;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,a,b,c;
		scanf("%d%d",&n,&m);
		seg.build(1,n,1);
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			seg.updata(a,b,1,c);
		}
		printf("Case %d: The total value of the hook is %d.\n",++t_cnt,seg.tree[1].valu);
	}
	return 0;
}

C++版本三

#include <stdio.h>
#include <algorithm>
using namespace std;
 
typedef struct
{
    int l,r,num,lazy;
}Tree;
 
Tree tree[500005];
 
void Build(int t,int l,int r)
{
    int mid;
    tree[t].l=l;
    tree[t].r=r;
    tree[t].num=r-l+1;
    tree[t].lazy=1;
    if (l==r) return;
    mid=(l+r)/2;
    Build(2*t+1,l,mid);
    Build(2*t+2,mid+1,r);
}
 
void Add(int t,int x,int y,int c)
{
    int mid,l,r;
    l=tree[t].l;
    r=tree[t].r;
    if (l==x && r==y)
    {
        tree[t].lazy=1;
        tree[t].num=(r-l+1)*c;
        return;
    }
    mid=(l+r)/2;
    if (tree[t].lazy==1)
    {
        tree[t].lazy=0;
        Add(2*t+1,l,mid,tree[t].num/(r-l+1));
        Add(2*t+2,mid+1,r,tree[t].num/(r-l+1));
    }
    if (x<=mid) Add(2*t+1,x,min(mid,y),c);
    if (y>mid) Add(2*t+2,max(mid+1,x),y,c);
    tree[t].num=tree[2*t+1].num+tree[2*t+2].num;
}
 
int Find(int t)
{
    if (tree[t].lazy==1) return tree[t].num;
    return Find(2*t+1)+Find(2*t+2);
}
 
int main()
{
    int i,j,n,T,m,x,y,c,cnt=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        Build(0,0,n-1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&c);
            Add(0,x-1,y-1,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",cnt++,Find(0));
    }
    return 0;
}

C++版本四

题意:在dota游戏中,pudge的钩子是他最厉害的武器,众多的钩子组成了连续的一列,有铜钩子,银钩子,金钩子,分别用1,2,3表示,有两种操作,一种要求你把某一区间的钩子统统变为另一种钩子,第二种操作询问你某一区间钩子的总和

典型的线段树区间更新,区间求和问题,用了lazy—tag思想,对于初学者来说这是线段树比较难的一个地方,在看我看了一遍又一遍的博客后,终于明白点了

在点更新时,如果我们更新一个点,最多影响到 Lgn个点,但是在区间更新时,有可能影响到全部的点,这时如果一直向下跟新下去就会超时,所以假如更新到某个节点的时候,而这个节点的左右段和要更新的最有端点一致的话,就可以仅更新该节点,并在他的子节点上面(两个)加上一个lazy-tag,不再往子节点更新值。如果以后更新到加lazy-tag标记的结点时,则需要把这个标记的值更新下去,并且在他的子节点跟新lazy-tag标记,并取消这个节点的lazy-tag。

另外注意区间更新与区间增减的区别
 

#include<stdio.h>
const int MAXN=100005;
struct NODE{
	int l,r;
	int sum;
	int lazy,tag;
}segTree[4*MAXN];
void pushup(int num)
{
	segTree[num].sum=segTree[num<<1].sum+segTree[num<<1|1].sum;
}
void build(int num,int l,int r)
{
	segTree[num].l=l;
	segTree[num].r=r;
	segTree[num].lazy=0;
	segTree[num].tag=0;
	if(l==r)
	{
		segTree[num].sum=1;
		return;
	}
	int mid=(l+r)>>1;
	build(num<<1,l,mid);
	build(num<<1|1,mid+1,r);
	pushup(num);
}
void pushdown(int num)
{
	segTree[num<<1].sum=(segTree[num<<1].r-segTree[num<<1].l+1)*segTree[num].tag;
	segTree[num<<1|1].sum=(segTree[num<<1|1].r-segTree[num<<1|1].l+1)*segTree[num].tag;
	//对子节点添加 lazy—tag标记 
	segTree[num<<1].lazy=segTree[num<<1|1].lazy=1;
	segTree[num<<1].tag=segTree[num<<1|1].tag=segTree[num].tag;
	segTree[num].lazy=segTree[num].tag=0;//取消父节点lazy—tag标记 
}
void update(int num,int l,int r,int m)
{
	if(segTree[num].l==l&&segTree[num].r==r)
    {
    	segTree[num].sum=(r-l+1)*m;
    	//添加 lazy—tag标记 
    	segTree[num].lazy=1;
    	segTree[num].tag=m;
    	//直接返回 
    	return;
    }
    if(segTree[num].lazy) pushdown(num);
    int mid=(segTree[num].l+segTree[num].r)>>1;
    if(r<=mid) update(num<<1,l,r,m);
    else if(l>mid) update(num<<1|1,l,r,m);
    else{
    	update(num<<1,l,mid,m);
    	update(num<<1|1,mid+1,r,m);
    }
    pushup(num);
}
int main(void)
{
	int test;
	int N,Q;
	int l,r,m;
	int Case=0;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d",&N);
		build(1,1,N);
		scanf("%d",&Q);
		while(Q--)
		{
			scanf("%d%d%d",&l,&r,&m);
			update(1,l,r,m);
		}
		printf("Case %d: The total value of the hook is %d.\n",++Case,segTree[1].sum);
	}
	return 0;
}