poj 1698 Just a Hook

Just a Hook

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


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.
在Dota游戏中,屠夫的对于大部分英雄来说都是一个很可怕的东西。钩子是由很多相同长度的金属条构成的。



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.
我们先把连续的金属节标记上1到N,对于每一个操作,屠夫可以改变连续从X到Y的金属节,让他们变成铜节,银节,或者金节。
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.铜节的价值是1.
For each silver stick, the value is 2.银节的价值是2.
For each golden stick, the value is 3.金节的价值是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.
输入包含几组数据。输入第一行是数据有多少组。不会比10组数据多。
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.
对于每一组数据,第一行包括一个数据N, 1<=N<=100,000,代表屠夫有多少节钩子,第二行包含一个整数Q,0<=Q<=100,000,代表操作的次数。
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.
对于接下来的Q行,每一行包括三个整数 X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, 代表一个操作:把X到Y的金属换成Z,Z=1表示是铜,Z=2表示是银,Z=3表示是金的。
 

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.
 

Source
 

Recommend
wangye

这是一道线段树段更新的基础题目,用来练习线段树的线段更新。注意一个lazy思想,就是每次更新的时候不更新到底,只更新到那个长度的线段,等到下一次更新的时候,即时更新。

#include <stdio.h>
#include <string.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int maxn = 111111;
int col[maxn << 2];
int sum[maxn << 2];
int n, q;
void PushUp(int rt){
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void PushDown(int rt, int m){
    if (col[rt]){
        col[rt << 1] = col[rt << 1 | 1] = col[rt];
        sum[rt << 1] = (m - (m >> 1)) * col[rt];
        sum[rt << 1 | 1] = (m >> 1) * col[rt];
        col[rt] = 0;
    }    
}

void updata(int L, int R, int x, int l, int r, int rt){
    int m;
    if (L <= l && r <= R){
        col[rt] = x;
        sum[rt] = x * (r - l + 1);
        return;
    }
    PushDown(rt, r - l + 1);
    m = (l + r) >> 1;
    if (L <= m) updata(L, R, x, lson);
    if (R > m) updata(L, R, x, rson);
    PushUp(rt);
}

void build(int l, int r, int rt){
    int m;
    col[rt] = 0;
    sum[rt] = 1;
    if (l == r)
        return;
    m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
int main(void){

    int case_n;
    int i;
    int x, y, z;
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    scanf("%d", &case_n);
    for (i = 1;i <= case_n;i ++){
        scanf("%d%d", &n, &q);
        build(1, n, 1);
        while(q--){
            scanf("%d%d%d", &x, &y, &z);
            updata(x, y, z, 1, n, 1);
        }
        printf("Case %d: The total value of the hook is %d.\n", i, sum[1]);

    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值