HDU 题目1698 Just a Hook(线段树)


Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12674    Accepted Submission(s): 6309


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.



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.
 


分析:看到题目首先想到了线段树,(因为题目是区间更新)区间更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候。延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当前区间已经更新了
首先 区间  [1,n] 建立线段树,(题目例子 n==10) 如下图;




然后依次更新区间,分3中情况,向左,向右,分开向左向右,

在修改和查询的时候,如果我们到了一个结点p,并且需要分开决定考虑其子结点(不能完全覆盖),那么我们就要看看结点p有没有标记,如果有,就要按照标记修改其子结点的信息,并且给子结点都标上相同的标记,同时消掉p的标记;

 进行更新操作

1 5 2

区间更新后的树如下:


 进行更新操作

5 9 3

区间更新后的树如下


假如 再 进行更新操作

6 10 1

区间更新后的树如下



#define N 100001
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;


typedef struct TREE
{
    int lside,rside,value;
}TREE;

TREE tree[N*4];//线段树占空间约为结点个数的 4 倍
int sum,temp;

void build_tree(int l,int r,int i)
{
    tree[i].lside = l;
    tree[i].rside = r;
    tree[i].value = 1;//cuperous
    if(tree[i].lside == tree[i].rside)  return;
    int mid = (tree[i].lside + tree[i].rside)>>1; 
    build_tree(l,mid,i<<1); //i*2
    build_tree(mid+1,r,i<<1|1); //i*2+1
}

void change(int x,int y,int z,int i)
{
//    printf("x=%d,y=%d,i=%d,tl=%d,tr=%d,v=%d\n",x,y,i,tree[i].lside,tree[i].rside,tree[i].value);
    if(x<= tree[i].lside && y>=tree[i].rside)//正好与跟新区间相同
    {
        tree[i].value = z;
        return ;
    }
    if(tree[i].value>0)//不能完全覆盖,需要将下面的都左右子树 都先覆盖为当前结点的标志,
    {
    	tree[i<<1].value =  tree[i].value;
    	tree[i<<1|1].value = tree[i].value;
    	tree[i].value = 0;
    }
    
    int mid = (tree[i].lside + tree[i].rside)>>1;
    if(y<=mid) change(x,y,z,i<<1);//向左
    else if( x>mid ) change(x,y,z,i<<1|1 );//向右
    else//分开的
    {
        change(x,mid,z,i<<1);
        change(mid+1,y,z,i<<1|1);
    }
}
void count(int i)
{
	if(tree[i].value>0)
	{
		sum += tree[i].value * (tree[i].rside - tree[i].lside + 1);//区间的value * 区间的长度 
		return;
	}
	count(i<<1);
	count(i<<1|1);
    
}
int main()
{
    int t,i,j,n,q,x,y,z;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&n);
        build_tree(1,n,1);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d%d",&x,&y,&z);
            change(x,y,z,1);
    //            for(j=1;j<40;j++)
    //    printf("j=%d,l=%d,r=%d,v=%d\n",j,tree[j].lside,tree[j].rside,tree[j].value);
        }
        sum = 0;
        count(1);
        printf("Case %d: The total value of the hook is %d.\n",i,sum);
    }
    return 0;
}

/*
7
10
3
1 5 2
5 9 3
6 10 1


*/