POJ3468 A Simple Problem with Integers(zkw线段树lazy标记)

傷城~ 2021-09-16 03:36 348阅读 0赞

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 140421 Accepted: 43534
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

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

4
55
9
15
Hint

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

一道过了几万人的经典题,作为zkw的训练题很好。
值得注意的是,这题其实不需要打lazy标记,直接维护维护前缀前缀和即可。
事实上,因为满足区间减法,你也可以用树状数组写。并且还有二维进阶题目等着你。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <list>
  8. #include <map>
  9. #include <queue>
  10. #include <stack>
  11. #include <vector>
  12. #include <bitset>
  13. #include <assert.h>
  14. #include<algorithm>
  15. using namespace std;
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  18. #define pb push_back
  19. #define mp make_pair
  20. #define all(x) (x).begin(),(x).end()
  21. #define fi first
  22. #define se second
  23. #define SZ(x) ((int)(x).size())
  24. typedef vector<int> VI;
  25. typedef long long ll;
  26. typedef pair<int,int> PII;
  27. const ll mod=1000000007;
  28. ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){
  29. if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
  30. ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
  31. const int maxn = 270000;
  32. //head
  33. ll T[maxn],b[maxn];//b是lazy标记
  34. int n,q,M,h,p;
  35. void init()
  36. {
  37. scanf("%d%d",&n,&q);
  38. for(M = 1,h = 0;M < 2 + n;M <<= 1,h++);//h为树的高度,根的高度为0
  39. rep(i,M+1,M+n+1)
  40. scanf("%lld",&T[i]);
  41. per(i,1,M)
  42. T[i] = T[i<<1] + T[i<<1|1];//没有用差分版本
  43. }
  44. void down(int t)
  45. {
  46. per(i,1,h+1)//向下传递
  47. if(b[p=t>>i])
  48. {
  49. b[p] >>= 1;//子节点只增加一半
  50. T[p<<1] += b[p]; T[p<<1|1] += b[p];
  51. b[p<<1] += b[p]; b[p<<1|1] += b[p];
  52. b[p] = 0;
  53. }
  54. }
  55. void change(int l,int r,int k)
  56. {
  57. int ll,rr;
  58. l = l + M - 1;
  59. r = r + M + 1;
  60. ll = l>>1; rr = r>>1;
  61. down(l);down(r);
  62. for(;l^r^1;l >>= 1,r>>=1,k<<=1)
  63. {
  64. if(~l&1) T[l^1] += k,b[l^1] += k;
  65. if(r&1) T[r^1] += k,b[r^1] += k;
  66. }
  67. for(int i = ll;i;i>>=1) T[i] = T[i<<1] + T[i<<1|1];//向上更新
  68. for(int i = rr;i;i>>=1) T[i] = T[i<<1] + T[i<<1|1];
  69. }
  70. void query(int l ,int r)
  71. {
  72. ll ans = 0;
  73. l = l + M - 1;
  74. r = r + M + 1;
  75. down(l);down(r);
  76. for(;l^r^1;l>>=1,r>>=1)
  77. {
  78. if(~l&1) ans += T[l^1];
  79. if(r&1) ans += T[r^1];
  80. }
  81. printf("%lld\n",ans);
  82. }
  83. char c;
  84. int l,r,k;
  85. void work()
  86. {
  87. while(q--)
  88. {
  89. getchar();
  90. scanf("%c%d%d",&c,&l,&r);
  91. if(c == 'Q')
  92. query(l,r);
  93. else
  94. scanf("%d",&k),change(l,r,k);
  95. }
  96. }
  97. int main()
  98. {
  99. init();
  100. work();
  101. return 0;
  102. }

发表评论

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

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

相关阅读