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.

思路:

0:代表线段被覆盖了。

1,2,3:分别代表值。

先建立线段树:

【1,10】

1

/                       \

【1,5】                   【6,10】

1                           1

/       \                  /         \

【1,3】    【4,5】            【6,8】   【9,10】

1            1                 1           1

/    \        /    \            /     \      /     \

【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

1       1       1     1         1       1      1      1

/     \                          /      \

【1,1】 【2,2】                  【6,6】  【7,7】

1        1                        1        1

插入1 5 2后:

【1,10】

/                       \

【1,5】                   【6,10】

1

/       \                  /         \

【1,3】    【4,5】            【6,8】   【9,10】

1            1                 1           1

/    \        /    \            /     \      /     \

【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

1      1        1     1          1      1       1       1

/     \                          /      \

【1,1】 【2,2】                  【6,6】  【7,7】

插入5 9 3后:

【1,10】

/                       \

【1,5】                   【6,10】

/       \                  /         \

【1,3】    【4,5】            【6,8】   【9,10】

0

/    \        /    \            /     \      /     \

【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

1                             1       1

/     \                          /      \

【1,1】 【2,2】                  【6,6】  【7,7】

1         1                        1        1

将红色的值加起来就是答案24了。

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. const int MAXN = ;
  5. int n;//hooks的个数
  6. int q;//operations的个数
  7. struct Node
  8. {
  9. int l;//左端点
  10. int r;//右端点
  11. int v;//价值
  12. }tree[MAXN * ];
  13. void BuildTree(int t,int l,int r)
  14. {
  15. tree[t].l = l;
  16. tree[t].r = r;
  17. tree[t].v = ;
  18. if (l == r)//为叶节点
  19. return ;
  20. BuildTree(t + t,l,(l + r) / );//建立左子树
  21. BuildTree(t + t + ,(l + r) / + ,r);//建立右子树
  22. }
  23. void Update(int t,int l,int r,int v)
  24. {
  25. if (tree[t].l == l && tree[t].r == r)//区间匹配
  26. {
  27. tree[t].v = v;//将value覆盖
  28. return ;
  29. }
  30. if (tree[t].v == v)//如果值相同,就没必要更改
  31. return ;
  32. if (tree[t].v > )//区间tree[t].l到tree[t].r的值要更改
  33. {
  34. tree[t + t + ].v = tree[t + t].v = tree[t].v;
  35. tree[t].v = ;
  36. }
  37. if (r <= (tree[t].l + tree[t].r) / )//在左孩子
  38. Update(t + t,l,r,v);
  39. else if (l > (tree[t].l + tree[t].r) / )//在右孩子
  40. Update(t + t + ,l,r,v);
  41. else//横跨中点
  42. {
  43. Update(t + t,l,(tree[t].l + tree[t].r) / ,v);
  44. Update(t + t + ,(tree[t].l + tree[t].r) / + ,r,v);
  45. }
  46. }
  47. int GetValue(int t)
  48. {
  49. int ans = ;
  50. if (tree[t].v > )
  51. ans += (tree[t].r - tree[t].l + ) * tree[t].v;
  52. else
  53. {
  54. ans += GetValue(t + t);
  55. ans += GetValue(t + t + );
  56. }
  57. return ans;
  58. }
  59. int main(void)
  60. {
  61. int t,x,y,z,i = ;
  62. scanf("%d",&t);
  63. while (t --)
  64. {
  65. scanf("%d",&n);
  66. BuildTree(,,n);//建立线段树
  67. scanf("%d",&q);
  68. while (q --)
  69. {
  70. scanf("%d%d%d",&x,&y,&z);
  71. Update(,x,y,z);//更新区间的值
  72. }
  73. printf("Case %d: The total value of the hook is %d.\n",i ++,GetValue());//得到value的总值
  74. }
  75. return ;
  76. }

G - Just a Hook的更多相关文章

  1. Flask初学者:g对象,hook钩子函数

    Flask的g对象 作用:g可以可以看作是单词global的缩写,使用“from flask import g”导入,g对象的作用是保存一些在一次请求中多个地方的都需要用到的数据,这些数据可能在用到的 ...

  2. vue.js中英文api

    全局配置 Vue.config is an object containing Vue's global configurations. You can modify its properties l ...

  3. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  4. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  5. GET/POST/g和钩子函数(hook)

    GET请求和POST请求: 1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放在url中,并且是通过`?`的 ...

  6. [MetaHook] Surface hook

    Hook ISurface function. #include <metahook.h> #include <vgui/ISurface.h> using namespace ...

  7. 理解钩子Hook以及在Thinkphp下利用钩子使用行为扩展

    什么是钩子函数 个人理解:钩子就像一个”陷阱”.”监听器”,当A发送一个消息到B时,当消息还未到达目的地B时,被钩子拦截调出一部分代码做处理,这部分代码也叫钩子函数或者回调函数 参考网上说法 譬如我们 ...

  8. Linux LSM(Linux Security Modules) Hook Technology

    目录 . 引言 . Linux Security Module Framework Introduction . LSM Sourcecode Analysis . LSMs Hook Engine: ...

  9. c c++ 函数入口和出口的hook(gcc 编译选项),然后打印出函数调用关系的方法

    GCC Function instrumentation机制可以用来跟踪函数的调用关系,在gcc中对应的选项为“-finstrument-functions”.可查看gcc的man page来获取更详 ...

随机推荐

  1. 关于FireFox下 CSS3 transition 与其他浏览器的差异

    最近一个项目,动画效果全靠CSS3来做,用得比较多的transition,发现了一点火狐与其他浏览器的小差异. 首先我们写CSS的时候,一般为属性值为0的属性,我们一般会这样写 #id{ posito ...

  2. 邻接表存储图,DFS遍历图的java代码实现

    import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new i ...

  3. 【现代程序设计】【homework-05】

    这次作业的运行效果图: 新建了20个客户端线程,服务器相应开了20个线程接收客户端数据,每一秒输出每一轮的结果 这次作业用c#完成 利用 Socket 类实现了局域网中的客户端和服务器之间的通信 主要 ...

  4. gulp之静态资源防缓存处理

    最近,因为校友网项目开始有些规模了.开始就要考虑对静态资源进行工程自动化的管理.一讲到前端的自动化工具,大家或许都会想到Grunt,Gulp,或者百度的FIS.这三个都有各自的特点,大家可以依据自己的 ...

  5. 【转】深受开发者喜爱的10大Core Data工具和开源库

    http://www.cocoachina.com/ios/20150902/13304.html 在iOS和OSX应用程序中存储和查询数据,Core Data是一个很好的选择.它不仅可以减少内存使用 ...

  6. HDU 5660 jrMz and angles (暴力枚举)

    jrMz and angles 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/E Description jrMz has tw ...

  7. javascript中字符串的trim功能表达式

    string.replace(/(^\s*)|(\s*$)/g, "") 用来删除行首行尾的空白字符(包括空格.制表符.换页符等等)

  8. LINUX下成功搭建SVN

    步骤如下: 1: yum install -y subversion 2:svnserve –version 3: [root@singledb ~]# mkdir /u02/svn [root@si ...

  9. ArcGIS10的GDB文件解析(初步)

    早就应该写一写关于esri的一些改变 参考http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//006z000000tr00000 ...

  10. c# socket通信较完善方案

    c#的socket通信应用.文件较多.附件为工程.  core   AbstractBytesWorker.cs    字节工作器(基类),用于用于同一不同功能的字节工作器  BinaryHand.c ...