hdu1698(线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

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


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.
Yu 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.


思路:一道线段树区间更新上午板子题,主要是测试一下板子好不好使~

#include   
#include   
#include   
#include   
#include   
#define MAXN 100010  
#define inf 0x3f3f3f3f  
  
using namespace std;  
  
struct node{  
    int l,r;//区间[l,r]  
    int add;//区间的延时标记  
    int sum;//区间和  
    int mx; //区间最大值  
    int mn; //区间最小值  
}tree[MAXN<<2];//一定要开到4倍多的空间  
int cnt=0;
void pushup(int index){  
    tree[index].sum = tree[index<<1].sum+tree[index<<1|1].sum;  
    tree[index].mx = max(tree[index<<1].mx,tree[index<<1|1].mx);  
    tree[index].mn = min(tree[index<<1].mn,tree[index<<1|1].mn);  
}  
void pushdown(int index){  
    //说明该区间之前更新过  
    //要想更新该区间下面的子区间,就要把上次更新该区间的值向下更新  
    if(tree[index].add){  
        //替换原来的值  
        
        tree[index<<1].sum = (tree[index<<1].r-tree[index<<1].l+1)*tree[index].add; 
        tree[index<<1|1].sum = (tree[index<<1|1].r-tree[index<<1|1].l+1)*tree[index].add; 
        tree[index<<1].mx = tree[index].add; 
        tree[index<<1|1].mx = tree[index].add; 
        tree[index<<1].mn = tree[index].add; 
        tree[index<<1|1].mn = tree[index].add; 
        tree[index<<1].add = tree[index].add; 
        tree[index<<1|1].add = tree[index].add; 
        tree[index].add = 0;
        
        //在原来的值的基础上加上val  
          
//        tree[index<<1].sum += (tree[index<<1].r-tree[index<<1].l+1)*tree[index].add;  
//        tree[index<<1|1].sum +=(tree[index<<1|1].r-tree[index<<1|1].l+1)*tree[index].add;  
//        tree[index<<1].mx += tree[index].add;  
//        tree[index<<1|1].mx += tree[index].add;  
//        tree[index<<1].mn += tree[index].add;  
//        tree[index<<1|1].mn += tree[index].add;  
//        tree[index<<1].add += tree[index].add;  
//        tree[index<<1|1].add += tree[index].add;  
//        tree[index].add = 0;  
  
    }  
}  
void build(int l,int r,int index){  
    tree[index].l = l;  
    tree[index].r = r;  
    tree[index].add = 0;//刚开始一定要清0  
    if(l == r){  
        tree[index].sum=1;  
        tree[index].mn = tree[index].mx = tree[index].sum;  
        return ;  
    }  
    int mid = (l+r)>>1;  
    build(l,mid,index<<1);  
    build(mid+1,r,index<<1|1);  
    pushup(index);  
}  
void updata(int l,int r,int index,int val){  
    if(l <= tree[index].l && r >= tree[index].r){  
        //把原来的值替换成val,因为该区间有tree[index].r-tree[index].l+1 
       // 个数,所以区间和 以及 最值为: 
        tree[index].sum = (tree[index].r-tree[index].l+1)*val; 
        tree[index].mn = val; 
        tree[index].mx = val; 
        tree[index].add = val;
        //在原来的值的基础上加上val,因为该区间有tree[index].r-tree[index].l+1  
        //个数,所以区间和 以及 最值为:  
//        tree[index].sum += (tree[index].r-tree[index].l+1)*val;  
//        tree[index].mn += val;  
//        tree[index].mx += val;  
//        tree[index].add += val;//延时标记  
  
        return ;  
    }  
    pushdown(index);  
    int mid = (tree[index].l+tree[index].r)>>1;  
    if(l <= mid){  
        updata(l,r,index<<1,val);  
    }  
    if(r > mid){  
        updata(l,r,index<<1|1,val);  
    }  
    pushup(index);  
}  
int query(int l,int r,int index){  
    if(l <= tree[index].l && r >= tree[index].r){  
        return tree[index].sum;  
        //return tree[index].mx;  
        //return tree[index].mn;  
    }  
    pushdown(index);  
    int mid = (tree[index].l+tree[index].r)>>1;  
    int ans = 0;  
    int Max = 0;  
    int Min = inf;  
    if(l <= mid){  
        ans += query(l,r,index<<1);  
        Max = max(query(l,r,index<<1),Max);  
        Min = min(query(l,r,index<<1),Min);  
    }  
    if(r > mid){  
        ans += query(l,r,index<<1|1);  
        Max = max(query(l,r,index<<1|1),Max);  
        Min = min(query(l,r,index<<1|1),Min);  
    }  
    return ans;  
    //return Max;  
    //return Min;  
}  
int main()  
{  
    int n,m,t,t1,t2,t3;
	scanf("%d",&t);  
    while(t--)
	{
	    cnt++;
		scanf("%d",&n);
	    scanf("%d",&m);
        build(1,n,1);
		updata(1,n,1,1);  
        while(m--)
		{  
            scanf("%d%d%d",&t1,&t2,&t3);  
            updata(t1,t2,1,t3);
        }
		printf("Case %d: The total value of the hook is %d.\n",cnt,query(1,n,1));  
    }  
    return 0;  
}

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