【hdu】 Just a Hook (线段树 -成段更新,延迟标记)

本文介绍了一种解决Pudge's Hook问题的高效算法。通过使用线段树结构,实现区间更新与延迟标记,有效计算钩子总价值。具体包括初始化、更新操作及最终查询值的实现。

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

Just a Hook

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 4
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.
 

这题花了不少时间,成段更新,延迟标记!
初始时,令所有tree[i].sum=1,此时,最上面一层即i=1时为可用的点。

每一步更新,不需要更新到底(单点更新),只需要更新tree[i].left==x&&tree[i].right==y即可,此点上面的均为不可用点!
最后查询,只需要找到可用点,(tree[i].right-tree[i].left+1)*tree[i].sum; 此时的sum必为更新后的值!
下面的代码很重要,当程序递归到第i个结点时,如果不为-1,则将它的左右儿子更新,将它置为不可用点,因为程序还要将递归到它的儿子节点。
  1. if(tree[pos].sum!=-1)  
  2.      {  
  3.          tree[pos<<1].sum=tree[pos<<1|1].sum=tree[pos].sum;  
  4.          tree[pos].sum=-1;  
  5.      } 


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct segtree
{
       int left,right;
       int sum;
}tree[400005];
void Build(int pos,int x,int y)
{
     tree[pos].left=x;
     tree[pos].right=y;
     tree[pos].sum=1; 
     if(x==y){return ;}
     int mid=(x+y)>>1;
     Build((pos<<1),x,mid);
     Build((pos<<1|1),mid+1,y);
}
void Update(int pos,int x,int y,int z)
{
     if(tree[pos].sum==z)return; 
     int L=tree[pos].left;
     int R=tree[pos].right;
     int M=(L+R)>>1;
     if(L==x&&R==y) 
     {
         tree[pos].sum=z;
         return ;
     }
     if(tree[pos].sum!=-1)
     {
         tree[pos<<1].sum=tree[pos<<1|1].sum=tree[pos].sum;
         tree[pos].sum=-1;
     }
     if(y<=M) Update((pos<<1),x,y,z);  
     else if(x>M) Update(pos<<1|1,x,y,z);
     else
     { 
          Update((pos<<1),x,M,z); 
          Update(pos<<1|1,M+1,y,z);
     }
}
int Search(int i)
{
    if(tree[i].sum!=-1) return (tree[i].right-tree[i].left+1)*tree[i].sum;
    return Search(2*i)+Search(2*i+1);
}
int n;
int main()
{
    int T,i,j,t;
    scanf("%d",&T);
    for(i=1;i<=T;i++)
    {
              scanf("%d",&n);
              Build(1,1,n);
              scanf("%d",&t);
              int x,y,z;
              while(t--)
              {
                        scanf("%d%d%d",&x,&y,&z);
                        Update(1,x,y,z);
              }
              printf("Case %d: The total value of the hook is %d.\n",i,Search(1));
    }
    return 0;
}
         
                
                


内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值