HDU 1689 Just a Hook (线段树区间更新+求和)

Just a Hook

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

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.

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.

思路

初始所有节点值为1 , 更新[l,r]为2 或 3,再求区间和,涉及懒标记,直接套模板= =

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn =  1e5+10  ;
const int inf = 0x3f3f3f3f;

struct node{
    int l,r;
    int add;
    int sum;
}tree[maxn<<2];

int kase=0;
int n,m,t;
int a,b,c;
int val = 1;
int ans = 0;
void pushup(int k)
{
    tree[k].sum = tree[k<<1].sum+tree[k<<1|1].sum;
}
void pushdown(int k)
{
    if(tree[k].add)
    {
        tree[k<<1].sum = (tree[k<<1].r-tree[k<<1].l+1)*tree[k].add;
        tree[k<<1|1].sum = (tree[k<<1|1].r-tree[k<<1|1].l+1)*tree[k].add;

        tree[k<<1].add = tree[k].add;
        tree[k<<1|1].add = tree[k].add;

        tree[k].add = 0;
    }
}
void build(int l,int r,int k)
{
    tree[k].l = l;  tree[k].r = r;  tree[k].add = 0;//刚开始一定要清0
    if(l == r){  tree[k].sum=1;  return ;  }
    int mid = (l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
    pushup(k);
}
void updata(int k)
{
    if(a <= tree[k].l && b >= tree[k].r)
    {
        tree[k].sum = (tree[k].r-tree[k].l+1)*val;
        tree[k].add = val;
        return ;
    }
    pushdown(k);
    int mid = (tree[k].l+tree[k].r)>>1;
    if(a<=mid){  updata(k<<1); }
    if(b>mid){  updata(k<<1|1);  }
    pushup(k);
}
void query(int k)
{
    if(a <= tree[k].l && b >= tree[k].r)
    {
      ans +=tree[k].sum ;
      return ;
    }
    pushdown(k);
    int mid = (tree[k].l+tree[k].r)>>1;
    if(a <= mid){ query(k<<1);}
    if(b > mid){ query(k<<1|1);}
}
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d",&n);
    scanf("%d",&m);
    build(1,n,1);
    while(m--)
    {
      scanf("%d%d%d",&a,&b,&val);
      updata(1);
    }
    ans=0;a=1;b=n;//这里需初始化
    query(1);
    printf("Case %d: The total value of the hook is %d.\n",++kase,ans);
  }
}

转载于:https://www.cnblogs.com/llke/p/10780588.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值