hdu_1698Just a Hook线段树_区间修改

Just a Hook

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


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.
 
方法一:建议两个线段树,一个保存子叶结点值,另一个保存结点的和,因此求区间和时只需输出根结点即可。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define maxn  100000+10
int a[maxn<<2],sum[maxn<<2];//a数组用于线段树标记,sum用于求和
void Getsum(int cur){
    sum[cur] = sum[cur<<1]+sum[cur<<1|1];
}
void build(int l,int r ,int cur){
    a[cur] = 0;//建树时初始结点都未标记
    //标记的意思是是否需要修改该区间
    if(l == r){
        sum[cur]=1;
//        printf("%d %d %d\n",l,cur,sum[cur]);
        return ;
    }
    int mid =  ( l + r ) >> 1;
    build(l,mid,cur<<1); //建立左子树
    build(mid+1,r,cur<<1|1);//建立右子树
    Getsum(cur);
}
void pushDown(int cur,int len){//维护结点cur,对应区间是cur<<1,cur<<1|1
    if(a[cur]){//本结点有标记才传递。
        a[cur<<1]=a[cur<<1|1]=a[cur];  //标记结点向左右子树传递
        sum[cur<<1]=a[cur]*(len-(len>>1));//左子树区间增加
        sum[cur<<1|1]=a[cur]*(len>>1);//右子树区间段增加
        a[cur]= 0 ;//清除标记
    }
}
void update(int x,int y,int z,int l,int r,int cur){
    if( x <= l && y >= r){
        a[cur]=z;  //改变边界的值
        sum[cur] = z*(r - l + 1);//计算区间和
        return ;
    }
    pushDown(cur,r-l+1);
    int mid = ( l + r )>> 1;
    if(x <= mid ) update(x,y,z,l,mid,cur<<1);
    if(y > mid )  update(x,y,z,mid+1,r,cur<<1|1);
    Getsum(cur);   //递归结束前重新计算本结点的附加信息
}
int main()
{
    int T,n,m,t=1,x,y,z;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&m);
        while(m--){
            scanf("%d %d %d",&x,&y,&z);
            update(x,y,z,1,n,1);
        }
//    printf("%d %d %d %d %d %d %d %d %d %d\n",sum[16],sum[17],sum[9],sum[10],sum[11],sum[24],sum[25],sum[14],sum[15]);
    printf("Case %d: The total value of the hook is %d.\n",t++,sum[1]);
    }
    return 0;
}

方法二:只建立一个线段树,在求区间和时需要调用函数分别累加区间值
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define maxn  100000+10
int a[maxn<<2];

void build(int l,int r ,int cur){
    a[cur] = 0;//建树时初始结点都未标记
    if(l == r){
        a[cur]=1;
        return ;
    }
    int mid =  ( l + r ) >> 1;
    build(l,mid,cur<<1); //建立左子树
    build(mid+1,r,cur<<1|1);//建立右子树
}
void pushDown(int cur){//维护结点cur,对应区间是cur<<1,cur<<1|1
    if(a[cur]){//本结点有标记才传递。
        a[cur<<1]=a[cur<<1|1]=a[cur];
        a[cur]= 0 ;//清除标记
    }
}
void update(int x,int y,int z,int l,int r,int cur){
    if( x <= l && y >= r){
        a[cur] = z;
        return ;
    }
    pushDown(cur);
    int mid = ( l + r )>> 1;
    if(x <= mid ) update(x,y,z,l,mid,cur<<1);
    if(y > mid )  update(x,y,z,mid+1,r,cur<<1|1);
}
int query(int l,int r,int cur){
    if(l == r )
        return a[cur];
    pushDown(cur);
    int mid = (l + r )>>1 ;
    return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);
}
int main()
{
    int T,n,m,t=1,x,y,z;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&m);
        while(m--){
            scanf("%d %d %d",&x,&y,&z);
            update(x,y,z,1,n,1);
        }
    printf("Case %d: The total value of the hook is %d.\n",t++,query(1,n,1));
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值