Just a Hook (HDU-1698)

Just a Hook (HDU-1698)

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.

题目翻译:

在DotA游戏中,对于大多数英雄来说,Pudge的肉钩其实是最可怕的东西。鱼钩由几根长度相同的连续金属杆组成。

现在Pudge想在钩子上做一些操作。

让我们将鱼钩上连续的金属棒编号,从1到n。每一次操作,Pudge都可以将连续的金属棒编号,从X到Y,换成铜棒、银棒或金棒。

吊钩的总价值计算为N根金属棒的总价值。更精确地说,每一种木棍的值计算如下:

对于每根铜棒,值是1。

每根银棒的价值是2。

每根金条的价值是3。

Pudge想知道执行操作后钩子的总价值。

你可以认为最初的钩子是由铜棒组成的。

输入

输入由几个测试用例组成。输入的第一行是用例的数量。不超过10例。

对于每种情况,第一行包含一个整数N, 1<=N<=100,000,这是Pudge 's meat hook的木棍数量,第二行包含一个整数Q, 0<=Q<=100,000,这是操作的数量。

接下来的Q行,每一行包含三个整数X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3,这定义了一个操作:将编号从X到Y的木棍换成金属类Z,其中Z=1代表铜类,Z=2代表银类,Z=3代表金类。

输出

对于每种情况,在一行中打印一个数字,表示操作之后钩子的总价值。在示例中使用该格式。

样例输入

1

10

2

1 5 2

5 9 3

样例输出

案例1:钩子的总价值为24。

思路:该题是最典型的线段树区间修改问题, 需要用到所谓的懒惰标记。   听起来挺难的,其实非常简单, 其原理如下:

因为修改很多值, 如果还是按照原来的更新方法, 每个结点更新一次的话,速度实在太慢。 那么能不能一起更新呢?  答案是肯定的。 一个点一个点的更新之所以慢 , 是因为每个被该点影响的点我们都需要更新。   为了能”顺便“更新, 我们在每个结点上多维护一个信息, 表示上次该区间修改的值是多少,然后然后每次向下更新之前将标记更新到儿子结点。

对于线段树, 只要理解好每个结点表示一个不重复不交叉的区间, 就差不多可以理解其更新过程了。
AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define PI 3.1415926
#define inf 0x3f3f3f3f
const int maxn=100010;
using namespace std;
long long Sum[maxn<<2];
long long A[maxn<<2];
void PushUp(int rt)
{
    Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1];
}
void Build(int l,int r,int rt)
{
    A[rt]=0;
    if(r==l)
    {
        Sum[rt]=1;
        return;
    }
    int m=(l+r)>>1;
    Build(l,m,rt<<1);
    Build(m+1,r,rt<<1|1);
    PushUp(rt);
}
void PushDown(int rt,int l,int r)
{
    if(A[rt])
    {
        int m=(l+r)>>1;
        A[rt<<1]=A[rt<<1|1]=A[rt];
        Sum[rt<<1]=(m-l+1)*A[rt];
        Sum[rt<<1|1]=(r-m)*A[rt];
        A[rt]=0;
    }
}
void Update(int x,int y,int k,int l,int r,int rt)
{
    if(x<=l && r<=y)
    {
        A[rt]=k;
        Sum[rt]=k*(r-l+1);
        return;
    }
    int m=(l+r)>>1;
    PushDown(rt,l,r);
    if(x<=m)
        Update(x,y,k,l,m,rt<<1);
    if(m<y)
        Update(x,y,k,m+1,r,rt<<1|1);
    PushUp(rt);
}
int Query(int x,int y,int l,int r,int rt)
{
    if(x<=l && r<=y)
        return Sum[rt];
    PushDown(rt,l,r);
    int num;
    int m=(l+r)>>1;
    if(x<=m)
        num+=Query(x,y,l,m,rt<<1);
    if(m<y)
        num+=Query(x,y,m+1,r,rt<<1|1);
    return num;
}
int main()
{
    int t;
    int n,q;
    int j=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&q);
        int x,y,z;
        Build(1,n,1);
        while(q--)
        {
            scanf("%d%d%d",&x,&y,&z);
            Update(x,y,z,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",++j,Query(1,n,1,n,1));
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值