Just a Hook(树状数组)

Stella981
• 阅读 443

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.

Just a Hook(树状数组)

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.

InputThe 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.
OutputFor 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条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。题解:简单的线段树的区间更新。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<iostream>
  5 #include<algorithm>
  6 using namespace std;
  7 const int MAXN=2e5+10;
  8 typedef long long ll;
  9 #define lson l,m,i<<1
 10 #define rson m+1,r,i<<1|1
 11 typedef struct Node
 12 {
 13     ll l,r;
 14     ll mid()
 15     {
 16         return (l+r)/2.0;
 17     }
 18     ll value;
 19 } Node;
 20 Node node[MAXN<<2];
 21 ll sum[MAXN<<2];//用来记录每个节点的值(区间和)
 22 ll add[MAXN<<2];//延迟标记数组
 23 void push_up(ll i)
 24 {
 25     sum[i]=sum[i<<1]+sum[i<<1|1];//该节点的和等于左儿子加幼儿子的和
 26 }
 27 void Build(ll l,ll r,ll i)
 28 {
 29     node[i].l=l;
 30     node[i].r=r;
 31     node[i].value=0;
 32     sum[i]=0;
 33     add[i]=0;
 34     if(l==r)
 35     {
 36         sum[i]=1;
 37         node[i].value=1;
 38         return ;
 39     }
 40     ll m=node[i].mid();
 41     Build(lson);//建立左子树
 42     Build(rson);//建立右子树
 43     push_up(i);
 44 }
 45 ll M;
 46 void Push_down(ll i,ll len)//延迟标记下压
 47 {
 48     if(add[i])
 49     {
 50         add[i<<1]=add[i];//把该节点的标记赋给他的左儿子  51          add[i<<1|1]=add[i];//把该节点的标记赋给他的右儿子
 52         sum[i<<1]=add[i]*(len-(len>>1));//把左儿子更新
 53         sum[i<<1|1]=add[i]*(len>>1);//把右儿子更新
 54         add[i]=0;//把该节点的延迟标记删除
 55     }
 56 }
 57 
 58 void update(ll l,ll r,ll i,ll v)
 59 {
 60     if(node[i].r==r&&node[i].l==l)
 61     {
 62         add[i]=v;//延迟标记
 63         sum[i]=v*(r-l+1);//给该节点赋值
 64         return;
 65     }
 66     ll m=node[i].mid();
 67     Push_down(i,node[i].r-node[i].l+1);//来判断节点是否有延迟标记,若有则更新
 68     if(r<=m)
 69         update(l,r,i<<1,v);//查询区间在该节点的左子树上 70     else
 71     {
 72         if(l>m)
 73             update(l,r,i<<1|1,v);///查询区间在该节点的右子树上
 74 
 75         else
 76         {
 77             update(l,m,i<<1,v);//查询区间在该节点的两颗树上
 78             update(m+1,r,i<<1|1,v);
 79         }
 80     }
 81     push_up(i);//归的第一步,向上更新
 82 }
 83 int main()
 84 {
 85     ll m,n,a,b,T,c;
 86     ll flag=0;
 87     scanf("%lld",&T);
 88     while(T--)
 89     {
 90         sum[0]=0;
 91 
 92         scanf("%lld%lld",&m,&n);
 93         Build(1,m,1);//建树
 94         flag++;
 95         while(n--)
 96         {
 97 
 98             scanf("%lld%lld%lld",&a,&b,&c);
 99             update(a,b,1,c);//更新树
100         }
101         printf("Case %lld: The total value of the hook is %lld.\n",flag,sum[1]);//sum[1],表示根节点的值即区间的和
102     }
103     return 0;
104 }
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这