博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1698 Just a Hook
阅读量:5250 次
发布时间:2019-06-14

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

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 27077    Accepted Submission(s): 13469

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.
 

 

Source
 
 
 
解析:线段树,区间更新。
 
 
 
1 #include 
2 3 struct Node{ 4 int l, r, val; 5 int color; 6 bool lazy; 7 }; 8 Node node[100005<<2]; 9 10 void pushUp(int v)11 {12 node[v].val = node[v<<1].val+node[v<<1|1].val;13 }14 15 void build(int v, int l, int r)16 {17 node[v].l = l;18 node[v].r = r;19 node[v].lazy = false;20 if(node[v].l == node[v].r){21 node[v].val = 1;22 return;23 }24 int mid = (l+r)>>1;25 build(v<<1, l, mid);26 build(v<<1|1, mid+1, r);27 pushUp(v);28 }29 30 void update(int v, int l, int r, int c)31 {32 if(node[v].l == l && node[v].r == r){33 node[v].lazy = true;34 node[v].color = c;35 node[v].val = (r-l+1)*c;36 return;37 }38 int mid = (node[v].l+node[v].r)>>1;39 if(node[v].lazy){ //pushDown40 node[v].lazy = false;41 update(v<<1, node[v].l, mid, node[v].color);42 update(v<<1|1, mid+1, node[v].r, node[v].color);43 }44 if(r <= mid)45 update(v<<1, l, r, c);46 else if(l > mid)47 update(v<<1|1, l, r, c);48 else{49 update(v<<1, l, mid, c);50 update(v<<1|1, mid+1, r, c);51 }52 pushUp(v);53 }54 55 int main()56 {57 int t, cn = 0, N, Q;58 scanf("%d", &t);59 while(t--){60 scanf("%d%d", &N, &Q);61 if(Q == 0){62 printf("Case %d: The total value of the hook is %d.\n", ++cn, N);63 }64 else{65 build(1, 1, N);66 int x, y, z;67 while(Q--){68 scanf("%d%d%d", &x, &y, &z);69 update(1, x, y, z);70 }71 printf("Case %d: The total value of the hook is %d.\n", ++cn, node[1].val);72 }73 }74 return 0;75 }

 

转载于:https://www.cnblogs.com/inmoonlight/p/5682328.html

你可能感兴趣的文章
GAN——生成手写数字
查看>>
python中的pil模块_在Python中使用PIL模块处理图像的教程
查看>>
hashmap java 便利_Java中HashMap的四种遍历方法,及效率比较
查看>>
850是什么意思_楼板没有挠度和裂缝的计算结果原因是什么?
查看>>
华为v8支持云闪付吗_华为EMUI11将正式推送,37款机型计划升级,你的手机支持吗?...
查看>>
java快速开发框架_Java 后台开发框架
查看>>
go 切片 转字符串_Go语言爱好者周刊:第 58 期—关于 context
查看>>
android authorities 获取_挖穿Android第三十九天
查看>>
elementui展示多张图片_多张图片的PPT,如何排版的更有创意?
查看>>
中亿验钞机升级_新版人民币来了,可验钞机却无法识别?工作人员回应了
查看>>
airpods固件更新方法_如何更新 AirPods / AirPods Pro 的固件
查看>>
axure 图片切换图片的交互_用v-on:click v-bind v-show 实现图片切换
查看>>
js起一个数的平方根_LeetCode 题解 | 69. x 的平方根
查看>>
boot jndi数据源 spring_MyBatis 多数据源读写分离(注解实现)
查看>>
gin post 数据参数_Golang GinWeb框架快速入门/参数解析
查看>>
新增数据接口_Tablestore入门手册-UpdateRow接口详解
查看>>
账号管理工具_myMail — 手机端的最佳邮箱管理工具
查看>>
的g极串一个电阻_深入讲解三极管和MOS管加下拉电阻的作用,下次设计电路注意了...
查看>>
某一列高度变化_降料面过程中煤气成份的变化规律
查看>>
编码方式_常见的编码方式之ASCII编码解答
查看>>