[关闭]
@Chilling 2017-02-16T17:58:44.000000Z 字数 2840 阅读 959

HDU-4027: Can you answer these queries?(区间)

线段树


Description

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.

Input

The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than .
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.

Output

For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.

Sample Input

10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8 

Sample Output

Case #1:
19
7
6 

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<math.h>
  4. #define LL long long
  5. using namespace std;
  6. LL a[100005];
  7. struct node
  8. {
  9. int l,r,lazy;
  10. LL sum;
  11. }s[100005*4];
  12. void build(int id,int l,int r)
  13. {
  14. s[id].l=l;
  15. s[id].r=r;
  16. if(l==r)
  17. {
  18. s[id].sum=a[l];
  19. if(s[id].sum==1)
  20. s[id].lazy=0;
  21. else
  22. s[id].lazy=1;
  23. }
  24. else
  25. {
  26. int mid=(l+r)/2;
  27. build(id*2,l,mid);
  28. build(id*2+1,mid+1,r);
  29. s[id].sum=s[id*2].sum+s[id*2+1].sum;
  30. s[id].lazy=s[id*2].lazy+s[id*2+1].lazy;
  31. }
  32. }
  33. void rep(int id,int l,int r)
  34. {
  35. if(s[id].lazy==0) //都是0或者1,不用开方了!
  36. return;
  37. if(s[id].r<l||s[id].l>r)
  38. return;
  39. if(s[id].l==s[id].r)
  40. {
  41. s[id].sum=(LL)sqrt(s[id].sum);
  42. if(s[id].sum==1)
  43. s[id].lazy=0;
  44. return ;
  45. }
  46. else
  47. {
  48. int mid=(s[id].l+s[id].r)/2;
  49. if(r<=mid)
  50. rep(id*2,l,r);
  51. else if(l>mid)
  52. rep(id*2+1,l,r);
  53. else
  54. {
  55. rep(id*2,l,r);
  56. rep(id*2+1,l,r);
  57. }
  58. }
  59. s[id].sum=s[id*2].sum+s[id*2+1].sum;
  60. s[id].lazy=s[id*2].lazy+s[id*2+1].lazy;
  61. }
  62. LL sum(int id,int l,int r)
  63. {
  64. if(s[id].r<l||s[id].l>r)
  65. return 0;
  66. if(l<=s[id].l&&s[id].r<=r)
  67. return s[id].sum;
  68. int mid=(s[id].l+s[id].r)/2;
  69. if(r<=mid)
  70. return sum(id*2,l,r);
  71. else if(l>mid)
  72. return sum(id*2+1,l,r);
  73. else
  74. return sum(id*2,l,r)+sum(id*2+1,l,r);
  75. }
  76. int main()
  77. {
  78. int n,i,m,t,x,y,tt=0;
  79. while(scanf("%d",&n)!=EOF)
  80. {
  81. tt++;
  82. for(i=1;i<=n;i++)
  83. scanf("%lld",&a[i]);
  84. build(1,1,n);
  85. scanf("%d",&m);
  86. printf("Case #%d:\n",tt);
  87. while(m--)
  88. {
  89. scanf("%d%d%d",&t,&x,&y);
  90. if(x>y) swap(x,y);
  91. if(t==0)
  92. rep(1,x,y);
  93. if(t==1)
  94. printf("%lld\n",sum(1,x,y));
  95. }
  96. printf("\n");
  97. }
  98. return 0;
  99. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注