题目:

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.

思路:
区间修改 区间查询
因为题目中的操作并非加减而是修改 直接进行区间覆盖就可以
因为是多组询问 在建树的时候不仅要对tree[rt].w进行赋值 还要对tree[rt].lazy进行初始化

代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <string>
  6. #include <cstring>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10. typedef long long ll;
  11. typedef unsigned long long ull;
  12. const int inf=0x3f3f3f3f;
  13. const int maxn=1e5+;
  14. int n,m,t,x,y,z,ans;
  15.  
  16. struct node{
  17. int l,r,w,lazy;
  18. }tree[maxn<<];
  19.  
  20. void build(int l,int r,int rt){
  21. tree[rt].l=l;
  22. tree[rt].r=r;
  23. if(l==r){
  24. tree[rt].w=;
  25. tree[rt].lazy=;
  26. return;
  27. }
  28. int mid=(l+r)/;
  29. build(l,mid,rt*);
  30. build(mid+,r,rt*+);
  31. tree[rt].w=tree[rt*].w+tree[rt*+].w;
  32. tree[rt].lazy=;
  33. }
  34.  
  35. void pushdown(int rt){
  36. tree[rt*].lazy=tree[rt].lazy;
  37. tree[rt*+].lazy=tree[rt].lazy;
  38. tree[rt*].w=tree[rt].lazy*(tree[rt*].r-tree[rt*].l+);
  39. tree[rt*+].w=tree[rt].lazy*(tree[rt*+].r-tree[rt*+].l+);
  40. tree[rt].lazy=;
  41. }
  42.  
  43. void update(int rt){
  44. if(tree[rt].l>=x && tree[rt].r<=y){
  45. // int index=z-tree[rt].w;
  46. tree[rt].w=z*(tree[rt].r-tree[rt].l+);
  47. tree[rt].lazy=z;
  48. return;
  49. }
  50. if(tree[rt].lazy) pushdown(rt);
  51. int mid=(tree[rt].l+tree[rt].r)/;
  52. if(x<=mid) update(rt*);
  53. if(y>mid) update(rt*+);
  54. tree[rt].w=tree[rt*].w+tree[rt*+].w;
  55. }
  56.  
  57. void query(int rt){
  58. if(tree[rt].l>= && tree[rt].r<=n){
  59. ans+=tree[rt].w;
  60. return;
  61. }
  62. if(tree[rt].lazy) pushdown(rt);
  63. int mid=(tree[rt].l+tree[rt].r)/;
  64. if(mid>=) query(rt*);
  65. if(n>mid) query(rt*+);
  66. }
  67.  
  68. int main(){
  69. scanf("%d",&t);
  70. for (int id=;id<=t;id++){
  71. scanf("%d",&n);
  72. build(,n,);
  73. scanf("%d",&m);
  74. for(int i=;i<=m;i++){
  75. scanf("%d%d%d",&x,&y,&z);
  76. update();
  77. }
  78. ans=;
  79. query();
  80. printf("Case %d: The total value of the hook is %d.\n",id,ans);
  81. }
  82. return ;
  83. }

HDOJ 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. BZOJ3531 树剖 + 动态开点线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=3531 首先这题意要求树链上的最大值以及求和,其树链剖分的做法已经昭然若揭 问题在于这次的信息有宗教 ...

  2. MacOS环境中 python3 部署

    MacOS环境中 python3 部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.在MacOS安装Python3.6 1>.打开python关于MacOS版本的官方网 ...

  3. SQLyog远程连接腾讯云服务器数据库Mysql遇到的坑

    首先说明我的数据库是安装在云服务器上,不是专业的数据库服务器,没错就是10块钱包月的. 然后觉得使用SQLyog远程维护数据库比较方面,可是怎么都登录不上去. 下面分析原因: 1.安全组是否放过了访问 ...

  4. golang channle阻塞

    当一个channle容量写满时,会出现阻塞状态 package main func main() { var c1 = make(chan int, 10) for i := 0; i < 10 ...

  5. jsp实现验证码登陆

    login.jsp: <%@ page language="java" import="java.util.*,com.cn.servlet.*" pag ...

  6. Mac OS X 启用超级用户 sudo -s 获得系统权限 Mac终端命令

    为了防止误操作破坏系统,用户状态下时没有权限操作系统重要文件, 所以先要取得root权限:“sudo -s” 详见:https://www.jianshu.com/p/138b98e662ed

  7. TTS与MediaPlayer混合使用

    package com.xxx.xxx.Util; import android.content.Context; import android.media.MediaPlayer; import a ...

  8. DataReader分页性能测试

    参考程序地址:http://www.cnblogs.com/eaglet/archive/2008/10/09/1306806.html 最近遇见程序慢的问题,使用的DataReader,猜想是分页导 ...

  9. 最棒的 JavaScript 学习指南(2018版)

    译者注:原文作者研究了近2.4万篇 JavaScript 文章得出这篇总结,全文包含学习指南.新人上手.Webpack.性能.基础概念.函数式编程.面试.教程案例.Async Await.并发.V8. ...

  10. 使用java poi解析表格

    @Test public void poi() throws Exception { InputStream inputStream=new FileInputStream("C:\\Use ...