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

  1. Sample Input
  2.  
  3. Sample Output
  4. Case : The total value of the hook is .

题意:

  输入T,表示T组样例,输入n,表示有n个节点,最初所有点的值为1,然后给定X、Y、Z,将从X到Y改为权值为Z,计算权值和。

思路:

  线段树区间更新的模板。

代码:

  1. #include <cstdio>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. using namespace std;
  6. struct node
  7. {
  8. int l,r;
  9. int w;
  10. }tree[];
  11. void build(int l,int r,int n)
  12. {
  13. tree[n].l = l;
  14. tree[n].r = r;
  15. tree[n].w = ;
  16. if(l == r)
  17. return ;
  18. int temp = (l+r)/;
  19. build(l,temp,*n);
  20. build(temp+,r,*n+);
  21. }
  22. void update(int l,int r,int w,int n)
  23. {
  24. if(tree[n].l == l&&tree[n].r == r)
  25. {
  26. tree[n].w = w;
  27. return;
  28. }
  29. if(tree[n].w!=-)//将值更改为-1,代表这个区间内的值都已经更改
  30. {
  31. tree[*n].w = tree[*n+].w = tree[n].w;
  32. tree[n].w = -;
  33. }
  34. int temp=(tree[n].l+tree[n].r)/;
  35. if(r <= temp)
  36. update(l,r,w,*n);
  37. else if(l>temp)
  38. update(l,r,w,*n+);
  39. else
  40. {
  41. update(l,temp,w,*n);
  42. update(temp+,r,w,*n+);
  43. }
  44. }
  45. int find(int n)//查找
  46. {
  47. if(tree[n].w != -)//如果节点不为-1,表示这个区间内的值都是tree[n].w
  48. {
  49. return (tree[n].r-tree[n].l+)*tree[n].w;
  50. }
  51. else//如果节点为-1,递归下去。
  52. {
  53. return find(*n)+find(*n+);
  54. }
  55. }
  56. int main()
  57. {
  58. int T;
  59. scanf("%d",&T);
  60. for(int i=; i<=T; i++)
  61. {
  62. int n;
  63. scanf("%d",&n);
  64. build(,n,);
  65. int p;
  66. scanf("%d",&p);
  67. while(p--)
  68. {
  69. int a,b,w;
  70. scanf("%d%d%d",&a,&b,&w);
  71. update(a,b,w,);
  72. }
  73. printf("Case %d: ",i);
  74. printf("The total value of the hook is %d.\n",find());
  75. }
  76. }

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. 6.MySQL简介

    MySQL简介 ·点击查看MySQL官方网站 ·MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于facle旗下 ...

  2. MSSQL ADO.NET

    为什么要学ADO.NET 之前我们所学的只能在查询分析器里查看数据,操作数据,我们让普通用户去学sql,所以我们搭建了一个界面(Web/Winform) 让用户方面的操作数据库中的数据 什么是ADO. ...

  3. 组合数+逆元 A - Chat Group Gym - 101775A

    题目链接:https://cn.vjudge.net/contest/274151#problem/A 具体思路:我们可以先把所有的情况算出来,为2^n.然后不合法的情况减去就可以了.注意除法的时候要 ...

  4. ueditor和thinkphp框架整合修改版

    基于tp官网上的一篇文章修改的  因为tp中所有目录其实都是性对于入口文件的 在原来的基础上略做修改后 已经做到 无论项目放在www下的任何位置 图片在编辑器中回填后都能正常显示! http://fi ...

  5. nmon的安装和使用

    1.下载nmon https://zh.osdn.net/projects/sfnet_nmon/downloads/nmon_x86_64_rhel6/ 2../nmon_x86_64_rhel6 ...

  6. CTF线下赛AWD套路小结

    近打了2场CTF线下赛,把AWD模式中的一些小套路做一些总结,本人web狗,二进制部分就不班门弄斧了. 一. AWD模式简介 AWD:Attack With Defence,比赛中每个队伍维护多台服务 ...

  7. 实现在点击asp:button按钮后,不刷新当前页面

    方法1:return false <asp:Button ID="Button1" runat="server" Text="Button&qu ...

  8. Python多态、鸭子类型

    一.多态 多态指的是一类事物有多种形态. 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCMeta): #同一类事物:动物 @abc.ab ...

  9. [ python ] 格式化输出、字符集、and/or/not 逻辑判断

    格式化输出 %: 占位符 s: 字符串 d: 数字 %%: 表示一个%, 第一个%是用来转义 实例: name = input('姓名:') age = int(input('年龄:')) print ...

  10. [ python ] 线程的操作

    目录 (见右侧目录栏导航) - 1. 前言    - 1.1 进程    - 1.2 有了进程为什么要有线程    - 1.3 线程的出现    - 1.4 进程和线程的关系    - 1.5 线程的 ...