线段树--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.



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.

题目就是说给出样例组数,给出一段区间,然后给出操作数,每次操作给出l,r一段区间然后增加v值。求最后的和。

解题思路:这个题很明显的就是线段树区间更新的问题,可以直接求和,不用再去记录每个地方的值然后最后再求和。刚开始TLE了两发,那个时候还没想到用区间更新,准确的说还不太会用。所以想用单点更新但是一想肯定花的时间特别多。。。然后学习了区间更新,感觉用的还不够熟练,还需要加强一些练习。

区间更新里用到了延迟标记,对于延迟标记也只是简单的理解了。感觉还是有点模糊。。。还要继续看资料。

这个题是求和,区间更新还可以最后求最小值最大值都可以,以后遇到题目再进行总结。

源代码如下:

//#include <iostream>
#include <string>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAX 1000000
using namespace std;
struct Tree{
    int v;
    //int L,R;
    int flag;
}tree[MAX];
int a[MAX];
void build (int id,int left,int right){
    tree[id].flag = 0;
    //tree[id].L = left;
    //tree[id].R = right;
    if (left == right){
        tree[id].v = 1;
    }
    else{
        int mid = (left + right) / 2;
        build(id*2,left,mid);
        build(id*2+1,mid+1,right);
        tree[id].v = tree[id*2].v+tree[id*2+1].v;
    }
}
void pushdown(int id,int l,int r)//将延迟标记向孩子节点传递
{
    if (tree[id].flag != 0){
        int mid = (l + r)/2;
        tree[id*2].flag = tree[id].flag;
        tree[id*2+1].flag = tree[id].flag;
        tree[id*2].v = (mid - l + 1)*tree[id].flag;
        tree[id*2+1].v = (r - mid) * tree[id].flag;
        tree[id].flag = 0;//清除该节点的标记。
    }
}/*
int getsum(int id,int l,int r)
{
    if (tree[id].L == l && tree[id].R == r)
        return tree[id].v;
    else{
        int mid = (tree[id].L + tree[id].R)/2;
        if (r <= mid)
            return getsum(id * 2,l,r);
        else if (l > mid)
            return getsum(id*2+1,l,r);
        else
            return getsum(id*2,l,mid) + getsum(id*2+1,mid+1,r);
    }
}*/
void update(int id,int l,int r,int L,int R,int v1){//l,r是要修改的区间,L,R是整个区间。
    if (L > r || R < l)
        return;
    if (l <= L && r >= R){
        tree[id].flag = v1;
        tree[id].v = (R-L+1)*v1;
        return ;
    }
    pushdown(id,L,R);
    int mid = (L + R)/2;
    update(id*2+1,l,r,mid+1,R,v1);
    update(id*2,l,r,L,mid,v1);
    tree[id].v = tree[id*2].v + tree[id*2+1].v;
}
int main()
{
    int n,c;
    scanf("%d",&n);
    int p = 1;
    while (n--){
        scanf("%d",&c);
        build(1,1,c);
        int m;
        scanf("%d",&m);
        for (int j = 1; j <= m; j++){
            int l,r;
            scanf("%d%d",&l,&r);
            int v;
            scanf("%d",&v);
            update(1,l,r,1,c,v);
        }
        //for (int i = 1; i <= c; i++)
          //  printf("%d ",tree[i].v);
        printf("Case %d: The total value of the hook is %d.\n",p,tree[1].v);//因为求和最后输出根节点的值就可以了。
        p++;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值