hdu 1698 Just a Hook(区间修改)

探讨了在DotA游戏中,Pudge的钩子如何通过更改金属棒的类型来增加其总价值,涉及区间更新和查询的数据结构问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

题意:
给你一个线段,改变该线段上区间的权值大小,求修改后该线段的权值大小;

一道模板题,被我弄成这样,也是醉了

#include<bits/stdc++.h>
using namespace std;

const int N = 100010;

struct node
{
    int l, r, w, mark;
}tree[N<<2];

void build(int x, int l, int r)
{
    tree[x].l = l; tree[x].r = r; tree[x].w = tree[x].r - tree[x].l + 1; tree[x].mark = 0;
    if(l == r){
        return;
    }
    int m = (l+r)>>1;
    build(x<<1, l, m);
    build(x<<1|1, m+1, r);
    //tree[x].w = tree[x<<1].w + tree[x<<1|1].w;
}

void down(int x)
{
    tree[x<<1].mark = tree[x].mark;
    tree[x<<1|1].mark = tree[x].mark;
    tree[x<<1].w = (tree[x<<1].r - tree[x<<1].l + 1) * tree[x].mark;
    tree[x<<1|1].w = (tree[x<<1|1].r - tree[x<<1|1].l + 1) * tree[x].mark;
    tree[x].mark = 0;
}

void change_interval(int x, int l, int r, int s)
{
    if(tree[x].l == l && tree[x].r == r){
        tree[x].w = s * (tree[x].r - tree[x].l + 1);
        tree[x].mark = s;
        //cout << tree[x].w << endl;
        return;
    }
    if(tree[x].mark)    down(x);
    int m = (tree[x].l + tree[x].r)>>1;
    if(m >= r)  change_interval(x<<1, l, r, s);
    else if(m < l) change_interval(x<<1|1, l, r, s);
    else{
        change_interval(x<<1, l, m, s);
        change_interval(x<<1|1, m+1, r, s);
    }
    tree[x].w = tree[x<<1].w + tree[x<<1|1].w;
}

int main()
{
    int t, n, q, l, r, s, cas = 1;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        build(1, 1, n);
        scanf("%d", &q);
        while(q--){
            scanf("%d%d%d", &l, &r, &s);
            change_interval(1, l, r, s);
        }
        printf("Case %d: The total value of the hook is %d.\n", cas++, tree[1].w);
//        for(int i = 1; i <= 31; i++){
//            printf("%d\n",tree[i].w);
//        }
    }
    return 0;
}
HDU 2034 是一道经典的 A-B Problem 题目,通常涉及简单的数学运算或者字符串处理逻辑。以下是对此类问题的分析以及可能的解决方法。 ### HDU 2034 的题目概述 该题目要求计算两个数之间的差值 \(A - B\) 并输出结果。需要注意的是,输入数据可能存在多种情况,因此程序需要能够适应不同的边界条件和特殊情况[^1]。 #### 输入描述 - 多组测试数据。 - 每组测试数据包含两行,分别表示整数 \(A\) 和 \(B\)。 #### 输出描述 对于每组测试数据,输出一行表示 \(A - B\) 的结果。 --- ### 解决方案 此类问题的核心在于正确读取多组输入并执行减法操作。以下是实现此功能的一种常见方式: ```python while True: try: a = int(input()) b = int(input()) print(a - b) except EOFError: break ``` 上述代码片段通过循环不断接收输入直到遇到文件结束符 (EOF),适用于批量处理多组测试数据的情况。 --- ### 特殊考虑事项 尽管基本思路简单明了,在实际编码过程中仍需注意以下几点: 1. **大数值支持**:如果题目中的 \(A\) 或 \(B\) 可能非常大,则应选用可以容纳高精度的数据类型来存储这些变量。 2. **负数处理**:当 \(B>A\) 导致结果为负时,确保程序不会因符号错误而失效。 3. **异常捕获**:为了防止运行期间由于非法字符或其他意外状况引发崩溃,建议加入必要的错误检测机制。 --- ### 示例解释 假设给定如下样例输入: ``` 5 3 7 2 ``` 按照以上算法流程依次完成各步操作后得到的结果应当分别为 `2` 和 `5`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值