题目链接:https://vjudge.net/problem/HDU-1698

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. 

InputThe 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. 
OutputFor 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. 1
  2. 10
  3. 2
  4. 1 5 2
  5. 5 9 3

Sample Output

  1. Case 1: The total value of the hook is 24.

 

题解:

经典的区间染色问题。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const double EPS = 1e-;
  15. const int INF = 2e9;
  16. const LL LNF = 2e18;
  17. const int MAXN = 1e5+;
  18.  
  19. int sum[MAXN<<], setv[MAXN<<];
  20.  
  21. void push_up(int u)
  22. {
  23. sum[u] = sum[u*] + sum[u*+];
  24. }
  25.  
  26. void push_down(int u, int l, int r)
  27. {
  28. if(setv[u]>)
  29. {
  30. setv[u*] = setv[u*+] = setv[u];
  31. sum[u*] = (r-l++)/*setv[u];
  32. sum[u*+] = (r-l+)/*setv[u];
  33. setv[u] = ;
  34. }
  35. }
  36.  
  37. void build(int u, int l, int r)
  38. {
  39. if(l==r)
  40. {
  41. setv[u] = ; //每个鱼钩初始化为金属1
  42. sum[u] = ;
  43. return;
  44. }
  45.  
  46. int mid = (l+r)>>;
  47. build(u*, l, mid);
  48. build(u*+, mid+, r);
  49. push_up(u);
  50. setv[u] = ;
  51. }
  52.  
  53. void set_val(int u, int l, int r, int x, int y, int val)
  54. {
  55. if(x<=l && r<=y)
  56. {
  57. sum[u] = (r-l+)*val;
  58. setv[u] = val;
  59. return;
  60. }
  61.  
  62. push_down(u, l, r);
  63. int mid = (l+r)>>;
  64. if(x<=mid) set_val(u*, l, mid, x, y, val);
  65. if(y>=mid+) set_val(u*+, mid+, r, x, y, val);
  66. push_up(u);
  67. }
  68.  
  69. int query(int u, int l, int r, int x, int y)
  70. {
  71. if(x<=l && r<=y)
  72. return sum[u];
  73.  
  74. push_down(u, l, r);
  75. int ret = ;
  76. int mid = (l+r)>>;
  77. if(x<=mid) ret += query(u*, l, mid, x, y);
  78. if(y>=mid+) ret += query(u*+, mid+, r, x, y);
  79. return ret;
  80. }
  81.  
  82. int main()
  83. {
  84. int T, n, m;
  85. scanf("%d", &T);
  86. for(int kase = ; kase<=T; kase++)
  87. {
  88. scanf("%d%d", &n, &m);
  89.  
  90. build(, , n);
  91. for(int i = ; i<=m; i++)
  92. {
  93. int x, y, z;
  94. scanf("%d%d%d", &x, &y, &z);
  95. set_val(, , n, x, y, z);
  96. }
  97.  
  98. int ans = query(, , n, , n);
  99. printf("Case %d: The total value of the hook is %d.\n", kase, ans);
  100. }
  101. }

HDU1698 Just a Hook —— 线段树 区间染色的更多相关文章

  1. 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...

  2. hdu1698 Just a hook 线段树区间更新

    题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...

  3. hdu1698 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(线段树区间更新)

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

  5. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  6. HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

    题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...

  7. hdu 5023(线段树区间染色,统计区间内颜色个数)

    题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数( ...

  8. 线段树区间染色 ZOJ 1610

    Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...

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

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

随机推荐

  1. [luoguP2948] [USACO09OPEN]滑雪课Ski Lessons(DP)

    传送门 f[i][j]表示i时刻能力值为j的最大滑雪数 显然f[0][1]=0,开始搜索 三种转移: ①美美的喝上一杯**:f[i+1][j]=max(f[i+1][j],f[i][j]) ②滑雪,f ...

  2. hdu4115:Eliminate the Conflict

    n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...

  3. 10.1——pair,map,set,multimap,multiset

    map和set只允许相同的键出现一次,而multimap和multiset则允许出现多次. 1. 引言——pair类型: pair需要添加头文件utility头文件 make_pair<v1,v ...

  4. POJ 2391 多源多汇拆点最大流 +flody+二分答案

    题意:在一图中,每个点有俩个属性:现在牛的数量和雨棚大小(下雨时能容纳牛的数量),每个点之间有距离, 给出牛(速度一样)在顶点之间移动所需时间,问最少时间内所有牛都能避雨. 模型分析:多源点去多汇点( ...

  5. python学习之-- redis模块操作 HASH

    redis 操作 之 -Hash Hash 操作:hash在内存中的存储格式 name hash n1 ------> k1 -> v1 k2 -> v2 k3 -> v3hs ...

  6. 从零开始写STL-二叉搜索树

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...

  7. Construct Binary Tree from Preorder and Inorder Traversal (DFS,参考)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. vijos 2035 奇数偶数与绚丽多彩的数

    描述 Q先生是一个热爱学习的男孩子. 他认为一个 n 位的正整数 x 若能被称作是绚丽多彩的,一定要满足对于{1,3,5,7,9} 中任意一个奇数或者没有在 x 中出现,或者在 x 中出现了恰好奇数次 ...

  9. Java面试题,深入理解final关键字

    final关键字 final的简介 final可以修饰变量,方法和类,用于表示所修饰的内容一旦赋值之后就不会再被改变,比如String类就是一个final类型的类. final的具体使用场景 fina ...

  10. Tomcat可以实现Session共享方案

    说明:原来Tomcat也是可以实现Session共享的,这样大大减少的硬编码的实现,并且前面用Nginx分流时不用考虑Session的问题,因为是Web容器提供了Session共享的支持. 1.在每个 ...