POJ 3468-A Simple Problem with Integers(区间更新线段树)

Dear 丶 2022-08-21 10:47 144阅读 0赞

A Simple Problem with Integers

















Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 88545   Accepted: 27517
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c“ means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b“ means querying the sum of Aa, Aa+1, … , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

  1. 10 5
  2. 1 2 3 4 5 6 7 8 9 10
  3. Q 4 4
  4. Q 1 10
  5. Q 2 4
  6. C 3 6 3
  7. Q 2 4

Sample Output

  1. 4
  2. 55
  3. 9
  4. 15

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly—2007.11.25, Yang Yi

From:http://www.cnblogs.com/littlex/archive/2012/01/30/2331670.html

这题很好的体现了lazy_tag的思想,当要增加的区间覆盖当前区间,则直接打上标记返回,当下次查询这个区间儿子区间的时候,直接标记往下传。可以想象,这个标记表示的是这个区间表示的整个区间的标记是整个子树的性质,当你不需要用到这个区间的儿子区间的时候,你可以不把操作都做到底,而是要用到的时候再往下传!

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5. #define MAX 500000
  6. struct node
  7. {
  8. int l,r;
  9. long long val;//增量
  10. long long num;//总和
  11. } tree[4*MAX];
  12. long long num[MAX];
  13. void build(int l,int r,int index)//建树
  14. {
  15. tree[index].l=l;
  16. tree[index].r=r;
  17. if(tree[index].l==tree[index].r)
  18. {
  19. tree[index].num=num[l-1];
  20. return ;
  21. }
  22. int mid=(l+r)/2;
  23. build(l,mid,2*index);
  24. build(mid+1,r,2*index+1);
  25. tree[index].num=tree[index*2].num+tree[index*2+1].num;//每个节点和
  26. }
  27. void update(int l,int r,int c,int index)//更新
  28. {
  29. if(tree[index].l>=l&&tree[index].r<=r)
  30. {
  31. tree[index].num+=(tree[index].r-tree[index].l+1)*c;
  32. tree[index].val+=c;
  33. return ;
  34. }
  35. else
  36. {
  37. if(tree[index].val)//最关键部分就是这个标记下传,将增加值val传给他的左右孩子
  38. {
  39. tree[index*2].num+=tree[index].val*(tree[index*2].r-tree[index*2].l+1);
  40. tree[index*2].val+=tree[index].val;
  41. tree[index*2+1].num+=tree[index].val*(tree[index*2+1].r-tree[index*2+1].l+1);
  42. tree[index*2+1].val+=tree[index].val;
  43. tree[index].val=0;
  44. }
  45. if(r>=tree[index*2+1].l)
  46. update(l,r,c,2*index+1);
  47. if(l<=tree[index*2].r)
  48. update(l,r,c,2*index);
  49. tree[index].num=tree[index*2].num+tree[index*2+1].num;//每个节点和
  50. }
  51. }
  52. long long query(int a,int b,int index)//查询
  53. {
  54. if(tree[index].l>=a&&tree[index].r<=b)
  55. return tree[index].num;
  56. if(tree[index].val) //求和同样需要标记下传
  57. {
  58. tree[index*2].num+=tree[index].val*(tree[index*2].r-tree[index*2].l+1);
  59. tree[index*2].val+=tree[index].val;
  60. tree[index*2+1].num+=tree[index].val*(tree[index*2+1].r-tree[index*2+1].l+1);
  61. tree[index*2+1].val+=tree[index].val;
  62. tree[index].val=0;
  63. }
  64. int mid=(tree[index].l+tree[index].r)/2;
  65. long long max1=0,max2=0;
  66. if(mid<a)
  67. max1= query(a,b,2*index+1);
  68. else if(b<=mid)
  69. max1=query(a,b,2*index);
  70. else
  71. {
  72. max1=query(a,mid,2*index);
  73. max2=query(mid+1,b,2*index+1);
  74. }
  75. return max1+max2;
  76. }
  77. int main()
  78. {
  79. int n,i,m;
  80. while(scanf("%d%d",&n,&m)!=EOF)
  81. {
  82. memset(tree,0,sizeof(tree));
  83. for(i=0; i<n; i++)
  84. scanf("%I64d",&num[i]);
  85. build(1,n,1);
  86. for(i=0; i<m; i++)
  87. {
  88. char st;
  89. cin>>st;
  90. if(st=='C')
  91. {
  92. int a,b,c;
  93. scanf("%d%d%d",&a,&b,&c);
  94. update(a,b,c,1);
  95. }
  96. else
  97. {
  98. int a,b;
  99. long long sum=0;
  100. scanf("%d%d",&a,&b);
  101. sum=query(a,b,1);
  102. printf("%I64d\n",sum);
  103. }
  104. }
  105. }
  106. return 0;
  107. }

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读