[关闭]
@Chilling 2017-02-16T09:58:49.000000Z 字数 3944 阅读 828

HDU-1689: Just a Hook

线段树


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.

题意:最初钩子每单位长度价值为1,进行q次操作,每次输入x,y和z,表示区间范围和替换的价值。每组测试数据输出一个总价值。
大致思路:每次找到对应节点后标记,并按照lazy标记修改当前节点的sum值。由于是用价值z直接替换区间内的值,所以lazy不用累加,直接替换。

  1. void build(int id,int l,int r)
  2. {
  3. s[id].l=l;
  4. s[id].r=r;
  5. s[id].lazy=0; //初始为0,表示没有进行替换
  6. if(s[id].l==s[id].r)
  7. s[id].sum=1;
  8. else
  9. {
  10. int mid=(l+r)/2;
  11. build(id*2,l,mid);
  12. build(id*2+1,mid+1,r);
  13. s[id].sum=s[id*2].sum+s[id*2+1].sum;
  14. }
  15. }
  1. void pushdown(int id)
  2. {
  3. s[id*2].lazy=s[id].lazy; //直接替换
  4. s[id*2+1].lazy=s[id].lazy;
  5. s[id*2].sum=s[id].lazy*(s[id*2].r-s[id*2].l+1);
  6. s[id*2+1].sum=s[id].lazy*(s[id*2+1].r-s[id*2+1].l+1);
  7. s[id].lazy=0;
  8. }
  9. void rep(int id,int l,int r,int val)
  10. {
  11. if(l<=s[id].l&&s[id].r<=r) //完全覆盖
  12. {
  13. s[id].lazy=val;
  14. s[id].sum=val*(s[id].r-s[id].l+1); //修改当前节点的sum值
  15. return;
  16. }
  17. if(s[id].lazy!=0) //不完全覆盖,且标记不为0,向下传递
  18. pushdown(id);
  19. int mid=(s[id].l+s[id].r)/2;
  20. if(r<=mid)
  21. rep(id*2,l,r,val);
  22. else if(l>mid)
  23. rep(id*2+1,l,r,val);
  24. else
  25. {
  26. rep(id*2,l,r,val);
  27. rep(id*2+1,l,r,val);
  28. }
  29. s[id].sum=s[id*2].sum+s[id*2+1].sum; //回溯
  30. }
  1. long long sum(int id,int l,int r)
  2. {
  3. if(l<=s[id].l&&s[id].r<=r)
  4. return s[id].sum;
  5. if(s[id].lazy!=0)
  6. pushdown(id);
  7. int mid=(s[id].l+s[id].r)/2;
  8. if(r<=mid)
  9. return sum(id*2,l,r);
  10. else if(l>mid)
  11. return sum(id*2+1,l,r);
  12. else
  13. return sum(id*2,l,r)+sum(id*2+1,l,r);
  14. }

完整代码:

  1. #include<stdio.h>
  2. struct node
  3. {
  4. int l,r;
  5. long long sum,lazy;
  6. }s[100005*4];
  7. void build(int id,int l,int r)
  8. {
  9. s[id].l=l;
  10. s[id].r=r;
  11. s[id].lazy=0;
  12. if(s[id].l==s[id].r)
  13. s[id].sum=1;
  14. else
  15. {
  16. int mid=(l+r)/2;
  17. build(id*2,l,mid);
  18. build(id*2+1,mid+1,r);
  19. s[id].sum=s[id*2].sum+s[id*2+1].sum;
  20. }
  21. }
  22. void pushdown(int id)
  23. {
  24. s[id*2].lazy=s[id].lazy; //直接替换
  25. s[id*2+1].lazy=s[id].lazy;
  26. s[id*2].sum=s[id].lazy*(s[id*2].r-s[id*2].l+1);
  27. s[id*2+1].sum=s[id].lazy*(s[id*2+1].r-s[id*2+1].l+1);
  28. s[id].lazy=0;
  29. }
  30. void rep(int id,int l,int r,int val)
  31. {
  32. if(l<=s[id].l&&s[id].r<=r)
  33. {
  34. s[id].lazy=val;
  35. s[id].sum=val*(s[id].r-s[id].l+1);
  36. return;
  37. }
  38. if(s[id].lazy!=0)
  39. pushdown(id);
  40. int mid=(s[id].l+s[id].r)/2;
  41. if(r<=mid)
  42. rep(id*2,l,r,val);
  43. else if(l>mid)
  44. rep(id*2+1,l,r,val);
  45. else
  46. {
  47. rep(id*2,l,r,val);
  48. rep(id*2+1,l,r,val);
  49. }
  50. s[id].sum=s[id*2].sum+s[id*2+1].sum;
  51. }
  52. long long sum(int id,int l,int r)
  53. {
  54. if(l<=s[id].l&&s[id].r<=r)
  55. return s[id].sum;
  56. if(s[id].lazy!=0)
  57. pushdown(id);
  58. int mid=(s[id].l+s[id].r)/2;
  59. if(r<=mid)
  60. return sum(id*2,l,r);
  61. else if(l>mid)
  62. return sum(id*2+1,l,r);
  63. else
  64. return sum(id*2,l,r)+sum(id*2+1,l,r);
  65. }
  66. int main()
  67. {
  68. int t,n,q,x,y,z,tt=0;
  69. scanf("%d",&t);
  70. while(t--)
  71. {
  72. tt++;
  73. scanf("%d%d",&n,&q);
  74. build(1,1,n);
  75. while(q--)
  76. {
  77. scanf("%d%d%d",&x,&y,&z);
  78. rep(1,x,y,z);
  79. }
  80. printf("Case %d: The total value of the hook is %lld.\n",tt,sum(1,1,n));
  81. }
  82. return 0;
  83. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注