博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1698 Just a Hook (线段树模板题-区间求和)
阅读量:7123 次
发布时间:2019-06-28

本文共 3737 字,大约阅读时间需要 12 分钟。

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.

 
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.
题目大意:Hook有一个由n个扣子组成的长链,初始值都为铜质,求q次改变之后长链的总值,每一次改变可以将一个扣子改成银质或金质。
思路:即一个区间修改查询的线段树模板题啦,有q次的区间修改,最后查询一次1-n的总和即可
 
1 #include
2 #include
3 #include
4 #include
5 6 using namespace std; 7 const int maxn = 100005; 8 int sum[maxn<<2],add[maxn<<2]; 9 int n,q,T;10 void pushup(int rt){11 sum[rt] = sum[rt<<1]+sum[rt<<1|1];12 }13 void pushdown(int rt, int ln, int rn)14 {15 if(add[rt])16 {17 sum[rt << 1] = ln * add[rt];18 sum[rt << 1 | 1] = rn * add[rt];19 add[rt << 1] = add[rt];20 add[rt << 1 | 1] = add[rt];21 add[rt] = 0;22 }23 return;24 }25 void build(int l,int r,int rt){26 add[rt] = 0;27 if(l==r){28 sum[rt] = 1;29 return ;30 }31 int mid = (l+r)>>1;32 build(l,mid,rt<<1);33 build(mid+1,r,rt<<1|1);34 pushup(rt);35 }36 void update(int l,int r,int rt,int L,int R,int C)37 {38 if(L<=l&&r<=R){39 sum[rt] = (r-l+1)*C;40 add[rt] = C;41 return ;42 }43 int mid = (l+r)>>1;44 pushdown(rt,mid-l+1,r-mid);45 if(L<=mid)update(l,mid,rt<<1,L,R,C);46 if(R>mid)update(mid+1,r,rt<<1|1,L,R,C);47 pushup(rt);48 }49 int query(int l,int r,int rt,int L,int R)50 {51 if(L<=l&&r<=R)return sum[rt];52 int mid = (l+r)>>1;53 pushdown(rt,mid-l+1,r-mid);54 int ans = 0;55 if(L<=mid) ans += query(l,mid,rt<<1,L,R);56 if(R>mid) ans += query(mid+1,r,rt<<1|1,L,R);57 return ans;58 }59 int main()60 {61 scanf("%d",&T);62 for(int t=1;t<=T;t++){63 scanf("%d",&n);64 build(1,n,1);65 scanf("%d",&q);66 int l,r,x;67 while(q--){68 scanf("%d%d%d",&l,&r,&x);69 update(1,n,1,l,r,x);70 }71 printf("Case %d: The total value of the hook is %d.\n",t,query(1,n,1,1,n));72 }73 return 0;74 }

 

转载于:https://www.cnblogs.com/wangrunhu/p/9587907.html

你可能感兴趣的文章
线程的几种可运行状态
查看>>
[bzoj 2555]Substring
查看>>
tab标签
查看>>
ecshop新增银联企业网银支付方式
查看>>
Angular5学习笔记 - 配置NG-ZORRO(八)
查看>>
使用Netty实现HTTP服务器
查看>>
JAVA开发工具eclipse中@author怎么改
查看>>
存储引擎与锁
查看>>
sqlog连接虚拟机mysql服务
查看>>
出错,网页显示不出内容
查看>>
Spring中的后置处理器BeanPostProcessor讲解
查看>>
《FPGA全程进阶---实战演练》第十四章 蜂鸣器操作
查看>>
浅析firmware完整生存和使用流程 【转】
查看>>
《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!【转】
查看>>
MMU介绍【转】
查看>>
构造函数
查看>>
利用自定义DataTable来重画数据集的用法
查看>>
职场沟通技巧
查看>>
Python爬虫——解决urlretrieve下载不完整问题且避免用时过长
查看>>
如何区分云计算和非云计算,首先得看它的核心本质——计算是否在线,计算的使用是否通过互联网完成。我从在阿里巴巴做云计算的第一天开始,就告诉自己:“云计算是一个社会最基础的公共服务,就像电一样。”...
查看>>