Just a Hook(树状数组)

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.

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

11 210 32 41 5 2 55 9 3

Sample Output

1Case 1: The total value of the hook is 24.题意:一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。题解:简单的线段树的区间更新。 2 3 1 #include<cstdio> 4 2 #include<cstring> 5 3 #include<cmath> 6 4 #include<iostream> 7 5 #include<algorithm> 8 6 using namespace std; 9 7 const int MAXN=2e5+10; 10 8 typedef long long ll; 11 9 #define lson l,m,i<<1 12 10 #define rson m+1,r,i<<1|1 13 11 typedef struct Node 14 12 { 15 13 ll l,r; 16 14 ll mid() 17 15 { 18 16 return (l+r)/2.0; 19 17 } 20 18 ll value; 21 19 } Node; 22 20 Node node[MAXN<<2]; 23 21 ll sum[MAXN<<2];//用来记录每个节点的值(区间和) 24 22 ll add[MAXN<<2];//延迟标记数组 25 23 void push_up(ll i) 26 24 { 27 25 sum[i]=sum[i<<1]+sum[i<<1|1];//该节点的和等于左儿子加幼儿子的和 28 26 } 29 27 void Build(ll l,ll r,ll i) 30 28 { 31 29 node[i].l=l; 32 30 node[i].r=r; 33 31 node[i].value=0; 34 32 sum[i]=0; 35 33 add[i]=0; 36 34 if(l==r) 37 35 { 38 36 sum[i]=1; 39 37 node[i].value=1; 40 38 return ; 41 39 } 42 40 ll m=node[i].mid(); 43 41 Build(lson);//建立左子树 44 42 Build(rson);//建立右子树 45 43 push_up(i); 46 44 } 47 45 ll M; 48 46 void Push_down(ll i,ll len)//延迟标记下压 49 47 { 50 48 if(add[i]) 51 49 { 52 50 add[i<<1]=add[i];//把该节点的标记赋给他的左儿子 51 add[i<<1|1]=add[i];//把该节点的标记赋给他的右儿子 53 52 sum[i<<1]=add[i]*(len-(len>>1));//把左儿子更新 54 53 sum[i<<1|1]=add[i]*(len>>1);//把右儿子更新 55 54 add[i]=0;//把该节点的延迟标记删除 56 55 } 57 56 } 58 57 59 58 void update(ll l,ll r,ll i,ll v) 60 59 { 61 60 if(node[i].r==r&&node[i].l==l) 62 61 { 63 62 add[i]=v;//延迟标记 64 63 sum[i]=v*(r-l+1);//给该节点赋值 65 64 return; 66 65 } 67 66 ll m=node[i].mid(); 68 67 Push_down(i,node[i].r-node[i].l+1);//来判断节点是否有延迟标记,若有则更新 69 68 if(r<=m) 70 69 update(l,r,i<<1,v);//查询区间在该节点的左子树上 70 else 71 71 { 72 72 if(l>m) 73 73 update(l,r,i<<1|1,v);///查询区间在该节点的右子树上 74 74 75 75 else 76 76 { 77 77 update(l,m,i<<1,v);//查询区间在该节点的两颗树上 78 78 update(m+1,r,i<<1|1,v); 79 79 } 80 80 } 81 81 push_up(i);//归的第一步,向上更新 82 82 } 83 83 int main() 84 84 { 85 85 ll m,n,a,b,T,c; 86 86 ll flag=0; 87 87 scanf("%lld",&T); 88 88 while(T--) 89 89 { 90 90 sum[0]=0; 91 91 92 92 scanf("%lld%lld",&m,&n); 93 93 Build(1,m,1);//建树 94 94 flag++; 95 95 while(n--) 96 96 { 97 97 98 98 scanf("%lld%lld%lld",&a,&b,&c); 99 99 update(a,b,1,c);//更新树 100100 } 101101 printf("Case %lld: The total value of the hook is %lld.\n",flag,sum[1]);//sum[1],表示根节点的值即区间的和 102102 } 103103 return 0; 104104 }
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang