Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24102    Accepted Submission(s): 12063

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.
 
一开始忘记了r--,l--
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstring>
  6. using namespace std;
  7. const int Max=+;
  8. int segTree[Max<<];
  9. int change[Max<<];
  10. int T,q,r,l,z,n;
  11. int PushUp(int node) //回溯,节点更新
  12. {
  13. segTree[node]=(segTree[node<<]+segTree[node<<|]);
  14. return ;
  15. }
  16. int PushDown(int node,int arange)
  17. {
  18. if(change[node]){
  19. change[node<<]=change[node<<|]=change[node];
  20. segTree[node<<]=change[node]*(arange-(arange>>));
  21. segTree[node<<|]=change[node]*(arange>>);
  22. change[node]=;
  23. }
  24. return ;
  25. }
  26. void build(int node,int begin,int end)
  27. {
  28. segTree[node]=;
  29. change[node]=;
  30. if(begin==end)
  31. return;
  32. int m=(begin+end)>>;
  33. build(node<<,begin,m);
  34. build(node<<|,m+,end);
  35. PushUp(node);
  36. return;
  37. }
  38. int updata(int node,int begin,int end)
  39. {
  40. if(l<=begin&&end<=r)
  41. {
  42. segTree[node]=z*(end-begin+);
  43. change[node]=z;
  44. return ;
  45. }
  46. PushDown(node,end-begin+);
  47. int mid=(begin+end)>>;
  48. if(mid>=l) updata(node<<,begin,mid);
  49. if(mid<r) updata(node<<|,mid+,end);
  50. PushUp(node);
  51. }
  52. int main()
  53. {
  54. int i,j;
  55. freopen("in.txt","r",stdin);
  56. cin>>T;
  57. int k=T;
  58. while(T--)
  59. {
  60. scanf("%d%d",&n,&q);
  61. build(,,n-);
  62. for(i=;i<q;i++)
  63. {
  64. scanf("%d%d%d",&l,&r,&z);
  65. l--,r--;
  66. updata(,,n-);
  67. }
  68. printf("Case %d: The total value of the hook is %d.\n",k-T,segTree[]);
  69. }
  70. }

更简单的代码:

  1. #include <cstdio>
  2. using namespace std;
  3. #define maxN 100005
  4. #define m ((L+R)>>1)
  5. int T, N, Q, X, Y, Z, i;
  6. int val[ * maxN];
  7. void push_down(int o){
  8. val[o * ] = val[o * + ] = val[o];
  9. val[o] = ;
  10. }
  11. void update(int o, int L, int R){
  12. if (X <= L&&R <= Y) val[o] = Z;
  13. else{
  14. if (val[o]) push_down(o);
  15. if (X <= m) update(o * , L, m);
  16. if (Y > m) update(o * + , m + , R);
  17. }
  18. }
  19. int sum(int o,int L,int R){
  20. if (val[o]) return (R-L+)*val[o];
  21. return sum(o * , L, m) + sum(o * + , m + , R);
  22. }
  23. int main(){
  24. scanf("%d", &T);
  25. for (int Case = ; Case <= T; Case++){
  26. val[] = ;
  27. scanf("%d%d", &N, &Q);
  28. for (i = ; i < Q; i++){
  29. scanf("%d%d%d", &X, &Y, &Z);
  30. update(, , N);
  31. }
  32. printf("Case %d: The total value of the hook is %d.\n",Case,sum(,,N));
  33. }
  34. return ;
  35. }

hdu just a hook(线段树,区间修改)的更多相关文章

  1. HDU - 4578 Transformation(线段树区间修改)

    https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuan ...

  2. HDU 5861 Road(线段树 区间修改 单点查询)

    Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  3. hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)

    Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...

  4. HDU1698Just a Hook(线段树 + 区间修改 + 求和)

    题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...

  5. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

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

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

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

  7. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  8. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  9. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

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

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

随机推荐

  1. .net mvc结合微软提供的FormsAuthenticationTicket登陆

    一.Web.config <system.web> <compilation debug="true" targetFramework="4.5&quo ...

  2. Windows10 Ubuntu子系统折腾

    UPDATE:(参考文章) 快速解决方案 使用cmder,设置startup参数为: %windir%\system32\bash.exe ~ 这样打开cmder就是默认进入bash了. ------ ...

  3. Oracle 10g RAC启动与关闭命令

    一. 检查共享设备 一般情况下, 存放OCR 和 Voting Disk 的OCFS2 或者raw 都是自动启动的. 如果他们没有启动,RAC 肯定是启动不了的. 1.1 如果使用ocfs2的,检查o ...

  4. hdu 1245 Saving James Bond

    http://acm.hdu.edu.cn/showproblem.php?pid=1245 #include <cstdio> #include <cstring> #inc ...

  5. 关于NGINX变量的一些测试结果

    作为NGINX变量,不像正规语言那么正式. 但处理自定义和内部变量时,这些操作,也是少不了的. geo $dollar { default "$"; } server { list ...

  6. Powershell 执行外部命令

    Powershell 执行外部命令 724 11月, 2011  在 Powershell  tagged Powershell教程 / 程序 by Mooser Lee本文索引[隐藏]1通过nets ...

  7. 转:sql语句中GROUP BY 和 HAVING和使用 count()

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  8. codility上的问题 (22)

    问题描述: 用1 * 1, 1 * 2的矩形覆盖一个n行m列的矩形,问有多少种方法. 数据范围 : n [1..10^6],  m [ 1..7] 要求复杂度: 时间  O(log(n) * 8 ^m ...

  9. 05_Elasticsearch 单模式下API的增删改查操作

    05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...

  10. wikioi1369 xth 砍树

    题目描述 Description 在一个凉爽的夏夜,xth 和 rabbit 来到花园里砍树.为啥米要砍树呢?是这样滴, 小菜儿的儿子窄森要出生了.Xth这个做伯伯的自然要做点什么.于是他决定带着 r ...