CodeForces 315B(线段树+区间更新)

桃扇骨 2022-06-06 04:50 316阅读 0赞

问题描述:

Sereja has got an array, consisting of n integers, a1, a2, …, an. 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 vi-th array element equal to xi. In other words, perform the assignment avi = xi.
  2. Increase each array element by yi. In other words, perform n assignments ai = ai + yi (1 ≤ i ≤ n).
  3. Take a piece of paper and write out the qi-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, …, an (1 ≤ ai ≤ 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 ti (1 ≤ ti ≤ 3) that represents the operation type. If ti = 1, then it is followed by two integers vi and xi, (1 ≤ vi ≤ n, 1 ≤ xi ≤ 109). If ti = 2, then it is followed by integer yi (1 ≤ yi ≤ 104). And if ti = 3, then it is followed by integer qi (1 ≤ qi ≤ 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. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. using namespace std;
  6. const int maxn=1e5+100;
  7. struct note
  8. {
  9. int l,r,val;
  10. }tree[maxn*4];
  11. int a[maxn];
  12. void build(int root,int ll,int rr)
  13. {
  14. tree[root].l=ll;
  15. tree[root].r=rr;
  16. tree[root].val=0;
  17. if (ll==rr) {
  18. tree[root].val=a[ll];
  19. return ;
  20. }
  21. int mid=(tree[root].l+tree[root].r)>>1;
  22. build(root<<1,ll,mid);
  23. build(root<<1|1,mid+1,rr);
  24. }
  25. void pushdown(int root)
  26. {
  27. if (tree[root].val) {
  28. tree[root<<1].val+=tree[root].val;
  29. tree[root<<1|1].val+=tree[root].val;
  30. tree[root].val=0;
  31. }
  32. }
  33. void update(int root,int l,int r,int cur,int p)
  34. {
  35. int ll=tree[root].l;
  36. int rr=tree[root].r;
  37. if (l<=ll&&r>=rr) {
  38. if (p==2) tree[root].val+=cur;
  39. else tree[root].val=cur;
  40. return ;
  41. }
  42. pushdown(root);
  43. int mid=(ll+rr)>>1;
  44. if (l<=mid) update(root<<1,l,r,cur,p);
  45. if (r>mid) update(root<<1|1,l,r,cur,p);
  46. }
  47. int query(int root,int pos)
  48. {
  49. int l=tree[root].l;
  50. int r=tree[root].r;
  51. if (l==r) {
  52. return tree[root].val;
  53. }
  54. pushdown(root);
  55. int mid=(l+r)>>1;
  56. if (pos<=mid)
  57. return query(root<<1,pos);
  58. else
  59. return query(root<<1|1,pos);
  60. }
  61. int main()
  62. {
  63. int n,m;
  64. while (scanf("%d%d",&n,&m)!=EOF) {
  65. for (int i=1;i<=n;i++)
  66. scanf("%d",&a[i]);
  67. build(1,1,n);
  68. for (int i=1;i<=m;i++) {
  69. int t;
  70. scanf("%d",&t);
  71. if (t==1) {
  72. int pos,x;
  73. scanf("%d%d",&pos,&x);
  74. update(1,pos,pos,x,1);
  75. }
  76. else if (t==2) {
  77. int v;
  78. scanf("%d",&v);
  79. update(1,1,n,v,2);
  80. }
  81. else if (t==3) {
  82. int pos;
  83. scanf("%d",&pos);
  84. printf("%d\n",query(1,pos));
  85. }
  86. }
  87. }
  88. return 0;
  89. }

发表评论

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

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

相关阅读