codeforces 315 B.Sereja and Array(线段树区间更新+单点更新+单点询问)

Bertha 。 2022-06-06 03:57 297阅读 0赞

Sereja has got an array, consisting of n integers, a1, a2, …, a**n. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms:

  1. Make v**i-th array element equal to x**i. In other words, perform the assignment avi = x**i.
  2. Increase each array element by y**i. In other words, perform n assignments a**i = a**i + y**i (1 ≤ i ≤ n).
  3. Take a piece of paper and write out the q**i-th array element. That is, the element aqi.

Help Sereja, complete all his operations.

Input

The first line contains integers n, m (1 ≤ n, m ≤ 105). The second line contains nspace-separated integers a1, a2, …, a**n (1 ≤ a**i ≤ 109) — the original array.

Next m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer t**i (1 ≤ t**i ≤ 3) that represents the operation type. If t**i = 1, then it is followed by two integers v**i and x**i, (1 ≤ v**i ≤ n, 1 ≤ x**i ≤ 109). If t**i = 2, then it is followed by integer y**i (1 ≤ y**i ≤ 104). And if t**i = 3, then it is followed by integer q**i (1 ≤ q**i ≤ n).

Output

For each third type operation print value aqi. Print the values in the order, in which the corresponding queries follow in the input.

Example

Input

  1. 10 11
  2. 1 2 3 4 5 6 7 8 9 10
  3. 3 2
  4. 3 9
  5. 2 10
  6. 3 1
  7. 3 10
  8. 1 1 10
  9. 2 10
  10. 2 10
  11. 3 1
  12. 3 10
  13. 3 9

Output

  1. 2
  2. 9
  3. 11
  4. 20
  5. 30
  6. 40
  7. 39

题解:

题意:

操作1 x y表示将a[x]=y

操作2 x 表示全体数列值加上x

操作3 x 表示询问a[x]处的值

思路:

比较裸的一道线段树的题,然后这里的区间更新因为是全体更新所以可以偷懒

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. using namespace std;
  16. #define lson k*2
  17. #define rson k*2+1
  18. #define M (t[k].l+t[k].r)/2
  19. struct node
  20. {
  21. int v;
  22. int tag;
  23. int l,r;
  24. }t[100005*4];
  25. void pushdown(int k)
  26. {
  27. if(t[k].tag)
  28. {
  29. t[lson].tag+=t[k].tag;
  30. t[rson].tag+=t[k].tag;
  31. if(t[lson].l==t[lson].r)
  32. t[lson].v+=t[k].tag;
  33. if(t[rson].l==t[rson].r)
  34. t[rson].v+=t[k].tag;
  35. t[k].tag=0;
  36. }
  37. }
  38. void Build(int l,int r,int k)
  39. {
  40. t[k].l=l;
  41. t[k].r=r;
  42. t[k].tag=0;
  43. if(l==r)
  44. {
  45. scanf("%d",&t[k].v);
  46. return;
  47. }
  48. int mid=M;
  49. Build(l,mid,lson);
  50. Build(mid+1,r,rson);
  51. }
  52. void update(int k,int v)
  53. {
  54. t[k].v+=v;
  55. t[k].tag+=v;
  56. }
  57. void change(int x,int v,int k)
  58. {
  59. if(t[k].l==x&&t[k].r==x)
  60. {
  61. t[k].v=v;
  62. return;
  63. }
  64. pushdown(k);
  65. int mid=M;
  66. if(x<=mid)
  67. change(x,v,lson);
  68. else
  69. change(x,v,rson);
  70. }
  71. int query(int x,int k)
  72. {
  73. if(t[k].l==x&&t[k].r==x)
  74. {
  75. return t[k].v;
  76. }
  77. pushdown(k);
  78. int mid=M;
  79. if(x<=mid)
  80. return query(x,lson);
  81. else
  82. return query(x,rson);
  83. }
  84. int main()
  85. {
  86. int i,j,n,m,d,x,y;
  87. scanf("%d%d",&n,&m);
  88. Build(1,n,1);
  89. while(m--)
  90. {
  91. scanf("%d",&d);
  92. if(d==1)
  93. {
  94. scanf("%d%d",&x,&y);
  95. change(x,y,1);
  96. }
  97. else if(d==2)
  98. {
  99. scanf("%d",&x);
  100. update(1,x);
  101. }
  102. else
  103. {
  104. scanf("%d",&x);
  105. printf("%d\n",query(x,1));
  106. }
  107. }
  108. return 0;
  109. }

发表评论

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

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

相关阅读