hdu 1689 Just a Hook

本文介绍了一道关于区间更新的问题,使用线段树进行高效处理。通过懒惰标记避免不必要的叶子节点更新,确保了算法效率。

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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.

【题意】 就是给你N个数,再给你Q条指令,每条指令给X,Y,Z三个值,意思是将[X,Y]区间内的数都替换成Z,最后求出总区间的和。

【解题思路】 一道简单的区间更新,用laz标记,无需更新到叶子结点,不然会超时。因为只需要总区间的和,此题也不用写求和函数,用线段树,写法写着写着就过了。话不多说,上代码!

【AC代码】

#include<stdio.h>
#define fff 100005
using namespace std;
struct node
{
    int l,r,len;
    int laz,sum;
}tr[fff*4];
void pushup(int id)
{
    tr[id].sum=tr[id*2].sum+tr[id*2+1].sum;
}
void pushdown(int id)
{
    if(tr[id].laz)
    {
        tr[id*2].laz=tr[id].laz;
        tr[id*2+1].laz=tr[id].laz;
        tr[id*2].sum=tr[id*2].len*tr[id].laz; //因为是区间替换,所以写=而不写+=
        tr[id*2+1].sum=tr[id*2+1].len*tr[id].laz; 
        tr[id].laz=0;
    }
}
void build(int id,int l,int r)
{
    tr[id].l=l;
    tr[id].r=r;
    tr[id].len=r-l+1;
    tr[id].laz=0;
    if(l==r)
    {
        tr[id].sum=1;
        return;
    }
    int mid=(r+l)/2;
    build(id*2,l,mid);
    build(id*2+1,mid+1,r);
    pushup(id);
}
void update(int id,int l,int r,int x)
{
    if(tr[id].l==l&&tr[id].r==r)
    {
        tr[id].laz=x;
        tr[id].sum=x*tr[id].len;
        return;
    }
    pushdown(id);
    int mid=(tr[id].l+tr[id].r)/2;
    if(l>mid)
        update(id*2+1,l,r,x);
    else if(r<=mid)
        update(id*2,l,r,x);
    else
    {
        update(id*2,l,mid,x);
        update(id*2+1,mid+1,r,x);
    }
    pushup(id);
}
int main()
{
    int t,n,q,a,b,c,cas;
    scanf("%d",&t);
    for(cas=1;cas<=t;cas++)
    {
        scanf("%d%d",&n,&q);
        build(1,1,n);
        while(q--)
        {
            scanf("%d%d%d",&a,&b,&c);
            update(1,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",cas,tr[1].sum);  //只求总和,所以直接用根节点的sum值就行
    }
    return 0;
}

线段树最基础的写法,若有什么改进的地方,还请多多指教。

基于径向基函数神经网络RBFNN的自适应滑模控制学习(Matlab代码实现)内容概要:本文介绍了基于径向基函数神经网络(RBFNN)的自适应滑模控制方法,并提供了相应的Matlab代码实现。该方法结合了RBF神经网络的非线性逼近能力和滑模控制的强鲁棒性,用于解决复杂系统的控制问题,尤其适用于存在不确定性和外部干扰的动态系统。文中详细阐述了控制算法的设计思路、RBFNN的结构与权重更新机制、滑模面的构建以及自适应律的推导过程,并通过Matlab仿真验证了所提方法的有效性和稳定性。此外,文档还列举了大量相关的科研方向和技术应用,涵盖智能优化算法、机器学习、电力系统、路径规划等多个领域,展示了该技术的广泛应用前景。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的研究生、科研人员及工程技术人员,特别是从事智能控制、非线性系统控制及相关领域的研究人员; 使用场景及目标:①学习和掌握RBF神经网络与滑模控制相结合的自适应控制策略设计方法;②应用于电机控制、机器人轨迹跟踪、电力电子系统等存在模型不确定性或外界扰动的实际控制系统中,提升控制精度与鲁棒性; 阅读建议:建议读者结合提供的Matlab代码进行仿真实践,深入理解算法实现细节,同时可参考文中提及的相关技术方向拓展研究思路,注重理论分析与仿真验证相结合。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值