(HDU 1698) Just a Hook(线段树)-附通用模板

本文介绍了一个基于线段树的数据结构算法问题,该问题是关于DotA游戏中Pudge的钩子价值计算。通过一系列操作改变钩子中金属棒的类型,并最终计算钩子的总价值。

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

Just a Hook

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


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
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.
 
 
线段树的模板题,因为以前线段树的模板总是有各种问题,今天重新写了一发,顺带交了这道题
线段树模板:
#define maxn 100010

struct node
{
    int l,r;
    int k;//标记值
    int m;//存储值
};

node tree[maxn];

void build(int n ,int l,int r)
{
    tree[n].l=l;
    tree[n].r=r;
    if(l==r)
    {

        tree[n].m=0;//视情况
        return;
    }
    int mid=(l+r)/2;
    build(n*2,l,mid);
    build(n*2+1,mid+1,r);
    tree[n].m=tree[n*2].m+tree[n*2+1].m;/看线段树种类
}

void pushup(int n)
{
    tree[n].m=tree[n*2].m+tree[n*2+1].m;////看线段树种类
}

void pushdown(int n,int m)
{
    if(tree[n].k)
    {
        tree[n*2].k+=tree[n].k;
        tree[n*2+1].k+=tree[n].k;

        tree[n*2].m+=tree[n].k*(m-m/2);////看线段树种类
        tree[n*2+1].m+=tree[2*n+1].k*(m/2);////看线段树种类

        tree[n].k=0;//更新后还原
    }
}

void update(int n,int l,int r,int c)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        if(l!=r)
            tree[n].k+=c;/看线段树种类
        tree[n].m+=c*(r-l+1);
        return;
    }
    if(l==r)
        return ;
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;
    if(r<=mid)
        update(n*2,l,r,c);
    else if(l>=mid+1)
        update(n*2+2,l,r,c);
    else
    {
        update(n*2,l,mid,c);
        update(n*2+1,mid+1,r,c);
    }
    pushup(n);
}

int query(int n,int l,int r)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        return tree[n].m;
    }
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;

    int res=0;
    if(r<=mid)
        res+=query(n*2,l,r);
    else if(l>=mid+1)
        res+=query(n*2+1,l,r);
    else
    {
        res+=query(n*2,l,mid);////看线段树种类
        res+=query(n*2+1,mid+1,r);////看线段树种类
    }
    return res;

}

 

 

 

AC代码:

 

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define maxn 100010

using namespace std;

struct node
{
    int l,r;
    int k;//标记值
    int m;//存储值
};

node tree[maxn*4];

void pushup(int n)
{
    tree[n].m=tree[n*2].m+tree[n*2+1].m;////看线段树种类
}

void build(int n ,int l,int r)
{
    tree[n].l=l;
    tree[n].r=r;
    tree[n].k=0;
    if(l==r)
    {
        tree[n].m=1;//视情况
        return;
    }
    int mid=(l+r)/2;
    build(n*2,l,mid);
    build(n*2+1,mid+1,r);
    pushup(n);
}



void pushdown(int n,int m)
{
    if(tree[n].k)
    {
        tree[n*2].k=tree[n].k;
        tree[n*2+1].k=tree[n].k;

        tree[n*2].m=tree[n].k*(m-m/2);////看线段树种类
        tree[n*2+1].m=tree[2*n+1].k*(m/2);////看线段树种类

        tree[n].k=0;//更新后还原
    }
}

void update(int n,int l,int r,int c)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        if(l!=r)
            tree[n].k=c;/看线段树种类
        tree[n].m=c*(r-l+1);
        return;
    }
    if(tree[n].l==tree[n].r)
        return ;
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;
    if(r<=mid)
        update(n*2,l,r,c);
    else if(l>=mid+1)
        update(n*2+1,l,r,c);
    else
    {
        update(n*2,l,mid,c);
        update(n*2+1,mid+1,r,c);
    }
    pushup(n);
}

int query(int n,int l,int r)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        return tree[n].m;
    }
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;

    int res=0;
    if(r<=mid)
        res+=query(n*2,l,r);
    else if(l>=mid+1)
        res+=query(n*2+1,l,r);
    else
    {
        res+=query(n*2,l,mid);
        res+=query(n*2+1,mid+1,r);
    }
    return res;

}

int main()
{
    int t;
    scanf("%d",&t);
    for(int kase=1;kase<=t;kase++)
    {
        int n;
        scanf("%d",&n);
        build(1,1,n);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(1,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",kase,tree[1].m);
    }
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/brotherHaiNo1/p/8496598.html

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、付费专栏及课程。

余额充值