最近刚学线段树,做了些经典题目来练手

Just a Hook

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

Total Submission(s): 24370 Accepted Submission(s): 12156

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.

题目大意:Dota中的屠夫,有一个钩子,钩子分为n段,现在给出m个指令,将【X,Y】段钩子变成Z种(Z分为三种:1为铜钩,2为银钩,3为金钩)初始钩子都为Cu,m次操作后,求整个钩的价值

每组样例有t组数据

(N<=100000,1<=X<=Y<=N,1<=Z<=3)

  1. 维护一棵线段树,区间修改,最后求一下全区间的和,唯一可以说的就是惰性标记是直接改变,并非不断累积
  2. 因为所求是全区间的和,所以没必要再额外写一个区间求和的函数,直接在修改时下放标记,最后输出这颗线段树的根即可

代码如下:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cstdio>
  5. using namespace std;
  6. #define maxlen 100001
  7. int value[maxlen<<2]={0},delta[maxlen<<2]={0};
  8. int dota[maxlen]={0};
  9. void updata(int now)
  10. {
  11. value[now]=value[now<<1]+value[now<<1|1];
  12. }
  13. void build(int l,int r,int now)
  14. {
  15. if (l==r)
  16. {
  17. value[now]=dota[l];
  18. return;
  19. }
  20. int mid=(l+r)>>1;
  21. build(l,mid,now<<1);
  22. build(mid+1,r,now<<1|1);
  23. updata(now);
  24. }
  25. void pushdown(int now,int ln,int rn)
  26. {
  27. if (delta[now]!=0)
  28. {
  29. delta[now<<1]=delta[now];
  30. delta[now<<1|1]=delta[now];
  31. value[now<<1]=delta[now]*ln;
  32. value[now<<1|1]=delta[now]*rn;
  33. delta[now]=0;
  34. }
  35. }
  36. void section_change(int L,int R,int l,int r,int now,int data)
  37. {
  38. if (L<=l && R>=r)
  39. {
  40. value[now]=data*(r-l+1);
  41. delta[now]=data;
  42. return;
  43. }
  44. int mid=(l+r)>>1;
  45. pushdown(now,mid-l+1,r-mid);
  46. if (L<=mid)
  47. section_change(L,R,l,mid,now<<1,data);
  48. if (R>mid)
  49. section_change(L,R,mid+1,r,now<<1|1,data);
  50. updata(now);
  51. }
  52. int main()
  53. {
  54. int t;
  55. int n;
  56. int m;
  57. int time=1;
  58. scanf("%d",&t);
  59. while (true)
  60. {
  61. if (t==0) break;
  62. scanf("%d",&n);
  63. for (int i=1; i<=n; i++)
  64. dota[i]=1;
  65. memset(delta,0,sizeof(delta));
  66. memset(value,0,sizeof(value));
  67. build(1,n,1);
  68. scanf("%d",&m);
  69. for (int i=1; i<=m; i++)
  70. {
  71. int start,end,data;
  72. scanf("%d%d%d",&start,&end,&data);
  73. section_change(start,end,1,n,1,data);
  74. }
  75. printf("Case %d: The total value of the hook is %d.\n",time,value[1]);
  76. time++;
  77. t--;
  78. }
  79. return 0;
  80. }

HDU-1698 JUST A HOOK 线段树的更多相关文章

  1. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  2. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  3. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  5. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  7. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  9. HDU 1698 Just a Hook(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  10. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. Cg关键字(keywords)

    保留标识符 除了下面列出的,任何以两个的下划线作为前缀(例如,__ newType)的标识符被保留. 注意,矩阵(matrix)和向量类型(vector types)(如half2x3或float4) ...

  2. [转] 值得推荐的C/C++框架和库

    http://www.cppblog.com/merlinfang/archive/2014/12/26/209311.aspx

  3. FusionCharts或其它flash的div图层总是浮在最上层? (转)

    div的图层由div的style中的z-index来决定,z-index是层垂直屏幕的坐标,0最小,越大的话位置越靠上. 由于FusionCharts的图表都放在div中,如果页面还有其他的div,将 ...

  4. 【转】【WPF】wpf 图片指针处理

    我一直用GDI+做Winform 的基于指针的图片处理,这次下决心全部移到wpf上(主要是显示布局很方便)采用的图片是2512*3307 的大图 830万像素类库基于WritableBitmapEx ...

  5. OpenGL2.0及以上版本中glm,glut,glew,glfw,mesa等部件的关系

    OpenGL2.0及以上版本中gl,glut,glew,glfw,mesa等部件的关系 一.OpenGL OpenGL函数库相关的API有核心库(gl),实用库(glu),辅助库(aux).实用工具库 ...

  6. php基础05:常量

    <?php // 1.PHP 常量介绍 // 常量是单个值的标识符(名称).在脚本中无法改变该值.有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号). // 2设置 PHP 常量 // ...

  7. ios 定位 监听是否跨入某个指定的区域

    /*****监听用户是否进入和走出 在某个区域*****/ 1 #import "ViewController.h" #import <CoreLocation/CoreLo ...

  8. 20145208 《Java程序设计》第4周学习总结

    20145208 <Java程序设计>第4周学习总结 教材学习内容总结 继承 在学习指导中我了解到继承是符合DRY原则的,DRY(Don't repeat yourself),字面意思来看 ...

  9. .NET MVC框架中控制器接收参数的四种方式

    1.通过路由中的配置的参数名字直接接收(要求:两者同名) routes.MapRoute(                 name: "Default",             ...

  10. 从士兵到程序员再到SOHO程序员

    2013年9月13日,我从就职了一年半的S公司正式离职,并开始了我梦寐以求的“SOHO程序员”之路. 这对于我来说,是一次人生道路上的重要选择,在这里,我想分享一下我是如何选择了这条道路的,同时也是对 ...